-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_connection.cpp
executable file
·276 lines (227 loc) · 5.76 KB
/
socket_connection.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
// OS-dependent header files for socket I/O.
#ifdef unix
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#else
#ifdef WIN32
#include <io.h>
#endif
#endif
#include "globals.h"
#include "socket_connection.h"
#include "utility.h"
#include "World.h"
// STL includes
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
using namespace std;
/********************************************************************/
SocketConnection::SocketConnection()
{
FileDescriptor = -1;
OutputBuffer = InputBuffer = InputLine = "";
Port = 0;
Host = "";
OutputLength = 0;
RepeatTimes = 0;
}
SocketConnection::SocketConnection(int descriptor)
{
FileDescriptor = descriptor;
OutputBuffer = InputBuffer = "";
Port = 0;
Host = "";
OutputLength = 0;
}
SocketConnection::~SocketConnection()
{
Disconnect();
}
/*
___ __ _
/ _ \ ___ / / __ __ ___ _ ___ _ (_)___ ___ _
/ // // -_)/ _ \/ // // _ `// _ `// // _ \/ _ `/
/____/ \__//_.__/\_,_/ \_, / \_, //_//_//_/\_, /
/___/ /___/ /___/
*/
void SocketConnection::Initialize()
{
}
bool SocketConnection::InOutSet()
{
/*
* Short-circuit if nothing to write.
*/
if ( OutputLength == 0 )
return true;
int bytesWritten = 0;
while ( OutputLength > 0 )
{
// Write as much of buffer as possible.
#ifdef unix
bytesWritten = write( FileDescriptor, OutputBuffer.c_str(), /*MIN(OutputLength, 512)*/ OutputLength );
#else
#ifdef WIN32
bytesWritten = send( FileDescriptor, OutputBuffer.c_str(), /*MIN(OutputLength, 512)*/ OutputLength, 0 );
#endif
#endif
if (bytesWritten < 0)
{
if (errno == EWOULDBLOCK || errno == EAGAIN)
break; // this is normal, and can happen,
// so just stop for now
return false; // Something went wrong!
}
if (bytesWritten == 0)
continue;
if (bytesWritten < OutputLength)
OutputBuffer = OutputBuffer.substr( bytesWritten );
else if (bytesWritten == OutputLength)
OutputBuffer = "";
OutputLength -= bytesWritten;
}
// All good!
return true;
}
bool SocketConnection::InInSet()
{
char buf[MAX_INPUT_LENGTH];
int bytesRead;
// subtract 1 so that we can always set last to 0
#ifdef unix
bytesRead = read( FileDescriptor, buf, MAX_INPUT_LENGTH - 1 );
#else
#ifdef WIN32
bytesRead = recv( FileDescriptor, buf, MAX_INPUT_LENGTH - 1, 0 );
#endif
#endif
if (bytesRead <= 0)
return false; // Something went wrong!
buf[bytesRead] = '\0';
InputBuffer.append(buf);
// Check for overflow
if (InputBuffer.length() > MAX_INBUF_SIZE)
{
char log_buf[MAX_INPUT_LENGTH];
sprintf( log_buf, "%s input overflow!", GetHost() );
gTheWorld->LogString( log_buf );
SendText("\n\r*** PUT A LID ON IT!!! ***\n\r" );
return false;
}
return true;
}
/*
* Transfer one line from input buffer to input line.
* Return true if a line was found.
* Otherwise, return false.
*/
bool SocketConnection::FindLine()
{
unsigned int inputBufferPos;
unsigned int lineBufferPos;
unsigned int length;
length = InputBuffer.length();
if ( length == 0 )
return false; // if it's empty, just stop now
const char * pInputBuffer = InputBuffer.c_str();
char lineBuffer[MAX_INPUT_LENGTH];
for ( inputBufferPos = 0, lineBufferPos = 0; inputBufferPos < length; inputBufferPos++ )
{
// leave one off for final 0
if ( lineBufferPos >= MAX_INPUT_LENGTH - 1 )
{
SendText("Line too long - truncated.\n\r");
// skip the rest of the line
/*
for ( ; d->inbuf[i] != '\0' || i>= MAX_INBUF_SIZE ; i++ )
{
if ( d->inbuf[i] == '\n' || d->inbuf[i] == '\r' )
break;
}
*/
// Empty the input buffer
InputBuffer = "";
InputLine = "";
return false;
}
if ( pInputBuffer[inputBufferPos] == '\b' && lineBufferPos > 0 )
{
// Move back the pointer one so that the next character
// overwrites the one that was backspaced.
--lineBufferPos;
}
else if ( pInputBuffer[inputBufferPos] == '\n' || pInputBuffer[inputBufferPos] == '\r' )
{
inputBufferPos++;
if ( inputBufferPos < length )
{
// there's at least one character left. Make sure it's not a \n or \r
if ( pInputBuffer[inputBufferPos] == '\n' || pInputBuffer[inputBufferPos] == '\r' )
// It is... so we need to cut off that character.
inputBufferPos++;
}
if (lineBufferPos == 0)
{
// This needs to be a space so that it's recognized
// as "input" - even if it's just empty input
InputLine = " ";
}
else
{
lineBuffer[lineBufferPos] = '\0';
InputLine.assign(lineBuffer);
}
// Remove all processed input buffer.
if (inputBufferPos >= length)
InputBuffer = "";
else
InputBuffer = InputBuffer.substr(inputBufferPos);
return true;
}
else if ( isascii(pInputBuffer[inputBufferPos]) && isprint(pInputBuffer[inputBufferPos]) )
lineBuffer[lineBufferPos++] = pInputBuffer[inputBufferPos];
}
return false;
}
void SocketConnection::ProcessInputBuffer()
{
// Do nothing.
}
bool SocketConnection::InExcSet()
{
//close ( FileDescriptor );
//FileDescriptor = -1;
return true;
}
////////////////////////
// Code object interface
////////////////////////
const string SocketConnection::codeGetClassName()
{
return "SocketConnection";
}
const string SocketConnection::codeGetBasicInfo()
{
// for copying results into
ostringstream os;
os << "Desc #" << this->GetDescriptor() << ".";
os << "Pending: " << OutputLength << " outbound, " <<
InputBuffer.length() << " inbound.";
return os.str();
}
const string SocketConnection::codeGetFullInfo()
{
// for copying results into
ostringstream os;
os << "Socket connection:" << endl;
os << "Opened on port " << this->GetPort() <<
", descriptor #" << this->GetDescriptor() << "." << endl;
return os.str();
}