OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1608 ASSERT(result); | 1608 ASSERT(result); |
1609 } | 1609 } |
1610 | 1610 |
1611 | 1611 |
1612 | 1612 |
1613 void Thread::YieldCPU() { | 1613 void Thread::YieldCPU() { |
1614 Sleep(0); | 1614 Sleep(0); |
1615 } | 1615 } |
1616 | 1616 |
1617 | 1617 |
1618 // ---------------------------------------------------------------------------- | |
1619 // Win32 socket support. | |
1620 // | |
1621 | |
1622 class Win32Socket : public Socket { | |
1623 public: | |
1624 explicit Win32Socket() { | |
1625 // Create the socket. | |
1626 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
1627 } | |
1628 explicit Win32Socket(SOCKET socket): socket_(socket) { } | |
1629 virtual ~Win32Socket() { Shutdown(); } | |
1630 | |
1631 // Server initialization. | |
1632 bool Bind(const int port); | |
1633 bool Listen(int backlog) const; | |
1634 Socket* Accept() const; | |
1635 | |
1636 // Client initialization. | |
1637 bool Connect(const char* host, const char* port); | |
1638 | |
1639 // Shutdown socket for both read and write. | |
1640 bool Shutdown(); | |
1641 | |
1642 // Data Transimission | |
1643 int Send(const char* data, int len) const; | |
1644 int Receive(char* data, int len) const; | |
1645 | |
1646 bool SetReuseAddress(bool reuse_address); | |
1647 | |
1648 bool IsValid() const { return socket_ != INVALID_SOCKET; } | |
1649 | |
1650 private: | |
1651 SOCKET socket_; | |
1652 }; | |
1653 | |
1654 | |
1655 bool Win32Socket::Bind(const int port) { | |
1656 if (!IsValid()) { | |
1657 return false; | |
1658 } | |
1659 | |
1660 sockaddr_in addr; | |
1661 memset(&addr, 0, sizeof(addr)); | |
1662 addr.sin_family = AF_INET; | |
1663 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1664 addr.sin_port = htons(port); | |
1665 int status = bind(socket_, | |
1666 reinterpret_cast<struct sockaddr *>(&addr), | |
1667 sizeof(addr)); | |
1668 return status == 0; | |
1669 } | |
1670 | |
1671 | |
1672 bool Win32Socket::Listen(int backlog) const { | |
1673 if (!IsValid()) { | |
1674 return false; | |
1675 } | |
1676 | |
1677 int status = listen(socket_, backlog); | |
1678 return status == 0; | |
1679 } | |
1680 | |
1681 | |
1682 Socket* Win32Socket::Accept() const { | |
1683 if (!IsValid()) { | |
1684 return NULL; | |
1685 } | |
1686 | |
1687 SOCKET socket = accept(socket_, NULL, NULL); | |
1688 if (socket == INVALID_SOCKET) { | |
1689 return NULL; | |
1690 } else { | |
1691 return new Win32Socket(socket); | |
1692 } | |
1693 } | |
1694 | |
1695 | |
1696 bool Win32Socket::Connect(const char* host, const char* port) { | |
1697 if (!IsValid()) { | |
1698 return false; | |
1699 } | |
1700 | |
1701 // Lookup host and port. | |
1702 struct addrinfo *result = NULL; | |
1703 struct addrinfo hints; | |
1704 memset(&hints, 0, sizeof(addrinfo)); | |
1705 hints.ai_family = AF_INET; | |
1706 hints.ai_socktype = SOCK_STREAM; | |
1707 hints.ai_protocol = IPPROTO_TCP; | |
1708 int status = getaddrinfo(host, port, &hints, &result); | |
1709 if (status != 0) { | |
1710 return false; | |
1711 } | |
1712 | |
1713 // Connect. | |
1714 status = connect(socket_, | |
1715 result->ai_addr, | |
1716 static_cast<int>(result->ai_addrlen)); | |
1717 freeaddrinfo(result); | |
1718 return status == 0; | |
1719 } | |
1720 | |
1721 | |
1722 bool Win32Socket::Shutdown() { | |
1723 if (IsValid()) { | |
1724 // Shutdown socket for both read and write. | |
1725 int status = shutdown(socket_, SD_BOTH); | |
1726 closesocket(socket_); | |
1727 socket_ = INVALID_SOCKET; | |
1728 return status == SOCKET_ERROR; | |
1729 } | |
1730 return true; | |
1731 } | |
1732 | |
1733 | |
1734 int Win32Socket::Send(const char* data, int len) const { | |
1735 if (len <= 0) return 0; | |
1736 int written = 0; | |
1737 while (written < len) { | |
1738 int status = send(socket_, data + written, len - written, 0); | |
1739 if (status == 0) { | |
1740 break; | |
1741 } else if (status > 0) { | |
1742 written += status; | |
1743 } else { | |
1744 return 0; | |
1745 } | |
1746 } | |
1747 return written; | |
1748 } | |
1749 | |
1750 | |
1751 int Win32Socket::Receive(char* data, int len) const { | |
1752 if (len <= 0) return 0; | |
1753 int status = recv(socket_, data, len, 0); | |
1754 return (status == SOCKET_ERROR) ? 0 : status; | |
1755 } | |
1756 | |
1757 | |
1758 bool Win32Socket::SetReuseAddress(bool reuse_address) { | |
1759 BOOL on = reuse_address ? true : false; | |
1760 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, | |
1761 reinterpret_cast<char*>(&on), sizeof(on)); | |
1762 return status == SOCKET_ERROR; | |
1763 } | |
1764 | |
1765 | |
1766 bool Socket::SetUp() { | |
1767 // Initialize Winsock32 | |
1768 int err; | |
1769 WSADATA winsock_data; | |
1770 WORD version_requested = MAKEWORD(1, 0); | |
1771 err = WSAStartup(version_requested, &winsock_data); | |
1772 if (err != 0) { | |
1773 PrintF("Unable to initialize Winsock, err = %d\n", Socket::LastError()); | |
1774 } | |
1775 | |
1776 return err == 0; | |
1777 } | |
1778 | |
1779 | |
1780 int Socket::LastError() { | |
1781 return WSAGetLastError(); | |
1782 } | |
1783 | |
1784 | |
1785 uint16_t Socket::HToN(uint16_t value) { | |
1786 return htons(value); | |
1787 } | |
1788 | |
1789 | |
1790 uint16_t Socket::NToH(uint16_t value) { | |
1791 return ntohs(value); | |
1792 } | |
1793 | |
1794 | |
1795 uint32_t Socket::HToN(uint32_t value) { | |
1796 return htonl(value); | |
1797 } | |
1798 | |
1799 | |
1800 uint32_t Socket::NToH(uint32_t value) { | |
1801 return ntohl(value); | |
1802 } | |
1803 | |
1804 | |
1805 Socket* OS::CreateSocket() { | |
1806 return new Win32Socket(); | |
1807 } | |
1808 | |
1809 | |
1810 void OS::SetUp() { | 1618 void OS::SetUp() { |
1811 // Seed the random number generator. | 1619 // Seed the random number generator. |
1812 // Convert the current time to a 64-bit integer first, before converting it | 1620 // Convert the current time to a 64-bit integer first, before converting it |
1813 // to an unsigned. Going directly can cause an overflow and the seed to be | 1621 // to an unsigned. Going directly can cause an overflow and the seed to be |
1814 // set to all ones. The seed will be identical for different instances that | 1622 // set to all ones. The seed will be identical for different instances that |
1815 // call this setup code within the same millisecond. | 1623 // call this setup code within the same millisecond. |
1816 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); | 1624 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
1817 srand(static_cast<unsigned int>(seed)); | 1625 srand(static_cast<unsigned int>(seed)); |
1818 limit_mutex = new Mutex(); | 1626 limit_mutex = new Mutex(); |
1819 } | 1627 } |
1820 | 1628 |
1821 | 1629 |
1822 void OS::TearDown() { | 1630 void OS::TearDown() { |
1823 delete limit_mutex; | 1631 delete limit_mutex; |
1824 } | 1632 } |
1825 | 1633 |
1826 | 1634 |
1827 } } // namespace v8::internal | 1635 } } // namespace v8::internal |
OLD | NEW |