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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
Copyright (c) 2008-2009 Jay Sorg
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
generic transport
*/
#include "os_calls.h"
#include "trans.h"
#include "arch.h"
#include "parse.h"
/*****************************************************************************/
struct trans* APP_CC
trans_create(int mode, int in_size, int out_size)
{
struct trans* self;
self = (struct trans*)g_malloc(sizeof(struct trans), 1);
make_stream(self->in_s);
init_stream(self->in_s, in_size);
make_stream(self->out_s);
init_stream(self->out_s, out_size);
self->mode = mode;
return self;
}
/*****************************************************************************/
void APP_CC
trans_delete(struct trans* self)
{
if (self == 0)
{
return;
}
free_stream(self->in_s);
free_stream(self->out_s);
g_tcp_close(self->sck);
if (self->listen_filename != 0)
{
g_file_delete(self->listen_filename);
g_free(self->listen_filename);
}
g_free(self);
}
/*****************************************************************************/
int APP_CC
trans_get_wait_objs(struct trans* self, tbus* objs, int* count, int* timeout)
{
if (self == 0)
{
return 1;
}
if (self->status != 1)
{
return 1;
}
objs[*count] = self->sck;
(*count)++;
return 0;
}
/*****************************************************************************/
int APP_CC
trans_check_wait_objs(struct trans* self)
{
tbus in_sck;
struct trans* in_trans;
int read_bytes;
int to_read;
int read_so_far;
int rv;
if (self == 0)
{
return 1;
}
if (self->status != 1)
{
return 1;
}
rv = 0;
if (self->type1 == 1) /* listening */
{
if (g_tcp_can_recv(self->sck, 0))
{
in_sck = g_tcp_accept(self->sck);
if (in_sck == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
/* ok, but shouldn't happen */
}
else
{
/* error */
self->status = 0;
rv = 1;
}
}
if (in_sck != -1)
{
if (self->trans_conn_in != 0) /* is function assigned */
{
in_trans = trans_create(self->mode, self->in_s->size,
self->out_s->size);
in_trans->sck = in_sck;
in_trans->type1 = 2;
in_trans->status = 1;
if (self->trans_conn_in(self, in_trans) != 0)
{
trans_delete(in_trans);
}
}
else
{
g_tcp_close(in_sck);
}
}
}
}
else /* connected server or client (2 or 3) */
{
if (g_tcp_can_recv(self->sck, 0))
{
read_so_far = (int)(self->in_s->end - self->in_s->data);
to_read = self->header_size - read_so_far;
read_bytes = g_tcp_recv(self->sck, self->in_s->end, to_read, 0);
if (read_bytes == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
/* ok, but shouldn't happen */
}
else
{
/* error */
self->status = 0;
rv = 1;
}
}
else if (read_bytes == 0)
{
/* error */
self->status = 0;
rv = 1;
}
else
{
self->in_s->end += read_bytes;
}
read_so_far = (int)(self->in_s->end - self->in_s->data);
if (read_so_far == self->header_size)
{
if (self->trans_data_in != 0)
{
rv = self->trans_data_in(self);
init_stream(self->in_s, 0);
}
}
}
}
return rv;
}
/*****************************************************************************/
int APP_CC
trans_force_read(struct trans* self, int size)
{
int rv;
int rcvd;
if (self->status != 1)
{
return 1;
}
rv = 0;
while (size > 0)
{
rcvd = g_tcp_recv(self->sck, self->in_s->end, size, 0);
if (rcvd == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (!g_tcp_can_recv(self->sck, 10))
{
/* check for term here */
}
}
else
{
/* error */
self->status = 0;
rv = 1;
}
}
else if (rcvd == 0)
{
/* error */
self->status = 0;
rv = 1;
}
else
{
self->in_s->end += rcvd;
size -= rcvd;
}
}
return rv;
}
/*****************************************************************************/
int APP_CC
trans_force_write(struct trans* self)
{
int size;
int total;
int rv;
int sent;
if (self->status != 1)
{
return 1;
}
rv = 0;
size = (int)(self->out_s->end - self->out_s->data);
total = 0;
while (total < size)
{
sent = g_tcp_send(self->sck, self->out_s->data + total, size - total, 0);
if (sent == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (!g_tcp_can_send(self->sck, 10))
{
/* check for term here */
}
}
else
{
/* error */
self->status = 0;
rv = 1;
}
}
else if (sent == 0)
{
/* error */
self->status = 0;
rv = 1;
}
else
{
total = total + sent;
}
}
return rv;
}
/*****************************************************************************/
int APP_CC
trans_connect(struct trans* self, const char* server, const char* port,
int timeout)
{
int error;
if (self->sck != 0)
{
g_tcp_close(self->sck);
}
if (self->mode == 1) /* tcp */
{
self->sck = g_tcp_socket();
g_tcp_set_non_blocking(self->sck);
error = g_tcp_connect(self->sck, server, port);
}
else if (self->mode == 2) /* unix socket */
{
self->sck = g_tcp_local_socket();
g_tcp_set_non_blocking(self->sck);
error = g_tcp_local_connect(self->sck, port);
}
else
{
self->status = 0;
return 1;
}
if (error == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (g_tcp_can_send(self->sck, timeout))
{
self->status = 1; /* ok */
self->type1 = 3; /* client */
return 0;
}
}
return 1;
}
self->status = 1; /* ok */
self->type1 = 3; /* client */
return 0;
}
/*****************************************************************************/
int APP_CC
trans_listen(struct trans* self, char* port)
{
if (self->sck != 0)
{
g_tcp_close(self->sck);
}
if (self->mode == 1) /* tcp */
{
self->sck = g_tcp_socket();
g_tcp_set_non_blocking(self->sck);
if (g_tcp_bind(self->sck, port) == 0)
{
if (g_tcp_listen(self->sck) == 0)
{
self->status = 1; /* ok */
self->type1 = 1; /* listener */
return 0;
}
}
}
else if (self->mode == 2) /* unix socket */
{
g_free(self->listen_filename);
self->listen_filename = 0;
g_file_delete(port);
self->sck = g_tcp_local_socket();
g_tcp_set_non_blocking(self->sck);
if (g_tcp_local_bind(self->sck, port) == 0)
{
self->listen_filename = g_strdup(port);
if (g_tcp_listen(self->sck) == 0)
{
g_chmod_hex(port, 0xffff);
self->status = 1; /* ok */
self->type1 = 1; /* listener */
return 0;
}
}
}
return 1;
}
/*****************************************************************************/
struct stream* APP_CC
trans_get_in_s(struct trans* self)
{
return self->in_s;
}
/*****************************************************************************/
struct stream* APP_CC
trans_get_out_s(struct trans* self, int size)
{
init_stream(self->out_s, size);
return self->out_s;
}
|