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

Side by Side Diff: runtime/bin/sync_socket_win.cc

Issue 2830273002: [dart:io][windows] Implements RawSynchronousSocket (Closed)
Patch Set: Fix test and statuses Created 3 years, 8 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
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #if !defined(DART_IO_DISABLED) 5 #if !defined(DART_IO_DISABLED)
6 6
7 #include "platform/globals.h" 7 #include "platform/globals.h"
8 #if defined(HOST_OS_WINDOWS) 8 #if defined(HOST_OS_WINDOWS)
9 9
10 #include "bin/socket_base.h"
10 #include "bin/sync_socket.h" 11 #include "bin/sync_socket.h"
11 12
12 #include "bin/builtin.h"
13 #include "bin/log.h"
14 #include "bin/utils.h"
15 #include "bin/utils_win.h"
16
17 // #define SOCKET_LOG_ERROR 1
18
19 // define SOCKET_LOG_ERROR to get log messages only for errors.
20 #if defined(SOCKET_LOG_ERROR)
21 #define LOG_ERR(msg, ...) \
22 { \
23 int err = errno; \
24 Log::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \
25 ##__VA_ARGS__); \
26 errno = err; \
27 }
28 #else
29 #define LOG_ERR(msg, ...)
30 #endif // defined(SOCKET_LOG_ERROR)
31
32 namespace dart { 13 namespace dart {
33 namespace bin { 14 namespace bin {
34 15
35 SynchronousSocket::SynchronousSocket(intptr_t fd) { 16 SynchronousSocket::SynchronousSocket(intptr_t fd) : fd_(fd) {}
36 LOG_ERR("SynchronousSocket is unimplemented\n"); 17
37 UNIMPLEMENTED(); 18
19 void SynchronousSocket::SetClosedFd() {
20 fd_ = kClosedFd;
38 } 21 }
siva 2017/04/21 16:40:36 This function seems to be the same in all versions
zra 2017/04/22 06:29:28 Done.
39 22
40 23
41 bool SynchronousSocket::Initialize() { 24 bool SynchronousSocket::Initialize() {
42 LOG_ERR("SynchronousSocket::Initialize is unimplemented\n"); 25 return SocketBase::Initialize();
43 UNIMPLEMENTED();
44 return false;
45 } 26 }
46 27
47 28
48 void SynchronousSocket::SetClosedFd() { 29 static intptr_t Create(const RawAddr& addr) {
49 LOG_ERR("SynchronousSocket::SetClosedFd is unimplemented\n"); 30 const intptr_t type = SOCK_STREAM;
50 UNIMPLEMENTED(); 31 SOCKET s = WSASocket(addr.ss.ss_family, type, 0, NULL, 0, 0);
32 return (s == INVALID_SOCKET) ? -1 : s;
33 }
34
35
36 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
37 SOCKET socket = static_cast<SOCKET>(fd);
38 intptr_t result =
39 connect(socket, &addr.addr, SocketAddress::GetAddrLength(addr));
40 return (result == SOCKET_ERROR) ? -1 : socket;
51 } 41 }
52 42
53 43
54 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { 44 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) {
55 LOG_ERR("SynchronousSocket::CreateConnect is unimplemented\n"); 45 intptr_t fd = Create(addr);
56 UNIMPLEMENTED(); 46 return (fd < 0) ? fd : Connect(fd, addr);
57 return -1; 47 }
48
49
50 intptr_t SynchronousSocket::Available(intptr_t fd) {
51 SOCKET socket = static_cast<SOCKET>(fd);
52 DWORD available;
53 intptr_t result = ioctlsocket(socket, FIONREAD, &available);
54 return (result == SOCKET_ERROR) ? -1 : static_cast<intptr_t>(available);
55 }
56
57
58 intptr_t SynchronousSocket::GetPort(intptr_t fd) {
59 SOCKET socket = static_cast<SOCKET>(fd);
60 RawAddr raw;
61 socklen_t size = sizeof(raw);
62 if (getsockname(socket, &raw.addr, &size) == SOCKET_ERROR) {
63 return 0;
64 }
65 return SocketAddress::GetAddrPort(raw);
66 }
67
68
69 SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) {
70 SOCKET socket = static_cast<SOCKET>(fd);
71 RawAddr raw;
72 socklen_t size = sizeof(raw);
73 if (getpeername(socket, &raw.addr, &size)) {
74 return NULL;
75 }
76 *port = SocketAddress::GetAddrPort(raw);
77 // Clear the port before calling WSAAddressToString as WSAAddressToString
78 // includes the port in the formatted string.
79 SocketAddress::SetAddrPort(&raw, 0);
80 return new SocketAddress(&raw.addr);
81 }
82
83
84 intptr_t SynchronousSocket::Read(intptr_t fd,
85 void* buffer,
86 intptr_t num_bytes) {
87 SOCKET socket = static_cast<SOCKET>(fd);
88 return recv(socket, reinterpret_cast<char*>(buffer), num_bytes, 0);
siva 2017/04/21 16:40:36 The behavior in SocketBase::Read for EAGAIN seems
zra 2017/04/22 06:29:28 Added an extra parameter to SocketBase::Read(), et
89 }
90
91
92 intptr_t SynchronousSocket::Write(intptr_t fd,
93 const void* buffer,
94 intptr_t num_bytes) {
95 SOCKET socket = static_cast<SOCKET>(fd);
96 return send(socket, reinterpret_cast<const char*>(buffer), num_bytes, 0);
siva 2017/04/21 16:40:36 Ditto comment about behavior being different from
zra 2017/04/22 06:29:28 Done.
58 } 97 }
59 98
60 99
61 void SynchronousSocket::ShutdownRead(intptr_t fd) { 100 void SynchronousSocket::ShutdownRead(intptr_t fd) {
62 LOG_ERR("SynchronousSocket::ShutdownRead is unimplemented\n"); 101 SOCKET socket = static_cast<SOCKET>(fd);
63 UNIMPLEMENTED(); 102 shutdown(socket, SD_RECEIVE);
64 } 103 }
65 104
66 105
67 void SynchronousSocket::ShutdownWrite(intptr_t fd) { 106 void SynchronousSocket::ShutdownWrite(intptr_t fd) {
68 LOG_ERR("SynchronousSocket::ShutdownWrite is unimplemented\n"); 107 SOCKET socket = static_cast<SOCKET>(fd);
69 UNIMPLEMENTED(); 108 shutdown(socket, SD_SEND);
70 } 109 }
71 110
72 111
112 void SynchronousSocket::Close(intptr_t fd) {
113 SOCKET socket = static_cast<SOCKET>(fd);
114 closesocket(socket);
115 }
siva 2017/04/21 16:40:36 I suppose datagram sync sockets are not implemente
zra 2017/04/22 06:29:28 Right.
116
73 } // namespace bin 117 } // namespace bin
74 } // namespace dart 118 } // namespace dart
75 119
76 #endif // defined(HOST_OS_WINDOWS) 120 #endif // defined(HOST_OS_WINDOWS)
77 121
78 #endif // !defined(DART_IO_DISABLED) 122 #endif // !defined(DART_IO_DISABLED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698