Chromium Code Reviews| Index: runtime/bin/sync_socket_win.cc |
| diff --git a/runtime/bin/sync_socket_win.cc b/runtime/bin/sync_socket_win.cc |
| index 3d5338b5f6d061e9ff693a17af063830cf38963f..8a94964ab681c532bd2f6191f4fb1a70bf0fc711 100644 |
| --- a/runtime/bin/sync_socket_win.cc |
| +++ b/runtime/bin/sync_socket_win.cc |
| @@ -7,69 +7,113 @@ |
| #include "platform/globals.h" |
| #if defined(HOST_OS_WINDOWS) |
| +#include "bin/socket_base.h" |
| #include "bin/sync_socket.h" |
| -#include "bin/builtin.h" |
| -#include "bin/log.h" |
| -#include "bin/utils.h" |
| -#include "bin/utils_win.h" |
| - |
| -// #define SOCKET_LOG_ERROR 1 |
| - |
| -// define SOCKET_LOG_ERROR to get log messages only for errors. |
| -#if defined(SOCKET_LOG_ERROR) |
| -#define LOG_ERR(msg, ...) \ |
| - { \ |
| - int err = errno; \ |
| - Log::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \ |
| - ##__VA_ARGS__); \ |
| - errno = err; \ |
| - } |
| -#else |
| -#define LOG_ERR(msg, ...) |
| -#endif // defined(SOCKET_LOG_ERROR) |
| - |
| namespace dart { |
| namespace bin { |
| -SynchronousSocket::SynchronousSocket(intptr_t fd) { |
| - LOG_ERR("SynchronousSocket is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| +SynchronousSocket::SynchronousSocket(intptr_t fd) : fd_(fd) {} |
| + |
| + |
| +void SynchronousSocket::SetClosedFd() { |
| + fd_ = kClosedFd; |
| } |
|
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.
|
| bool SynchronousSocket::Initialize() { |
| - LOG_ERR("SynchronousSocket::Initialize is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| - return false; |
| + return SocketBase::Initialize(); |
| } |
| -void SynchronousSocket::SetClosedFd() { |
| - LOG_ERR("SynchronousSocket::SetClosedFd is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| +static intptr_t Create(const RawAddr& addr) { |
| + const intptr_t type = SOCK_STREAM; |
| + SOCKET s = WSASocket(addr.ss.ss_family, type, 0, NULL, 0, 0); |
| + return (s == INVALID_SOCKET) ? -1 : s; |
| +} |
| + |
| + |
| +static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + intptr_t result = |
| + connect(socket, &addr.addr, SocketAddress::GetAddrLength(addr)); |
| + return (result == SOCKET_ERROR) ? -1 : socket; |
| } |
| intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { |
| - LOG_ERR("SynchronousSocket::CreateConnect is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| - return -1; |
| + intptr_t fd = Create(addr); |
| + return (fd < 0) ? fd : Connect(fd, addr); |
| +} |
| + |
| + |
| +intptr_t SynchronousSocket::Available(intptr_t fd) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + DWORD available; |
| + intptr_t result = ioctlsocket(socket, FIONREAD, &available); |
| + return (result == SOCKET_ERROR) ? -1 : static_cast<intptr_t>(available); |
| +} |
| + |
| + |
| +intptr_t SynchronousSocket::GetPort(intptr_t fd) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + RawAddr raw; |
| + socklen_t size = sizeof(raw); |
| + if (getsockname(socket, &raw.addr, &size) == SOCKET_ERROR) { |
| + return 0; |
| + } |
| + return SocketAddress::GetAddrPort(raw); |
| +} |
| + |
| + |
| +SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + RawAddr raw; |
| + socklen_t size = sizeof(raw); |
| + if (getpeername(socket, &raw.addr, &size)) { |
| + return NULL; |
| + } |
| + *port = SocketAddress::GetAddrPort(raw); |
| + // Clear the port before calling WSAAddressToString as WSAAddressToString |
| + // includes the port in the formatted string. |
| + SocketAddress::SetAddrPort(&raw, 0); |
| + return new SocketAddress(&raw.addr); |
| +} |
| + |
| + |
| +intptr_t SynchronousSocket::Read(intptr_t fd, |
| + void* buffer, |
| + intptr_t num_bytes) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + 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
|
| +} |
| + |
| + |
| +intptr_t SynchronousSocket::Write(intptr_t fd, |
| + const void* buffer, |
| + intptr_t num_bytes) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + 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.
|
| } |
| void SynchronousSocket::ShutdownRead(intptr_t fd) { |
| - LOG_ERR("SynchronousSocket::ShutdownRead is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + shutdown(socket, SD_RECEIVE); |
| } |
| void SynchronousSocket::ShutdownWrite(intptr_t fd) { |
| - LOG_ERR("SynchronousSocket::ShutdownWrite is unimplemented\n"); |
| - UNIMPLEMENTED(); |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + shutdown(socket, SD_SEND); |
| } |
| +void SynchronousSocket::Close(intptr_t fd) { |
| + SOCKET socket = static_cast<SOCKET>(fd); |
| + closesocket(socket); |
| +} |
|
siva
2017/04/21 16:40:36
I suppose datagram sync sockets are not implemente
zra
2017/04/22 06:29:28
Right.
|
| + |
| } // namespace bin |
| } // namespace dart |