Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
| 6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 10 | 10 |
| 11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
| 13 // Declare the OS-specific types ahead of defining the generic class. | 13 // Declare the OS-specific types ahead of defining the generic class. |
| 14 #if defined(TARGET_OS_ANDROID) | 14 #if defined(TARGET_OS_ANDROID) |
| 15 #include "bin/socket_android.h" | 15 #include "bin/socket_android.h" |
| 16 #elif defined(TARGET_OS_LINUX) | 16 #elif defined(TARGET_OS_LINUX) |
| 17 #include "bin/socket_linux.h" | 17 #include "bin/socket_linux.h" |
| 18 #elif defined(TARGET_OS_MACOS) | 18 #elif defined(TARGET_OS_MACOS) |
| 19 #include "bin/socket_macos.h" | 19 #include "bin/socket_macos.h" |
| 20 #elif defined(TARGET_OS_WINDOWS) | 20 #elif defined(TARGET_OS_WINDOWS) |
| 21 #include "bin/socket_win.h" | 21 #include "bin/socket_win.h" |
| 22 #else | 22 #else |
| 23 #error Unknown target os. | 23 #error Unknown target os. |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #define SOCKADDR_STORAGE_LENGTH(sa) \ | |
| 27 sa.ss_family == AF_INET6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN | |
| 28 | |
| 29 class SocketAddress { | |
| 30 public: | |
| 31 explicit SocketAddress(struct addrinfo* addrinfo); | |
| 32 | |
| 33 ~SocketAddress() { | |
| 34 delete[] as_string_; | |
| 35 } | |
| 36 | |
| 37 int GetType() { | |
| 38 if (addr_.ss_family == AF_INET6) return 1; | |
| 39 return 0; | |
| 40 } | |
| 41 | |
| 42 const char* AsString() const { return as_string_; } | |
| 43 const sockaddr_storage& GetAddr() const { return addr_; } | |
| 44 | |
| 45 private: | |
| 46 const char* as_string_; | |
|
Søren Gjesse
2013/04/18 12:59:24
How about static buffer for as_string as well?
ch
Anders Johnsen
2013/04/19 09:45:36
Done.
| |
| 47 sockaddr_storage addr_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | |
| 50 }; | |
| 51 | |
| 52 class SocketAddresses { | |
| 53 public: | |
| 54 explicit SocketAddresses(intptr_t count) | |
| 55 : count_(count), | |
| 56 addresses_(new SocketAddress*[count_]) {} | |
| 57 | |
| 58 ~SocketAddresses() { | |
| 59 for (intptr_t i = 0; i < count_; i++) { | |
| 60 delete addresses_[i]; | |
| 61 } | |
| 62 delete[] addresses_; | |
| 63 } | |
| 64 | |
| 65 intptr_t GetCount() const { return count_; } | |
| 66 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | |
| 67 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | |
| 68 | |
| 69 private: | |
| 70 const intptr_t count_; | |
| 71 SocketAddress** addresses_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | |
| 74 }; | |
| 26 | 75 |
| 27 class Socket { | 76 class Socket { |
| 28 public: | 77 public: |
| 29 enum SocketRequest { | 78 enum SocketRequest { |
| 30 kLookupRequest = 0, | 79 kLookupRequest = 0, |
| 31 }; | 80 }; |
| 32 | 81 |
| 33 static bool Initialize(); | 82 static bool Initialize(); |
| 34 static intptr_t Available(intptr_t fd); | 83 static intptr_t Available(intptr_t fd); |
| 35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 84 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
| 36 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 85 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
| 37 static intptr_t CreateConnect(const char* host, const intptr_t port); | 86 static intptr_t CreateConnect(sockaddr_storage addr, |
| 87 const intptr_t port); | |
| 38 static intptr_t GetPort(intptr_t fd); | 88 static intptr_t GetPort(intptr_t fd); |
| 39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 89 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
| 40 static void GetError(intptr_t fd, OSError* os_error); | 90 static void GetError(intptr_t fd, OSError* os_error); |
| 41 static int GetType(intptr_t fd); | 91 static int GetType(intptr_t fd); |
| 42 static intptr_t GetStdioHandle(int num); | 92 static intptr_t GetStdioHandle(int num); |
| 43 static void Close(intptr_t fd); | 93 static void Close(intptr_t fd); |
| 44 static bool SetNonBlocking(intptr_t fd); | 94 static bool SetNonBlocking(intptr_t fd); |
| 45 static bool SetBlocking(intptr_t fd); | 95 static bool SetBlocking(intptr_t fd); |
| 46 static bool SetNoDelay(intptr_t fd, bool enabled); | 96 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 47 | 97 |
| 48 // Perform a IPv4 hostname lookup. Returns the hostname string in | 98 // Perform a hostname lookup. Returns the SocketAddresses. |
| 49 // IPv4 dotted-decimal format. | 99 static SocketAddresses* LookupAddress(char* host, OSError** os_error); |
| 50 static const char* LookupIPv4Address(char* host, OSError** os_error); | |
| 51 | 100 |
| 52 static Dart_Port GetServicePort(); | 101 static Dart_Port GetServicePort(); |
| 53 | 102 |
| 54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 103 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 104 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
| 56 | 105 |
| 57 private: | 106 private: |
| 58 static dart::Mutex mutex_; | 107 static dart::Mutex mutex_; |
| 59 static int service_ports_size_; | 108 static int service_ports_size_; |
| 60 static Dart_Port* service_ports_; | 109 static Dart_Port* service_ports_; |
| 61 static int service_ports_index_; | 110 static int service_ports_index_; |
| 62 | 111 |
| 63 DISALLOW_ALLOCATION(); | 112 DISALLOW_ALLOCATION(); |
| 64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 113 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
| 65 }; | 114 }; |
| 66 | 115 |
| 67 | 116 |
| 68 class ServerSocket { | 117 class ServerSocket { |
| 69 public: | 118 public: |
| 70 static const intptr_t kTemporaryFailure = -2; | 119 static const intptr_t kTemporaryFailure = -2; |
| 71 | 120 |
| 72 static intptr_t Accept(intptr_t fd); | 121 static intptr_t Accept(intptr_t fd); |
| 73 | 122 |
| 74 // Returns a positive integer if the call is successful. In case of failure | 123 // Returns a positive integer if the call is successful. In case of failure |
| 75 // it returns: | 124 // it returns: |
| 76 // | 125 // |
| 77 // -1: system error (errno set) | 126 // -1: system error (errno set) |
| 78 // -5: invalid bindAddress | 127 // -5: invalid bindAddress |
| 79 static intptr_t CreateBindListen(const char* bindAddress, | 128 static intptr_t CreateBindListen(sockaddr_storage addr, |
| 80 intptr_t port, | 129 intptr_t port, |
| 81 intptr_t backlog); | 130 intptr_t backlog); |
| 82 | 131 |
| 83 DISALLOW_ALLOCATION(); | 132 DISALLOW_ALLOCATION(); |
| 84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 133 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 85 }; | 134 }; |
| 86 | 135 |
| 87 #endif // BIN_SOCKET_H_ | 136 #endif // BIN_SOCKET_H_ |
| OLD | NEW |