-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp_client.cc
163 lines (147 loc) · 4.05 KB
/
tcp_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*- c++ -*- */
/*
* Copyright 2018 Lucas Teske <lucas@teske.com.br>
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "tcp_client.h"
#include <cstdio>
#include <sstream>
#include <iostream>
#ifdef _WIN32
# include <winsock2.h>
# include <Ws2tcpip.h>
# include <atomic>
# ifdef _MSC_VER
# pragma comment(lib, "ws2_32.lib")
# endif
# ifndef MSG_WAITALL
# define MSG_WAITALL (1 << 3)
# endif
#else
# include <sys/socket.h>
# include <arpa/inet.h>
# include <sys/resource.h>
# include <sys/select.h>
# include <sys/ioctl.h>
# include <netdb.h>
# include <unistd.h>
# define ioctlsocket ioctl
#endif
#if defined(_WIN32) || defined(__APPLE__)
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#endif
#ifdef _WIN32
std::atomic_bool tcp_client::initialized(false);
std::atomic_uint tcp_client::sockCount(0);
void tcp_client::socket_initialize() {
if (!initialized) {
initialized = true;
sockCount = 1;
WSADATA wsa_data;
WSAStartup(MAKEWORD(1, 1), &wsa_data);
} else {
sockCount++;
}
}
#endif
tcp_client::tcp_client(std::string addr, int port)
{
this->port = port;
#ifdef _WIN32
socket_initialize();
#endif
hostent * record = gethostbyname(addr.c_str());
if (record == NULL) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"Cannot resolve: " + addr );
}
in_addr * address = (in_addr *)record->h_addr;
memset(&socketAddr, 0x00, sizeof(sockaddr_in));
socketAddr.sin_addr = *address;
}
void tcp_client::connect_conn() {
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
throw std::runtime_error("Socket Error Code " + std::to_string(errno));
}
socketAddr.sin_family = AF_INET;
socketAddr.sin_port = htons(port);
int x = connect(s, (struct sockaddr *) &socketAddr, sizeof(socketAddr));
if (x < 0) {
throw std::runtime_error("Socket Error Code " + std::to_string(errno));
}
}
void tcp_client::close_conn() {
if (s > 0) {
int status = 0;
#ifdef _WIN32
status = shutdown(s, SD_BOTH);
if (status == 0) {
status = closesocket(s);
}
#else
status = shutdown(s, 2);
if (status == 0) {
status = close(s);
}
#endif
}
}
tcp_client::~tcp_client() {
#ifdef _WIN32
sockCount--;
if (!sockCount) {
WSACleanup();
}
#endif
}
void tcp_client::receive_data(char *data, int length) {
long n = recv(s, data, length, MSG_WAITALL);
if (n == 0) {
throw std::runtime_error("Client Disconnected");
} else if (n != length) {
throw std::runtime_error("Socket Error Code " + std::to_string(errno));
}
}
void tcp_client::send_data(char * data, int length) {
int n = send(s, data, length, MSG_NOSIGNAL);
if (n == 0) {
throw std::runtime_error("Client Disconnected");
} else if (n != length) {
throw std::runtime_error("Socket Error Code " + std::to_string(errno));
}
}
uint64_t tcp_client::available_data() {
if (s < 0) {
return 0;
}
unsigned long bytesAvailable = 0;
int ret = ioctlsocket(s, FIONREAD, &bytesAvailable);
switch (ret) {
case EINVAL:
case EFAULT:
case ENOTTY:
throw std::runtime_error("Socket Error Code " + std::to_string(ret));
break;
case EBADF:
throw std::runtime_error("Client Disconnected");
break;
}
return bytesAvailable;
}