Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: src/platform-win32.cc

Issue 27085: Add socket support to platform code.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform-macos.cc ('k') | test/cctest/SConscript » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 #include <mmsystem.h> // For timeGetTime(). 60 #include <mmsystem.h> // For timeGetTime().
61 #ifndef __MINGW32__ 61 #ifndef __MINGW32__
62 #include <dbghelp.h> // For SymLoadModule64 and al. 62 #include <dbghelp.h> // For SymLoadModule64 and al.
63 #endif // __MINGW32__ 63 #endif // __MINGW32__
64 #include <limits.h> // For INT_MAX and al. 64 #include <limits.h> // For INT_MAX and al.
65 #include <tlhelp32.h> // For Module32First and al. 65 #include <tlhelp32.h> // For Module32First and al.
66 66
67 // These additional WIN32 includes have to be right here as the #undef's below 67 // These additional WIN32 includes have to be right here as the #undef's below
68 // makes it impossible to have them elsewhere. 68 // makes it impossible to have them elsewhere.
69 #include <winsock2.h> 69 #include <winsock2.h>
70 #include <ws2tcpip.h>
70 #include <process.h> // for _beginthreadex() 71 #include <process.h> // for _beginthreadex()
71 #include <stdlib.h> 72 #include <stdlib.h>
72 73
73 #undef VOID 74 #undef VOID
74 #undef DELETE 75 #undef DELETE
75 #undef IN 76 #undef IN
76 #undef THIS 77 #undef THIS
77 #undef CONST 78 #undef CONST
78 #undef NAN 79 #undef NAN
79 #undef GetObject 80 #undef GetObject
(...skipping 1464 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 1545
1545 private: 1546 private:
1546 HANDLE sem; 1547 HANDLE sem;
1547 }; 1548 };
1548 1549
1549 1550
1550 Semaphore* OS::CreateSemaphore(int count) { 1551 Semaphore* OS::CreateSemaphore(int count) {
1551 return new Win32Semaphore(count); 1552 return new Win32Semaphore(count);
1552 } 1553 }
1553 1554
1555
1556 // ----------------------------------------------------------------------------
1557 // Win32 socket support.
1558 //
1559
1560 class Win32Socket : public Socket {
1561 public:
1562 explicit Win32Socket() {
1563 // Create the socket.
1564 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1565 }
1566 explicit Win32Socket(SOCKET socket): socket_(socket) { }
1567
1568
1569 virtual ~Win32Socket() {
1570 if (IsValid()) {
1571 // Close socket.
1572 closesocket(socket_);
1573 }
1574 }
1575
1576 // Server initialization.
1577 bool Bind (const int port);
1578 bool Listen(int backlog) const;
1579 Socket* Accept () const;
1580
1581 // Client initialization.
1582 bool Connect(const char* host, const char* port);
1583
1584 // Data Transimission
1585 int Send(const char* data, int len) const;
1586 bool SendAll(const char* data, int len) const;
1587 int Receive(char* data, int len) const;
1588
1589 bool IsValid() const { return socket_ != INVALID_SOCKET; }
1590
1591 private:
1592 SOCKET socket_;
1593 };
1594
1595
1596 bool Win32Socket::Bind(const int port) {
1597 if (!IsValid()) {
1598 return false;
1599 }
1600
1601 sockaddr_in addr;
1602 memset(&addr, 0, sizeof(addr));
1603 addr.sin_family = AF_INET;
1604 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1605 addr.sin_port = htons(port);
1606 int status = bind(socket_,
1607 reinterpret_cast<struct sockaddr *>(&addr),
1608 sizeof (addr));
1609 return status == 0;
1610 }
1611
1612
1613 bool Win32Socket::Listen(int backlog) const {
1614 if (!IsValid()) {
1615 return false;
1616 }
1617
1618 int status = listen(socket_, backlog);
1619 return status == 0;
1620 }
1621
1622
1623 Socket* Win32Socket::Accept() const {
1624 if (!IsValid()) {
1625 return NULL;
1626 }
1627
1628 SOCKET socket = accept(socket_, NULL, NULL);
1629 if (socket == INVALID_SOCKET) {
1630 return NULL;
1631 } else {
1632 return new Win32Socket(socket);
1633 }
1634 }
1635
1636
1637 bool Win32Socket::Connect(const char* host, const char* port) {
1638 if (!IsValid()) {
1639 return false;
1640 }
1641
1642 // Lookup host and port.
1643 struct addrinfo *result = NULL;
1644 struct addrinfo hints;
1645 memset(&hints, 0, sizeof(addrinfo));
1646 hints.ai_family = AF_INET;
1647 hints.ai_socktype = SOCK_STREAM;
1648 hints.ai_protocol = IPPROTO_TCP;
1649 int status = getaddrinfo(host, port, &hints, &result);
1650 if (status != 0) {
1651 return false;
1652 }
1653
1654 // Connect.
1655 status = connect(socket_, result->ai_addr, result->ai_addrlen);
1656 return status == 0;
1657 }
1658
1659
1660 int Win32Socket::Send(const char* data, int len) const {
1661 int status = send(socket_, data, len, 0);
1662 return status;
1663 }
1664
1665
1666 bool Win32Socket::SendAll(const char* data, int len) const {
1667 int sent_len = 0;
1668 while (sent_len < len) {
1669 int status = Send(data, len);
1670 if (status <= 0) {
1671 return false;
1672 }
1673 sent_len += status;
1674 }
1675 return true;
1676 }
1677
1678
1679 int Win32Socket::Receive(char* data, int len) const {
1680 int status = recv(socket_, data, len, 0);
1681 return status;
1682 }
1683
1684
1685 bool Socket::Setup() {
1686 // Initialize Winsock32
1687 int err;
1688 WSADATA winsock_data;
1689 WORD version_requested = MAKEWORD(1, 0);
1690 err = WSAStartup(version_requested, &winsock_data);
1691 if (err != 0) {
1692 PrintF("Unable to initialize Winsock, err = %d\n", Socket::LastError());
1693 }
1694
1695 return err == 0;
1696 }
1697
1698
1699 int Socket::LastError() {
1700 return WSAGetLastError();
1701 }
1702
1703
1704 uint16_t Socket::HToN(uint16_t value) {
1705 return htons(value);
1706 }
1707
1708
1709 uint16_t Socket::NToH(uint16_t value) {
1710 return ntohs(value);
1711 }
1712
1713
1714 uint32_t Socket::HToN(uint32_t value) {
1715 return htonl(value);
1716 }
1717
1718
1719 uint32_t Socket::NToH(uint32_t value) {
1720 return ntohl(value);
1721 }
1722
1723
1724 Socket* OS::CreateSocket() {
1725 return new Win32Socket();
1726 }
1727
1728
1554 #ifdef ENABLE_LOGGING_AND_PROFILING 1729 #ifdef ENABLE_LOGGING_AND_PROFILING
1555 1730
1556 // ---------------------------------------------------------------------------- 1731 // ----------------------------------------------------------------------------
1557 // Win32 profiler support. 1732 // Win32 profiler support.
1558 // 1733 //
1559 // On win32 we use a sampler thread with high priority to sample the program 1734 // On win32 we use a sampler thread with high priority to sample the program
1560 // counter for the profiled thread. 1735 // counter for the profiled thread.
1561 1736
1562 class Sampler::PlatformData : public Malloced { 1737 class Sampler::PlatformData : public Malloced {
1563 public: 1738 public:
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1662 1837
1663 // Release the thread handles 1838 // Release the thread handles
1664 CloseHandle(data_->sampler_thread_); 1839 CloseHandle(data_->sampler_thread_);
1665 CloseHandle(data_->profiled_thread_); 1840 CloseHandle(data_->profiled_thread_);
1666 } 1841 }
1667 1842
1668 1843
1669 #endif // ENABLE_LOGGING_AND_PROFILING 1844 #endif // ENABLE_LOGGING_AND_PROFILING
1670 1845
1671 } } // namespace v8::internal 1846 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-macos.cc ('k') | test/cctest/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698