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 class SocketAddress { | |
27 public: | |
28 enum Type { | |
29 IPv4 = 0, | |
30 IPv6 = 1, | |
31 }; | |
32 | |
33 SocketAddress(Type type, | |
34 const char* as_string, | |
35 uint8_t* data, | |
36 intptr_t length) | |
37 : type(type), as_string(as_string), data(data), length(length) {} | |
38 | |
39 ~SocketAddress() { | |
40 delete[] as_string; | |
41 delete[] data; | |
42 } | |
43 | |
44 Type type; | |
Søren Gjesse
2013/04/18 09:07:40
Make these mempers private with _ postfix. Then ad
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
45 const char* as_string; | |
46 uint8_t* data; | |
Søren Gjesse
2013/04/18 09:07:40
can't this be
sockaddr_storage address
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
47 const intptr_t length; | |
Søren Gjesse
2013/04/18 09:07:40
Please add:
private:
DISALLOW_COPY_AND_ASSIGN(
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
48 }; | |
49 | |
50 class SocketAddresses { | |
51 public: | |
52 explicit SocketAddresses(intptr_t count) | |
53 : count(count), | |
54 addresses(new SocketAddress*[count]) {} | |
55 | |
56 ~SocketAddresses() { | |
57 for (intptr_t i = 0; i < count; i++) { | |
58 delete addresses[i]; | |
59 } | |
60 delete[] addresses; | |
61 } | |
62 | |
Søren Gjesse
2013/04/18 09:07:40
As above.
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
63 const intptr_t count; | |
64 SocketAddress** addresses; | |
65 }; | |
26 | 66 |
27 class Socket { | 67 class Socket { |
28 public: | 68 public: |
29 enum SocketRequest { | 69 enum SocketRequest { |
30 kLookupRequest = 0, | 70 kLookupRequest = 0, |
31 }; | 71 }; |
32 | 72 |
33 static bool Initialize(); | 73 static bool Initialize(); |
34 static intptr_t Available(intptr_t fd); | 74 static intptr_t Available(intptr_t fd); |
35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 75 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); | 76 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); | 77 static intptr_t CreateConnect(SocketAddress::Type type, |
78 uint8_t* host_data, | |
79 intptr_t host_length, | |
80 const intptr_t port); | |
38 static intptr_t GetPort(intptr_t fd); | 81 static intptr_t GetPort(intptr_t fd); |
39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 82 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
40 static void GetError(intptr_t fd, OSError* os_error); | 83 static void GetError(intptr_t fd, OSError* os_error); |
41 static int GetType(intptr_t fd); | 84 static int GetType(intptr_t fd); |
42 static intptr_t GetStdioHandle(int num); | 85 static intptr_t GetStdioHandle(int num); |
43 static void Close(intptr_t fd); | 86 static void Close(intptr_t fd); |
44 static bool SetNonBlocking(intptr_t fd); | 87 static bool SetNonBlocking(intptr_t fd); |
45 static bool SetBlocking(intptr_t fd); | 88 static bool SetBlocking(intptr_t fd); |
46 static bool SetNoDelay(intptr_t fd, bool enabled); | 89 static bool SetNoDelay(intptr_t fd, bool enabled); |
47 | 90 |
48 // Perform a IPv4 hostname lookup. Returns the hostname string in | 91 // Perform a hostname lookup. Returns the SocketAddresses. |
49 // IPv4 dotted-decimal format. | 92 static SocketAddresses* LookupAddress(char* host, OSError** os_error); |
50 static const char* LookupIPv4Address(char* host, OSError** os_error); | |
51 | 93 |
52 static Dart_Port GetServicePort(); | 94 static Dart_Port GetServicePort(); |
53 | 95 |
54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 96 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 97 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
56 | 98 |
57 private: | 99 private: |
58 static dart::Mutex mutex_; | 100 static dart::Mutex mutex_; |
59 static int service_ports_size_; | 101 static int service_ports_size_; |
60 static Dart_Port* service_ports_; | 102 static Dart_Port* service_ports_; |
61 static int service_ports_index_; | 103 static int service_ports_index_; |
62 | 104 |
63 DISALLOW_ALLOCATION(); | 105 DISALLOW_ALLOCATION(); |
64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 106 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
65 }; | 107 }; |
66 | 108 |
67 | 109 |
68 class ServerSocket { | 110 class ServerSocket { |
69 public: | 111 public: |
70 static const intptr_t kTemporaryFailure = -2; | 112 static const intptr_t kTemporaryFailure = -2; |
71 | 113 |
72 static intptr_t Accept(intptr_t fd); | 114 static intptr_t Accept(intptr_t fd); |
73 | 115 |
74 // Returns a positive integer if the call is successful. In case of failure | 116 // Returns a positive integer if the call is successful. In case of failure |
75 // it returns: | 117 // it returns: |
76 // | 118 // |
77 // -1: system error (errno set) | 119 // -1: system error (errno set) |
78 // -5: invalid bindAddress | 120 // -5: invalid bindAddress |
79 static intptr_t CreateBindListen(const char* bindAddress, | 121 static intptr_t CreateBindListen(SocketAddress::Type type, |
122 uint8_t* host_data, | |
123 intptr_t host_length, | |
80 intptr_t port, | 124 intptr_t port, |
81 intptr_t backlog); | 125 intptr_t backlog); |
82 | 126 |
83 DISALLOW_ALLOCATION(); | 127 DISALLOW_ALLOCATION(); |
84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 128 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
85 }; | 129 }; |
86 | 130 |
87 #endif // BIN_SOCKET_H_ | 131 #endif // BIN_SOCKET_H_ |
OLD | NEW |