-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcss3dsocket.cpp
More file actions
163 lines (127 loc) · 3.69 KB
/
Copy pathrcss3dsocket.cpp
File metadata and controls
163 lines (127 loc) · 3.69 KB
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
#include "rcss3dsocket.h"
#include <rcssnet/exception.hpp>
#include <string.h>
#include <errno.h>
//#include <sys/socket.h>
#include <netinet/in.h>
//#include <arpa/inet.h>
using namespace rcss::net;
rcss3dSocket::rcss3dSocket(){
// std::cout << "make socket class(not connected)" << std::endl;
}
rcss3dSocket::rcss3dSocket(int port_arg, std::string host_arg): port(port_arg), host(host_arg){
std::cout << "connect to TCP " << host << ":" << port << std::endl;
try{
Addr local(INADDR_ANY, INADDR_ANY);
soc.bind(local);
}catch(BindErr error){
std::cerr << "failed to bind socket with '" << error.what() << "'" << std::endl;
soc.close();
}
try{
Addr server(port, host);
soc.connect(server);
}catch(ConnectErr error){
std::cerr << "connection failed with: '" << error.what() <<"'" << std::endl;
soc.close();
}
}
rcss3dSocket::~rcss3dSocket(){
soc.close();
std::cout << "closed connection to " << host << ":" << port << std::endl;
}
void rcss3dSocket::PutMessage(const std::string &msg){
if(msg.empty()){
return;
}
//prefix the message with it's payload length
unsigned int len = htonl(msg.size());
std::string prefix((const char*)&len, sizeof(unsigned int));
std::string str = prefix + msg;
// std::cout << "PutMessage\n" << str << std::endl;
soc.send(str.data(), str.size());
}
bool rcss3dSocket::GetMessage(std::string &msg){
static char buffer[16*1024];
unsigned int bytesRead = 0;
while(bytesRead < sizeof(unsigned int)){
SelectInput();
int readResult = soc.recv(buffer+bytesRead, sizeof(unsigned int)-bytesRead);
if(readResult < 0)
continue;
bytesRead += readResult;
}
unsigned int msgLen = ntohl(*(unsigned int*)buffer);
if(sizeof(unsigned int)+msgLen > sizeof(buffer)){
std::cout << buffer << std::endl;
std::cerr << "message too long: abortion" << std::endl;
abort();
}
unsigned int msgRead = bytesRead - sizeof(unsigned int);
char *offset = buffer + bytesRead;
while(msgRead < msgLen){
if(! SelectInput()){
return false;
}
int readLen = sizeof(buffer) - msgRead;
if(readLen > msgLen - msgRead)
readLen = msgLen - msgRead;
int readResult = soc.recv(offset, readLen);
if(readResult < 0)
continue;
msgRead += readResult;
offset += readResult;
}
(*offset) = 0;
msg = std::string(buffer+sizeof(unsigned int));
// std::cout << "GetMessage" << std::endl << msg << std::endl;
return true;
}
void rcss3dSocket::Done(){
soc.close();
std::cout << "closed connection to " << host << ":" << port << std::endl;
}
bool rcss3dSocket::Connect(int port_arg, std::string host_arg){
host = host_arg;
port = port_arg;
std::cout << "connect to TCP " << host << ":" << port << std::endl;
try{
Addr local(INADDR_ANY, INADDR_ANY);
soc.bind(local);
}catch(BindErr error){
std::cerr << "failed to bind socket with '" << error.what() << "'" << std::endl;
soc.close();
return false;
}
try{
Addr server(port, host);
soc.connect(server);
}catch(ConnectErr error){
std::cerr << "connection failed with: '" << error.what() << "'" << std::endl;
soc.close();
return false;
}
return true;
}
bool rcss3dSocket::SelectInput(){
fd_set readfds;
struct timeval tv = {60, 0};
FD_ZERO(&readfds);
FD_SET(soc.getFD(), &readfds);
while(1){
switch(select(soc.getFD()+1, &readfds, 0, 0, &tv)){
case 1:
return 1;
case 0:
std::cerr << "(SelectInput) select failed " << strerror(errno) << std::endl;
abort();
return 0;
default:
if(errno == EINTR)
continue;
std::cerr << "(SelectInput) select failed " << strerror(errno) << std::endl;
abort();
return 0;
}
}
}