OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef DEBUGGER_BASE_DEBUG_SOCKET_H_ | |
6 #define DEBUGGER_BASE_DEBUG_SOCKET_H_ | |
7 | |
8 #ifdef _WIN32 | |
9 #include <winsock.h> | |
10 #else | |
11 #define SOCKET int | |
12 #define INVALID_SOCKET -1 | |
13 #endif | |
14 | |
15 #include <string> | |
16 #include "debugger/base/debug_blob.h" | |
17 | |
18 namespace debug { | |
19 class Socket; | |
20 | |
21 class SocketBase { | |
22 public: | |
23 static const int kSocketNoError = 0; | |
24 | |
25 SocketBase(); | |
26 virtual ~SocketBase(); | |
27 | |
28 /// Destroys socket. Further attempt to use it will return false. | |
29 /// Close() can be called many times, it has no effect on a closed | |
30 /// connection. You can safely reuse object after it has been closed. | |
31 void Close(); | |
32 | |
33 /// @return returns the error status for the last sockets operation that | |
34 /// failed for the calling thread | |
35 /// Look here for erroc codes description: | |
36 /// http://msdn.microsoft.com/en-us/library/ms740668%28v=vs.85%29.aspx | |
37 int GetLastError(); | |
38 | |
39 protected: | |
40 // DISALLOW_COPY_AND_ASSIGN | |
41 SocketBase(const SocketBase&); | |
42 SocketBase& operator = (const SocketBase&); | |
43 void ClearSavedLastError(); | |
44 | |
45 SOCKET sock_; | |
46 bool init_success_; | |
47 int saved_last_error_; | |
48 }; | |
49 | |
50 /// Implements a listening TCP/IP socket. | |
51 /// | |
52 /// Example: | |
53 /// ListeningSocket lsn; | |
54 /// lsn.Listen(4014); | |
55 /// Socket conn; | |
56 /// int wait_ms = 100; | |
57 /// if (lsn.Accept(wait_ms, &conn)) | |
58 /// DoSomethingWithNewConnection(conn); | |
59 /// | |
60 /// Note: add wsock32.lib to the list of linked libraries. | |
61 class ListeningSocket : public SocketBase { | |
62 public: | |
63 /// Starts listening for incoming connection. | |
64 /// @param[in] port port to listen on. | |
65 /// @return true if operation succeeds. | |
66 bool Listen(int port); | |
67 | |
68 /// Receives incoming connection, if any. | |
69 /// @param[in] wait_ms number of milliseconds to wait for connection | |
70 /// @param[out] new_connection pointer to |Socket| object that | |
71 /// receives connection | |
72 /// @return true if connection is received. | |
73 bool Accept(int wait_ms, Socket* new_connection); | |
74 }; | |
75 | |
76 /// Implements a raw socket interface. | |
77 /// | |
78 /// Example: | |
79 /// Socket conn; | |
80 /// if (conn.ConnectTo("172.29.20.175", 4016)) | |
81 /// DoSomethingWithNewConnection(conn); | |
82 class Socket : public SocketBase { | |
83 public: | |
84 /// Attempts to establish connection to |host| on |port|. | |
85 /// @param[in] host name of destinamtion host. | |
86 /// @param[in] port TCP/IP port number to connect to | |
87 /// @return true if connection is established. | |
88 bool ConnectTo(const std::string& host, int port); | |
89 | |
90 /// @return true if connection is alive. | |
91 bool IsConnected() const; | |
92 | |
93 /// Reads data from connection. | |
94 /// @param[out] buff buffer for incoming data | |
95 /// @param[in] buff_len size of the |buff| in bytes | |
96 /// @param[in] wait_ms number of milliseconds to wait | |
97 /// @return number of received bytes | |
98 /// Note: function blocks for not more then |wait_ms| milliseconds. | |
99 size_t Read(void* buff, size_t buff_len, int wait_ms); | |
100 | |
101 /// Writes (sends) data to the connection. | |
102 /// @param[in] buff buffer with data to send. | |
103 /// @param[in] buff_len size of the |buff| in bytes | |
104 /// @param[in] wait_ms number of milliseconds to wait | |
105 /// the number of bytes sent | |
106 /// @return number of bytes sent | |
107 /// Note: function blocks for not more then |wait_ms| milliseconds. | |
108 size_t Write(const void* buff, size_t buff_len, int wait_ms); | |
109 | |
110 /// Writes (sends) data to the connection. | |
111 /// Blocks until all data is gone or connection is closed. | |
112 /// @param[in] buff buffer with data to send. | |
113 /// @param[in] buff_len size of the |buff| in bytes | |
114 /// @return number of written bytes | |
115 size_t WriteAll(const void* buff, size_t buff_len); | |
116 | |
117 /// Writes (sends) data to the connection. | |
118 /// Blocks until all data is gone or connection is closed. | |
119 /// @param[in] blob data to send. | |
120 /// @return number of written bytes | |
121 size_t WriteAll(const Blob& blob); | |
122 | |
123 /// Reads data to the connection. | |
124 /// Blocks until |buff_len| bytes received or connection is closed. | |
125 /// @param[out] buff buffer for incoming data | |
126 /// @param[in] buff_len size of the |buff| in bytes | |
127 /// @return number of written bytes | |
128 size_t ReadAll(void* buff, size_t buff_len); | |
129 | |
130 private: | |
131 void AttachTo(SOCKET sock); | |
132 | |
133 friend class ListeningSocket; | |
134 }; | |
135 } // namespace debug | |
136 #endif // DEBUGGER_BASE_DEBUG_SOCKET_H_ | |
137 | |
138 | |
OLD | NEW |