2929#ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
3030#define UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
3131
32- #include < netdb.h>
33- #include < sys/socket.h>
34- #include < sys/types.h>
35- #include < unistd.h>
36-
3732#include < atomic>
3833#include < chrono>
3934#include < functional>
4035#include < thread>
4136
37+ #include " ur_client_library/comm/socket_t.h"
38+
4239namespace urcl
4340{
4441namespace comm
@@ -80,7 +77,7 @@ class TCPServer
8077 * \param func Function handling the event information. The file descriptor created by the
8178 * connection event will be passed to the function.
8279 */
83- void setConnectCallback (std::function<void (const int )> func)
80+ void setConnectCallback (std::function<void (const socket_t )> func)
8481 {
8582 new_connection_callback_ = func;
8683 }
@@ -91,7 +88,7 @@ class TCPServer
9188 * \param func Function handling the event information. The file descriptor created by the
9289 * connection event will be passed to the function.
9390 */
94- void setDisconnectCallback (std::function<void (const int )> func)
91+ void setDisconnectCallback (std::function<void (const socket_t )> func)
9592 {
9693 disconnect_callback_ = func;
9794 }
@@ -102,7 +99,7 @@ class TCPServer
10299 * \param func Function handling the event information. The file client's file_descriptor will be
103100 * passed to the function as well as the actual message received from the client.
104101 */
105- void setMessageCallback (std::function<void (const int , char *, int )> func)
102+ void setMessageCallback (std::function<void (const socket_t , char *, int )> func)
106103 {
107104 message_callback_ = func;
108105 }
@@ -133,7 +130,7 @@ class TCPServer
133130 *
134131 * \returns True on success, false otherwise
135132 */
136- bool write (const int fd, const uint8_t * buf, const size_t buf_len, size_t & written);
133+ bool write (const socket_t fd, const uint8_t * buf, const size_t buf_len, size_t & written);
137134
138135 /* !
139136 * \brief Get the maximum number of clients allowed to connect to this server
@@ -164,10 +161,10 @@ class TCPServer
164161 // ! Handles connection events
165162 void handleConnect ();
166163
167- void handleDisconnect (const int fd);
164+ void handleDisconnect (const socket_t fd);
168165
169166 // ! read data from socket
170- void readData (const int fd);
167+ void readData (const socket_t fd);
171168
172169 // ! Event handler. Blocks until activity on any client or connection attempt
173170 void spin ();
@@ -178,25 +175,22 @@ class TCPServer
178175 std::atomic<bool > keep_running_;
179176 std::thread worker_thread_;
180177
181- std::atomic<int > listen_fd_;
178+ std::atomic<socket_t > listen_fd_;
182179 int port_;
183180
184- int maxfd_;
181+ socket_t maxfd_;
185182 fd_set masterfds_;
186183 fd_set tempfds_;
187184
188185 uint32_t max_clients_allowed_;
189- std::vector<int > client_fds_;
190-
191- // Pipe for the self-pipe trick (https://cr.yp.to/docs/selfpipe.html)
192- int self_pipe_[2 ];
186+ std::vector<socket_t > client_fds_;
193187
194188 static const int INPUT_BUFFER_SIZE = 100 ;
195189 char input_buffer_[INPUT_BUFFER_SIZE];
196190
197- std::function<void (const int )> new_connection_callback_;
198- std::function<void (const int )> disconnect_callback_;
199- std::function<void (const int , char * buffer, int nbytesrecv)> message_callback_;
191+ std::function<void (const socket_t )> new_connection_callback_;
192+ std::function<void (const socket_t )> disconnect_callback_;
193+ std::function<void (const socket_t , char * buffer, int nbytesrecv)> message_callback_;
200194};
201195
202196} // namespace comm
0 commit comments