tcp_socket.h 3.11 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2019 by Contributors
 * \file tcp_socket.h
 * \brief TCP socket for DGL distributed training.
 */
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
29
30
31
32
33
34
 * It supports only TCP connections.
 */
class TCPSocket {
 public:
  /*!
   * \brief TCPSocket constructor
   */
  TCPSocket();

  /*!
   * \brief TCPSocket deconstructor
35
   */
36
37
38
39
40
41
42
43
  ~TCPSocket();

  /*!
   * \brief Connect to a given server address
   * \param ip ip address
   * \param port end port
   * \return true for success and false for failure
   */
44
  bool Connect(const char* ip, int port);
45
46
47
48
49
50
51

  /*!
   * \brief Bind on the given IP and PORT
   * \param ip ip address
   * \param port end port
   * \return true for success and false for failure
   */
52
  bool Bind(const char* ip, int port);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

  /*!
   * \brief listen for remote connection
   * \param max_connection maximal connection
   * \return true for success and false for failure
   */
  bool Listen(int max_connection);

  /*!
   * \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
   */
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
   * \param flag true for nonblocking, false for blocking
74
75
   * \return true for success and false for failure
   */
76
  bool SetNonBlocking(bool flag);
77
78
79

  /*!
   * \brief Set timeout for socket
80
   * \param timeout seconds timeout
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
   */
  void SetTimeout(int timeout);

  /*!
   * \brief Shut down one or both halves of the connection.
   * \param ways ways for shutdown
   * 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.
   * \return true for success and false for failure
   */
  bool ShutDown(int ways);

  /*!
   * \brief close socket.
   */
  void Close();

  /*!
   * \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
123
  int Socket() const;

 private:
  /*!
   * \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_