diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/trans.c | 171 | ||||
-rw-r--r-- | common/trans.h | 4 |
2 files changed, 110 insertions, 65 deletions
diff --git a/common/trans.c b/common/trans.c index 408b4a7e..ae150940 100644 --- a/common/trans.c +++ b/common/trans.c @@ -92,6 +92,86 @@ trans_get_wait_objs(struct trans *self, tbus *objs, int *count) /*****************************************************************************/ int APP_CC +trans_get_wait_objs_rw(struct trans *self, + tbus *robjs, int *rcount, + tbus *wobjs, int *wcount) +{ + if (self == 0) + { + return 1; + } + + if (self->status != TRANS_STATUS_UP) + { + return 1; + } + + robjs[*rcount] = self->sck; + (*rcount)++; + + if (self->wait_s != 0) + { + wobjs[*wcount] = self->sck; + (*wcount)++; + } + + return 0; +} + +/*****************************************************************************/ +int APP_CC +send_waiting(struct trans *self, int block) +{ + struct stream *temp_s; + int bytes; + int sent; + int timeout; + int cont; + + timeout = block ? 100 : 0; + cont = 1; + while (cont) + { + if (self->wait_s != 0) + { + temp_s = self->wait_s; + if (g_tcp_can_send(self->sck, timeout)) + { + bytes = (int) (temp_s->end - temp_s->p); + sent = g_tcp_send(self->sck, temp_s->p, bytes, 0); + if (sent > 0) + { + temp_s->p += sent; + if (temp_s->p >= temp_s->end) + { + self->wait_s = (struct stream *) (temp_s->next_packet); + free_stream(temp_s); + } + } + else if (sent == 0) + { + return 1; + } + else + { + if (!g_tcp_last_error_would_block(self->sck)) + { + return 1; + } + } + } + } + else + { + break; + } + cont = block; + } + return 0; +} + +/*****************************************************************************/ +int APP_CC trans_check_wait_objs(struct trans *self) { tbus in_sck = (tbus)0; @@ -203,6 +283,12 @@ trans_check_wait_objs(struct trans *self) } } } + if (send_waiting(self, 0) != 0) + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } } return rv; @@ -291,7 +377,12 @@ trans_force_write_s(struct trans *self, struct stream *out_s) size = (int)(out_s->end - out_s->data); total = 0; - self->in_write = 1; + if (send_waiting(self, 1) != 0) + { + self->status = TRANS_STATUS_DOWN; + return 1; + } + while (total < size) { sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0); @@ -309,7 +400,6 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* term */ self->status = TRANS_STATUS_DOWN; - self->in_write = 0; return 1; } } @@ -319,7 +409,6 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* error */ self->status = TRANS_STATUS_DOWN; - self->in_write = 0; return 1; } } @@ -327,7 +416,6 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* error */ self->status = TRANS_STATUS_DOWN; - self->in_write = 0; return 1; } else @@ -335,7 +423,6 @@ trans_force_write_s(struct trans *self, struct stream *out_s) total = total + sent; } } - self->in_write = 0; return 0; } @@ -349,15 +436,12 @@ trans_force_write(struct trans *self) /*****************************************************************************/ int APP_CC -trans_write_check(struct trans* self, int timeout) +trans_write_copy(struct trans *self) { int size; - int total; - int sent; - int error; - tbus robjs[1]; - tbus wobjs[1]; struct stream *out_s; + struct stream *wait_s; + struct stream *temp_s; if (self->status != TRANS_STATUS_UP) { @@ -365,63 +449,24 @@ trans_write_check(struct trans* self, int timeout) } out_s = self->out_s; - size = (int)(out_s->end - out_s->data); - total = 0; - - self->in_write = 1; - while (total < size) + make_stream(wait_s); + init_stream(wait_s, size); + out_uint8a(wait_s, out_s->data, size); + s_mark_end(wait_s); + if (self->wait_s == 0) { - robjs[0] = self->sck; - wobjs[0] = self->sck; - error = g_obj_wait(robjs, 1, wobjs, 1, timeout); - if (error != 0) - { - /* error */ - self->status = TRANS_STATUS_DOWN; - self->in_write = 0; - return 1; - } - - if (!g_tcp_can_send(self->sck, 0)) - { - trans_check_wait_objs(self); - continue; - } - - sent = g_tcp_send(self->sck, 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 = TRANS_STATUS_DOWN; - self->in_write = 0; - return 1; - } - } - else if (sent == 0) - { - /* error */ - self->status = TRANS_STATUS_DOWN; - self->in_write = 0; - return 1; - } - else + self->wait_s = wait_s; + } + else + { + temp_s = self->wait_s; + while (temp_s->next_packet != 0) { - total = total + sent; + temp_s = (struct stream *) (temp_s->next_packet); } + temp_s->next_packet = wait_s; } - self->in_write = 0; - return 0; } diff --git a/common/trans.h b/common/trans.h index d9ad6c19..b7b9c20d 100644 --- a/common/trans.h +++ b/common/trans.h @@ -54,7 +54,7 @@ struct trans struct stream* out_s; char* listen_filename; tis_term is_term; /* used to test for exit */ - int in_write; + struct stream* wait_s; }; struct trans* APP_CC @@ -74,7 +74,7 @@ trans_force_read(struct trans* self, int size); int APP_CC trans_force_write(struct trans* self); int APP_CC -trans_write_check(struct trans* self, int timeout); +trans_write_copy(struct trans* self); int APP_CC trans_connect(struct trans* self, const char* server, const char* port, int timeout); |