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

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

Issue 2830273002: [dart:io][windows] Implements RawSynchronousSocket (Closed)
Patch Set: Fix Linux build 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
« no previous file with comments | « runtime/bin/sync_socket_macos.cc ('k') | tests/standalone/io/raw_synchronous_socket_test.dart » ('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 (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 bool SynchronousSocket::Initialize() {
36 LOG_ERR("SynchronousSocket is unimplemented\n"); 17 return SocketBase::Initialize();
37 UNIMPLEMENTED();
38 } 18 }
39 19
40 20
41 bool SynchronousSocket::Initialize() { 21 static intptr_t Create(const RawAddr& addr) {
42 LOG_ERR("SynchronousSocket::Initialize is unimplemented\n"); 22 const intptr_t type = SOCK_STREAM;
43 UNIMPLEMENTED(); 23 SOCKET s = WSASocket(addr.ss.ss_family, type, 0, NULL, 0, 0);
44 return false; 24 return (s == INVALID_SOCKET) ? -1 : s;
45 } 25 }
46 26
47 27
48 void SynchronousSocket::SetClosedFd() { 28 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
49 LOG_ERR("SynchronousSocket::SetClosedFd is unimplemented\n"); 29 SOCKET socket = static_cast<SOCKET>(fd);
50 UNIMPLEMENTED(); 30 intptr_t result =
31 connect(socket, &addr.addr, SocketAddress::GetAddrLength(addr));
32 return (result == SOCKET_ERROR) ? -1 : socket;
51 } 33 }
52 34
53 35
54 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { 36 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) {
55 LOG_ERR("SynchronousSocket::CreateConnect is unimplemented\n"); 37 intptr_t fd = Create(addr);
56 UNIMPLEMENTED(); 38 return (fd < 0) ? fd : Connect(fd, addr);
57 return -1; 39 }
40
41
42 intptr_t SynchronousSocket::Available(intptr_t fd) {
43 SOCKET socket = static_cast<SOCKET>(fd);
44 DWORD available;
45 intptr_t result = ioctlsocket(socket, FIONREAD, &available);
46 return (result == SOCKET_ERROR) ? -1 : static_cast<intptr_t>(available);
47 }
48
49
50 intptr_t SynchronousSocket::GetPort(intptr_t fd) {
51 SOCKET socket = static_cast<SOCKET>(fd);
52 RawAddr raw;
53 socklen_t size = sizeof(raw);
54 if (getsockname(socket, &raw.addr, &size) == SOCKET_ERROR) {
55 return 0;
56 }
57 return SocketAddress::GetAddrPort(raw);
58 }
59
60
61 SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) {
62 SOCKET socket = static_cast<SOCKET>(fd);
63 RawAddr raw;
64 socklen_t size = sizeof(raw);
65 if (getpeername(socket, &raw.addr, &size)) {
66 return NULL;
67 }
68 *port = SocketAddress::GetAddrPort(raw);
69 // Clear the port before calling WSAAddressToString as WSAAddressToString
70 // includes the port in the formatted string.
71 SocketAddress::SetAddrPort(&raw, 0);
72 return new SocketAddress(&raw.addr);
73 }
74
75
76 intptr_t SynchronousSocket::Read(intptr_t fd,
77 void* buffer,
78 intptr_t num_bytes) {
79 SOCKET socket = static_cast<SOCKET>(fd);
80 return recv(socket, reinterpret_cast<char*>(buffer), num_bytes, 0);
81 }
82
83
84 intptr_t SynchronousSocket::Write(intptr_t fd,
85 const void* buffer,
86 intptr_t num_bytes) {
87 SOCKET socket = static_cast<SOCKET>(fd);
88 return send(socket, reinterpret_cast<const char*>(buffer), num_bytes, 0);
58 } 89 }
59 90
60 91
61 void SynchronousSocket::ShutdownRead(intptr_t fd) { 92 void SynchronousSocket::ShutdownRead(intptr_t fd) {
62 LOG_ERR("SynchronousSocket::ShutdownRead is unimplemented\n"); 93 SOCKET socket = static_cast<SOCKET>(fd);
63 UNIMPLEMENTED(); 94 shutdown(socket, SD_RECEIVE);
64 } 95 }
65 96
66 97
67 void SynchronousSocket::ShutdownWrite(intptr_t fd) { 98 void SynchronousSocket::ShutdownWrite(intptr_t fd) {
68 LOG_ERR("SynchronousSocket::ShutdownWrite is unimplemented\n"); 99 SOCKET socket = static_cast<SOCKET>(fd);
69 UNIMPLEMENTED(); 100 shutdown(socket, SD_SEND);
70 } 101 }
71 102
72 103
104 void SynchronousSocket::Close(intptr_t fd) {
105 SOCKET socket = static_cast<SOCKET>(fd);
106 closesocket(socket);
107 }
108
73 } // namespace bin 109 } // namespace bin
74 } // namespace dart 110 } // namespace dart
75 111
76 #endif // defined(HOST_OS_WINDOWS) 112 #endif // defined(HOST_OS_WINDOWS)
77 113
78 #endif // !defined(DART_IO_DISABLED) 114 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/sync_socket_macos.cc ('k') | tests/standalone/io/raw_synchronous_socket_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698