Skip to content

Commit db078bc

Browse files
committed
linux support
1 parent 81ab0c3 commit db078bc

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

demo/tcp_server.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@ int main()
2626
sockets::TCPServer server;
2727
server.set_socket("127.0.0.1", 10000);
2828
server.set_keepalive(1, 1, 1);
29-
server.set_timeout(50);
29+
server.set_timeout(500);
3030
server.socket_bind();
3131

3232
int idx = 0;
3333
while (true)
3434
{
35-
std::this_thread::sleep_for(500ms);
35+
std::this_thread::sleep_for(100ms);
3636
++idx;
3737
std::cout << "try: " << idx << "; " << std::endl;
3838

3939
// Receive message
40-
int res =
41-
server.receive(reinterpret_cast<char *>(&rx_msg), MSG_SIZE);
40+
int res = server.receive(reinterpret_cast<char *>(&rx_msg), MSG_SIZE);
4241

4342
// Processing the result
4443
switch (res)

demo/udp_server.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ int main()
2525
// Server initialization
2626
sockets::UDPServer server;
2727
server.set_socket("127.0.0.1", 10000);
28-
server.set_timeout(1000);
28+
server.set_timeout(500);
2929
server.socket_bind();
3030

3131
int idx = 0;
3232
while (true)
3333
{
34-
std::this_thread::sleep_for(500ms);
34+
//std::this_thread::sleep_for(50ms);
3535
++idx;
3636
std::cout << "try: " << idx << "; " << std::endl;
3737

3838
// Receive message
39-
int res =
40-
server.receive(reinterpret_cast<char *>(&rx_msg), MSG_SIZE);
39+
int res = server.receive(reinterpret_cast<char *>(&rx_msg), MSG_SIZE);
4140

4241
// Processing the result
4342
switch (res)

include/simple_socket/simple_socket.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <winsock2.h>
1212
#else
1313
#include <arpa/inet.h>
14+
#include <fcntl.h>
1415
#include <netinet/tcp.h>
1516
#include <sys/socket.h>
1617
#include <unistd.h>
@@ -51,20 +52,20 @@ class Socket
5152
explicit Socket(SocketType socket_type);
5253
~Socket();
5354
int set_socket(const std::string &ip_address, uint16_t port);
54-
void set_timeout(const timeout_ms timeout = 1000);
55+
void set_timeout(const timeout_ms timeout = 50);
5556

5657
protected:
5758
void set_port(uint16_t port);
5859
int set_address(const std::string &ip_address);
5960
int socket_init();
6061
void socket_close();
61-
int wait_for_receive(const int sockfd, const timeout_ms timeout = 100);
62+
int wait_for_receive(const int sockfd);
6263

6364
protected:
6465
int sockfd_;
6566
sockaddr_in address_;
6667
SocketType socket_type_;
67-
timeout_ms timeout_ = 100;
68+
timeout_ms timeout_ = 50;
6869
};
6970

7071
class UDPClient : public Socket

src/simple_socket.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ int Socket::set_socket(const std::string &ip_address, uint16_t port)
4848

4949
/**
5050
* @brief Sets timeouts for the connect and receive functions
51-
*
52-
* @param timeout timeout in ms. Default 1000 ms. 0 for infinite wait.
51+
*
52+
* @param timeout timeout in ms. Default 50 ms. 0 for infinite wait.
5353
*/
5454
void Socket::set_timeout(const timeout_ms timeout)
5555
{
@@ -84,8 +84,8 @@ int Socket::socket_init()
8484
}
8585

8686
int optval = 1;
87-
if (setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof(optval)) <
88-
0)
87+
if (setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
88+
sizeof(optval)) < 0)
8989
{
9090
return -1;
9191
}
@@ -103,19 +103,20 @@ void Socket::socket_close()
103103
#endif
104104
}
105105

106-
int Socket::wait_for_receive(const int sockfd, const timeout_ms timeout)
106+
int Socket::wait_for_receive(const int sockfd)
107107
{
108108
fd_set set;
109109
FD_ZERO(&set);
110110
FD_SET(sockfd, &set);
111111

112-
if (timeout == 0) {
112+
if (timeout_ == 0)
113+
{
113114
return select(0, &set, NULL, NULL, NULL);
114115
}
115116

116117
timeval tv;
117-
tv.tv_sec = timeout / 1000;
118-
tv.tv_usec = (timeout % 1000) * 1000;
118+
tv.tv_sec = timeout_ / 1000;
119+
tv.tv_usec = (timeout_ % 1000) * 1000;
119120

120121
return select(0, &set, NULL, NULL, &tv);
121122
}
@@ -143,6 +144,8 @@ UDPServer::UDPServer(const std::string &ip_address, uint16_t port)
143144
#ifdef _WIN32
144145
u_long on = 1;
145146
ioctlsocket(sockfd_, FIONBIO, &on);
147+
#else
148+
fcntl(sockfd_, F_SETFL, O_NONBLOCK);
146149
#endif
147150
}
148151

@@ -156,10 +159,11 @@ int UDPServer::socket_bind()
156159

157160
int UDPServer::receive(char *recv_buf, const int recv_buf_size)
158161
{
159-
if (wait_for_receive(sockfd_, timeout_) <= 0) {
162+
if (wait_for_receive(sockfd_) < 0)
163+
{
160164
return static_cast<int>(SocketErrors::RECEIVE_ERROR);
161165
}
162-
166+
163167
return recvfrom(sockfd_, recv_buf, recv_buf_size, 0,
164168
reinterpret_cast<sockaddr *>(&client_), &client_size_);
165169
}
@@ -178,7 +182,8 @@ int TCPSocket::receive(char *recv_buf, const int recv_buf_size)
178182
return static_cast<int>(SocketErrors::CONNECT_ERROR);
179183
}
180184

181-
if (wait_for_receive(dest_sock_, timeout_) <= 0) {
185+
if (wait_for_receive(dest_sock_) < 0)
186+
{
182187
return static_cast<int>(SocketErrors::RECEIVE_ERROR);
183188
}
184189

@@ -250,6 +255,8 @@ TCPServer::TCPServer(const std::string &ip_address, uint16_t port)
250255
#ifdef _WIN32
251256
u_long on = 1;
252257
ioctlsocket(sockfd_, FIONBIO, &on);
258+
#else
259+
fcntl(sockfd_, F_SETFL, O_NONBLOCK);
253260
#endif
254261
}
255262

@@ -274,7 +281,8 @@ int TCPServer::make_connection()
274281
{
275282
close_connection();
276283

277-
if (wait_for_receive(sockfd_, timeout_) <= 0) {
284+
if (wait_for_receive(sockfd_) < 0)
285+
{
278286
return static_cast<int>(SocketErrors::CONNECT_ERROR);
279287
}
280288

0 commit comments

Comments
 (0)