60
60
return "TcpSocket";
63
// return true if the eos flag is set
65
bool TcpSocket::iseos (void) const {
68
if (d_sbuf.length () != 0) {
72
// check if we can read one character
73
bool status = c_rdwait (d_sid, 0);
74
if (status == false) {
78
// read in the character - might be the eos
80
if (c_read (d_sid, &c, 1) <= 0) {
93
// check if we can read one character
95
bool TcpSocket::valid (void) const {
98
if (d_sbuf.length () != 0) {
102
// check if we can read one character
103
bool status = c_rdwait (d_sid, d_tout);
104
if (status == false) {
108
// read in the character - might be the eos
110
if (c_read (d_sid, &c, 1) <= 0) {
63
123
// read one character from the socket
65
125
char TcpSocket::read (void) {
68
128
// check if we can read a character
69
if (valid (-1) == false) return eofc;
129
if (valid () == false) {
70
133
// check the pushback buffer first
71
if (d_buffer.length () != 0) {
72
long result = d_buffer.read ();
134
if (d_sbuf.empty () == false) {
135
char result = d_sbuf.read ();
76
// read the next character
139
// read the next character in case valid
140
// does not push a character (safety code only)
79
143
if ((count = c_read (d_sid, &c, 1)) < 0) {
80
144
throw Exception ("read-error", c_errmsg (count));
83
if (count == 0) return eofc;
147
if (count == 0) c = eosc;
156
// copy the tcp socket into a buffer
158
long TcpSocket::copy (char* rbuf, const long size) {
159
// check argument first
160
if ((rbuf == nilp) || (size <= 0)) return 0;
166
// check the pushback buffer first
167
for (long i = 0; i < size; i++) {
168
if (d_sbuf.empty () == true) break;
169
rbuf[i] = d_sbuf.read ();
172
if (result == size) {
176
// check if we can read one character
177
bool status = c_rdwait (d_sid, d_tout);
178
if (status == false) {
184
if ((count = c_read (d_sid, &rbuf[result], size-result)) < 0) {
185
throw Exception ("read-error", c_errmsg (count));
92
196
// write one character to the socket
94
void TcpSocket::write (const char value) {
96
long count = c_write (d_sid, &value, 1);
98
if (count < 0) throw Exception ("write-error", c_errmsg (count));
101
// write a character string to the socket
103
void TcpSocket::write (const char* value) {
104
long size = Ascii::strlen (value);
105
if (size == 0) return;
107
long count = c_write (d_sid, value, size);
109
if (count < 0) throw Exception ("write-error", c_errmsg (count));
112
// return true if the eof flag is set
114
bool TcpSocket::iseof (void) const {
117
if (d_buffer.length () != 0) {
121
// check if we can read one character
122
bool status = c_rdwait (d_sid, 0);
123
if (status == false) {
127
// read in the character - might be the eof
129
if (c_read (d_sid, &c, 1) <= 0) {
133
d_buffer.pushback (c);
142
// check if we can read one character
144
bool TcpSocket::valid (const long tout) const {
147
if (d_buffer.length () != 0) {
151
// check if we can read one character
152
bool status = c_rdwait (d_sid, tout);
153
if (status == false) {
157
// read in the character - might be the eof
159
if (c_read (d_sid, &c, 1) <= 0) {
163
d_buffer.pushback (c);
198
long TcpSocket::write (const char value) {
201
long result = c_write (d_sid, &value, 1);
202
if (result < 0) throw Exception ("write-error", c_errmsg (result));
211
// write a data buffer to the socket
213
long TcpSocket::write (const char* data) {
217
// check for size first
218
long size = Ascii::strlen (data);
223
long result = c_write (d_sid, data, size);
224
if (result < 0) throw Exception ("write-error", c_errmsg (result));
233
// write a character array to the socket
235
long TcpSocket::write (const char* rbuf, const long size) {
236
// check argument first
237
if ((rbuf == nilp) || (size <= 0)) return 0;
242
while (result != size) {
243
long count = c_write (d_sid, &rbuf[result], size-result);
244
if (count < 0) throw Exception ("write-error", c_errmsg (result));
245
if (count == 0) break;
188
275
bool TcpSocket::listen (const long backlog) const {
190
bool result = c_iplisten (d_sid, backlog);
278
bool result = c_iplisten (d_sid, backlog);
195
287
// accept a connection from this tcp socket
197
289
TcpSocket* TcpSocket::accept (void) const {
199
int sid = c_ipaccept (d_sid);
202
throw Exception ("accept-error", c_errmsg (sid));
292
int sid = c_ipaccept (d_sid);
293
if (sid < 0) throw Exception ("accept-error", c_errmsg (sid));
294
TcpSocket* result = new TcpSocket (sid);
204
TcpSocket* result = new TcpSocket (sid);
209
303
// -------------------------------------------------------------------------