tcp_socket.h 3.11 KB
Newer Older
1
2
/*!
 *  Copyright (c) 2019 by Contributors
3
4
 * @file tcp_socket.h
 * @brief TCP socket for DGL distributed training.
5
 */
6
7
#ifndef DGL_RPC_NETWORK_TCP_SOCKET_H_
#define DGL_RPC_NETWORK_TCP_SOCKET_H_
8
9
10
11
12
13

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")
14
#else  // !_WIN32
15
16
17
18
19
20
21
22
#include <sys/socket.h>
#endif  // _WIN32
#include <string>

namespace dgl {
namespace network {

/*!
23
 * @brief TCPSocket is a simple wrapper around a socket.
24
25
26
27
28
 * It supports only TCP connections.
 */
class TCPSocket {
 public:
  /*!
29
   * @brief TCPSocket constructor
30
31
32
33
   */
  TCPSocket();

  /*!
34
   * @brief TCPSocket deconstructor
35
   */
36
37
38
  ~TCPSocket();

  /*!
39
40
41
42
   * @brief Connect to a given server address
   * @param ip ip address
   * @param port end port
   * @return true for success and false for failure
43
   */
44
  bool Connect(const char* ip, int port);
45
46

  /*!
47
48
49
50
   * @brief Bind on the given IP and PORT
   * @param ip ip address
   * @param port end port
   * @return true for success and false for failure
51
   */
52
  bool Bind(const char* ip, int port);
53
54

  /*!
55
56
57
   * @brief listen for remote connection
   * @param max_connection maximal connection
   * @return true for success and false for failure
58
59
60
61
   */
  bool Listen(int max_connection);

  /*!
62
63
64
65
66
   * @brief wait doe a new connection
   * @param socket new SOCKET will be stored to socket
   * @param ip_client new IP will be stored to ip_client
   * @param port_client new PORT will be stored to port_client
   * @return true for success and false for failure
67
   */
68
  bool Accept(TCPSocket* socket, std::string* ip_client, int* port_client);
69
70

  /*!
71
   * @brief SetNonBlocking() is needed refering to this example of epoll:
72
   * http://www.kernel.org/doc/man-pages/online/pages/man4/epoll.4.html
73
74
   * @param flag true for nonblocking, false for blocking
   * @return true for success and false for failure
75
   */
76
  bool SetNonBlocking(bool flag);
77
78

  /*!
79
80
   * @brief Set timeout for socket
   * @param timeout seconds timeout
81
82
83
84
   */
  void SetTimeout(int timeout);

  /*!
85
86
   * @brief Shut down one or both halves of the connection.
   * @param ways ways for shutdown
87
88
89
   * If ways is SHUT_RD, further receives are disallowed.
   * If ways is SHUT_WR, further sends are disallowed.
   * If ways is SHUT_RDWR, further sends and receives are disallowed.
90
   * @return true for success and false for failure
91
92
93
94
   */
  bool ShutDown(int ways);

  /*!
95
   * @brief close socket.
96
97
98
99
   */
  void Close();

  /*!
100
101
102
103
   * @brief Send data.
   * @param data data for sending
   * @param len_data length of data
   * @return return number of bytes sent if OK, -1 on error
104
105
   */
  int64_t Send(const char* data, int64_t len_data);
106
107

  /*!
108
109
110
111
   * @brief Receive data.
   * @param buffer buffer for receving
   * @param size_buffer size of buffer
   * @return return number of bytes received if OK, -1 on error
112
113
   */
  int64_t Receive(char* buffer, int64_t size_buffer);
114
115

  /*!
116
117
   * @brief Get socket's file descriptor
   * @return socket's file descriptor
118
   */
119
120
121
122
  int Socket() const;

 private:
  /*!
123
   * @brief socket's file descriptor
124
   */
125
126
127
128
129
130
  int socket_;
};

}  // namespace network
}  // namespace dgl

131
#endif  // DGL_RPC_NETWORK_TCP_SOCKET_H_