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

Side by Side Diff: src/platform/socket.cc

Issue 23484014: Cleanup Socket class and remove it from the platform files. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Cleanup leftover junk, and rename test-sockets to test-socket. Created 7 years, 3 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
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "platform/socket.h"
29
30 #if V8_OS_POSIX
31 #include <sys/types.h>
32 #include <sys/socket.h>
33
34 #include <netinet/in.h>
35 #include <netdb.h>
36
37 #include <unistd.h>
38 #endif
39
40 #include <cerrno>
41
42 #include "checks.h"
43 #include "once.h"
44
45 namespace v8 {
46 namespace internal {
47
48 #if V8_OS_WIN
49
50 static V8_DECLARE_ONCE(initialize_winsock) = V8_ONCE_INIT;
51
52
53 static void InitializeWinsock() {
54 WSADATA wsa_data;
55 int result = WSAStartup(MAKEWORD(1, 0), &wsa_data);
56 CHECK_EQ(0, result);
57 }
58
59 #endif // V8_OS_WIN
60
61
62 Socket::Socket() {
63 #if V8_OS_WIN
64 // Be sure to initialize the WinSock DLL first.
65 CallOnce(&initialize_winsock, &InitializeWinsock);
66 #endif // V8_OS_WIN
67
68 // Create the native socket handle.
69 native_handle_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
70 }
71
72
73 bool Socket::Bind(int port) {
74 ASSERT_GE(port, 0);
75 ASSERT_LT(port, 65536);
76 if (!IsValid()) return false;
77 struct sockaddr_in sin;
78 memset(&sin, 0, sizeof(sin));
79 sin.sin_family = AF_INET;
80 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
81 sin.sin_port = htons(static_cast<uint16_t>(port));
82 int result = ::bind(
83 native_handle_, reinterpret_cast<struct sockaddr*>(&sin), sizeof(sin));
84 return result == 0;
85 }
86
87
88 bool Socket::Listen(int backlog) {
89 if (!IsValid()) return false;
90 int result = ::listen(native_handle_, backlog);
91 return result == 0;
92 }
93
94
95 Socket* Socket::Accept() {
96 if (!IsValid()) return NULL;
97 while (true) {
98 NativeHandle native_handle = ::accept(native_handle_, NULL, NULL);
99 if (native_handle == kInvalidNativeHandle) {
100 #if V8_OS_POSIX
101 if (errno == EINTR) continue; // Retry after signal.
102 #endif
103 return NULL;
104 }
105 return new Socket(native_handle);
106 }
107 }
108
109
110 bool Socket::Connect(const char* host, const char* port) {
111 ASSERT_NE(NULL, host);
112 ASSERT_NE(NULL, port);
113 if (!IsValid()) return false;
114
115 // Lookup host and port.
116 struct addrinfo* info = NULL;
117 struct addrinfo hint;
118 memset(&hint, 0, sizeof(hint));
119 hint.ai_family = AF_INET;
120 hint.ai_socktype = SOCK_STREAM;
121 hint.ai_protocol = IPPROTO_TCP;
122 int result = ::getaddrinfo(host, port, &hint, &info);
123 if (result != 0) {
124 return false;
125 }
126
127 // Connect to the host on the given port.
128 for (struct addrinfo* ai = info; ai != NULL; ai = ai->ai_next) {
129 // Try to connect using this addr info.
130 while (true) {
131 result = ::connect(native_handle_, ai->ai_addr, ai->ai_addrlen);
132 if (result == 0) {
133 freeaddrinfo(info);
134 return true;
135 }
136 #if V8_OS_POSIX
137 if (errno == EINTR) continue; // Retry after signal.
138 #endif
139 break;
140 }
141 }
142 freeaddrinfo(info);
143 return false;
144 }
145
146
147 bool Socket::Shutdown() {
148 if (!IsValid()) return false;
149 // Shutdown socket for both read and write.
150 #if V8_OS_POSIX
151 int result = ::shutdown(native_handle_, SHUT_RDWR);
152 ::close(native_handle_);
153 #elif V8_OS_WIN
154 int result = ::shutdown(native_handle_, SD_BOTH);
155 ::closesocket(native_handle_);
156 #endif
157 native_handle_ = kInvalidNativeHandle;
158 return result == 0;
159 }
160
161
162 int Socket::Send(const char* buffer, int length) {
163 ASSERT(length <= 0 || buffer != NULL);
164 if (!IsValid()) return 0;
165 int offset = 0;
166 while (offset < length) {
167 int result = ::send(native_handle_, buffer + offset, length - offset, 0);
168 if (result == 0) {
169 break;
170 } else if (result > 0) {
171 ASSERT(result <= length - offset);
172 offset += result;
173 } else {
174 #if V8_OS_POSIX
175 if (errno == EINTR) continue; // Retry after signal.
176 #endif
177 return 0;
178 }
179 }
180 return offset;
181 }
182
183
184 int Socket::Receive(char* buffer, int length) {
185 if (!IsValid()) return 0;
186 if (length <= 0) return 0;
187 ASSERT_NE(NULL, buffer);
188 while (true) {
189 int result = ::recv(native_handle_, buffer, length, 0);
190 if (result < 0) {
191 #if V8_OS_POSIX
192 if (errno == EINTR) continue; // Retry after signal.
193 #endif
194 return 0;
195 }
196 return result;
197 }
198 }
199
200
201 bool Socket::SetReuseAddress(bool reuse_address) {
202 if (!IsValid()) return 0;
203 int v = reuse_address ? 1 : 0;
204 int result = ::setsockopt(native_handle_, SOL_SOCKET, SO_REUSEADDR,
205 reinterpret_cast<char*>(&v), sizeof(v));
206 return result == 0;
207 }
208
209
210 // static
211 int Socket::GetLastError() {
212 #if V8_OS_POSIX
213 return errno;
214 #elif V8_OS_WIN
215 // Be sure to initialize the WinSock DLL first.
216 CallOnce(&initialize_winsock, &InitializeWinsock);
217
218 // Now we can safely perform WSA calls.
219 return ::WSAGetLastError();
220 #endif
221 }
222
223 } } // namespace v8::internal
OLDNEW
« src/platform.h ('K') | « src/platform/socket.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698