improve trans

ulab-original
jsorg71 15 years ago
parent e83b4c43f5
commit 27e097663f

@ -1,5 +1,5 @@
/*
Copyright (c) 2008-2009 Jay Sorg
Copyright (c) 2008-2010 Jay Sorg
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
@ -70,7 +70,7 @@ trans_get_wait_objs(struct trans* self, tbus* objs, int* count, int* timeout)
{
return 1;
}
if (self->status != 1)
if (self->status != TRANS_STATUS_UP)
{
return 1;
}
@ -94,12 +94,12 @@ trans_check_wait_objs(struct trans* self)
{
return 1;
}
if (self->status != 1)
if (self->status != TRANS_STATUS_UP)
{
return 1;
}
rv = 0;
if (self->type1 == 1) /* listening */
if (self->type1 == TRANS_TYPE_LISTENER) /* listening */
{
if (g_tcp_can_recv(self->sck, 0))
{
@ -113,7 +113,7 @@ trans_check_wait_objs(struct trans* self)
else
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
}
@ -124,8 +124,8 @@ trans_check_wait_objs(struct trans* self)
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;
in_trans->type1 = TRANS_TYPE_SERVER;
in_trans->status = TRANS_STATUS_UP;
if (self->trans_conn_in(self, in_trans) != 0)
{
trans_delete(in_trans);
@ -144,29 +144,32 @@ trans_check_wait_objs(struct trans* self)
{
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 (to_read > 0)
{
if (g_tcp_last_error_would_block(self->sck))
read_bytes = g_tcp_recv(self->sck, self->in_s->end, to_read, 0);
if (read_bytes == -1)
{
/* ok, but shouldn't happen */
if (g_tcp_last_error_would_block(self->sck))
{
/* ok, but shouldn't happen */
}
else
{
/* error */
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
}
else
else if (read_bytes == 0)
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
}
else if (read_bytes == 0)
{
/* error */
self->status = 0;
rv = 1;
}
else
{
self->in_s->end += read_bytes;
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)
@ -184,19 +187,19 @@ trans_check_wait_objs(struct trans* self)
/*****************************************************************************/
int APP_CC
trans_force_read(struct trans* self, int size)
trans_force_read_s(struct trans* self, struct stream* in_s, int size)
{
int rv;
int rcvd;
if (self->status != 1)
if (self->status != TRANS_STATUS_UP)
{
return 1;
}
rv = 0;
while (size > 0)
{
rcvd = g_tcp_recv(self->sck, self->in_s->end, size, 0);
rcvd = g_tcp_recv(self->sck, in_s->end, size, 0);
if (rcvd == -1)
{
if (g_tcp_last_error_would_block(self->sck))
@ -209,19 +212,19 @@ trans_force_read(struct trans* self, int size)
else
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
}
else if (rcvd == 0)
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
else
{
self->in_s->end += rcvd;
in_s->end += rcvd;
size -= rcvd;
}
}
@ -230,23 +233,30 @@ trans_force_read(struct trans* self, int size)
/*****************************************************************************/
int APP_CC
trans_force_write(struct trans* self)
trans_force_read(struct trans* self, int size)
{
return trans_force_read_s(self, self->in_s, size);
}
/*****************************************************************************/
int APP_CC
trans_force_write_s(struct trans* self, struct stream* out_s)
{
int size;
int total;
int rv;
int sent;
if (self->status != 1)
if (self->status != TRANS_STATUS_UP)
{
return 1;
}
rv = 0;
size = (int)(self->out_s->end - self->out_s->data);
size = (int)(out_s->end - out_s->data);
total = 0;
while (total < size)
{
sent = g_tcp_send(self->sck, self->out_s->data + total, size - total, 0);
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))
@ -259,14 +269,14 @@ trans_force_write(struct trans* self)
else
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
}
else if (sent == 0)
{
/* error */
self->status = 0;
self->status = TRANS_STATUS_DOWN;
rv = 1;
}
else
@ -277,6 +287,13 @@ trans_force_write(struct trans* self)
return rv;
}
/*****************************************************************************/
int APP_CC
trans_force_write(struct trans* self)
{
return trans_force_write_s(self, self->out_s);
}
/*****************************************************************************/
int APP_CC
trans_connect(struct trans* self, const char* server, const char* port,
@ -288,13 +305,13 @@ trans_connect(struct trans* self, const char* server, const char* port,
{
g_tcp_close(self->sck);
}
if (self->mode == 1) /* tcp */
if (self->mode == TRANS_MODE_TCP) /* 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 */
else if (self->mode == TRANS_MODE_UNIX) /* unix socket */
{
self->sck = g_tcp_local_socket();
g_tcp_set_non_blocking(self->sck);
@ -302,7 +319,7 @@ trans_connect(struct trans* self, const char* server, const char* port,
}
else
{
self->status = 0;
self->status = TRANS_STATUS_DOWN;
return 1;
}
if (error == -1)
@ -311,15 +328,15 @@ trans_connect(struct trans* self, const char* server, const char* port,
{
if (g_tcp_can_send(self->sck, timeout))
{
self->status = 1; /* ok */
self->type1 = 3; /* client */
self->status = TRANS_STATUS_UP; /* ok */
self->type1 = TRANS_TYPE_CLIENT; /* client */
return 0;
}
}
return 1;
}
self->status = 1; /* ok */
self->type1 = 3; /* client */
self->status = TRANS_STATUS_UP; /* ok */
self->type1 = TRANS_TYPE_CLIENT; /* client */
return 0;
}
@ -331,7 +348,7 @@ trans_listen(struct trans* self, char* port)
{
g_tcp_close(self->sck);
}
if (self->mode == 1) /* tcp */
if (self->mode == TRANS_MODE_TCP) /* tcp */
{
self->sck = g_tcp_socket();
g_tcp_set_non_blocking(self->sck);
@ -339,13 +356,13 @@ trans_listen(struct trans* self, char* port)
{
if (g_tcp_listen(self->sck) == 0)
{
self->status = 1; /* ok */
self->type1 = 1; /* listener */
self->status = TRANS_STATUS_UP; /* ok */
self->type1 = TRANS_TYPE_LISTENER; /* listener */
return 0;
}
}
}
else if (self->mode == 2) /* unix socket */
else if (self->mode == TRANS_MODE_UNIX) /* unix socket */
{
g_free(self->listen_filename);
self->listen_filename = 0;
@ -358,8 +375,8 @@ trans_listen(struct trans* self, char* port)
if (g_tcp_listen(self->sck) == 0)
{
g_chmod_hex(port, 0xffff);
self->status = 1; /* ok */
self->type1 = 1; /* listener */
self->status = TRANS_STATUS_UP; /* ok */
self->type1 = TRANS_TYPE_LISTENER; /* listener */
return 0;
}
}

@ -1,5 +1,5 @@
/*
Copyright (c) 2008-2009 Jay Sorg
Copyright (c) 2008-2010 Jay Sorg
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
@ -29,6 +29,16 @@
#include "arch.h"
#include "parse.h"
#define TRANS_MODE_TCP 1
#define TRANS_MODE_UNIX 2
#define TRANS_TYPE_LISTENER 1
#define TRANS_TYPE_SERVER 2
#define TRANS_TYPE_CLIENT 3
#define TRANS_STATUS_DOWN 0
#define TRANS_STATUS_UP 1
struct trans; /* forward declaration */
typedef int (*ttrans_data_in)(struct trans* self);
@ -58,6 +68,10 @@ trans_get_wait_objs(struct trans* self, tbus* objs, int* count, int* timeout);
int APP_CC
trans_check_wait_objs(struct trans* self);
int APP_CC
trans_force_read_s(struct trans* self, struct stream* in_s, int size);
int APP_CC
trans_force_write_s(struct trans* self, struct stream* out_s);
int APP_CC
trans_force_read(struct trans* self, int size);
int APP_CC
trans_force_write(struct trans* self);

Loading…
Cancel
Save