add unix socket to trans

ulab-original
jsorg71 15 years ago
parent 8a8b57153d
commit 2a7c40dad8

@ -386,6 +386,23 @@ g_tcp_connect(int sck, const char* address, const char* port)
return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in)); return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in));
} }
/*****************************************************************************/
/* returns error, zero is good */
int APP_CC
g_tcp_local_connect(int sck, const char* port)
{
#if defined(_WIN32)
return -1;
#else
struct sockaddr_un s;
memset(&s, 0, sizeof(struct sockaddr_un));
s.sun_family = AF_UNIX;
strcpy(s.sun_path, port);
return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_un));
#endif
}
/*****************************************************************************/ /*****************************************************************************/
int APP_CC int APP_CC
g_tcp_set_non_blocking(int sck) g_tcp_set_non_blocking(int sck)

@ -64,6 +64,8 @@ g_tcp_close(int sck);
int APP_CC int APP_CC
g_tcp_connect(int sck, const char* address, const char* port); g_tcp_connect(int sck, const char* address, const char* port);
int APP_CC int APP_CC
g_tcp_local_connect(int sck, const char* port);
int APP_CC
g_tcp_force_send(int sck, char* data, int len); g_tcp_force_send(int sck, char* data, int len);
int APP_CC int APP_CC
g_tcp_force_recv(int sck, char* data, int len); g_tcp_force_recv(int sck, char* data, int len);

@ -54,6 +54,11 @@ trans_delete(struct trans* self)
free_stream(self->in_s); free_stream(self->in_s);
free_stream(self->out_s); free_stream(self->out_s);
g_tcp_close(self->sck); g_tcp_close(self->sck);
if (self->listen_filename != 0)
{
g_file_delete(self->listen_filename);
g_free(self->listen_filename);
}
g_free(self); g_free(self);
} }
@ -283,9 +288,23 @@ trans_connect(struct trans* self, const char* server, const char* port,
{ {
g_tcp_close(self->sck); g_tcp_close(self->sck);
} }
self->sck = g_tcp_socket(); if (self->mode == 1) /* tcp */
g_tcp_set_non_blocking(self->sck); {
error = g_tcp_connect(self->sck, server, port); 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 (error == -1)
{ {
if (g_tcp_last_error_would_block(self->sck)) if (g_tcp_last_error_would_block(self->sck))
@ -312,15 +331,37 @@ trans_listen(struct trans* self, char* port)
{ {
g_tcp_close(self->sck); g_tcp_close(self->sck);
} }
self->sck = g_tcp_socket(); if (self->mode == 1) /* tcp */
g_tcp_set_non_blocking(self->sck);
if (g_tcp_bind(self->sck, port) == 0)
{ {
if (g_tcp_listen(self->sck) == 0) self->sck = g_tcp_socket();
g_tcp_set_non_blocking(self->sck);
if (g_tcp_bind(self->sck, port) == 0)
{ {
self->status = 1; /* ok */ if (g_tcp_listen(self->sck) == 0)
self->type1 = 1; /* listener */ {
return 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; return 1;

@ -37,7 +37,7 @@ typedef int (*ttrans_conn_in)(struct trans* self, struct trans* new_self);
struct trans struct trans
{ {
tbus sck; tbus sck;
int mode; /* tcp, shared memory, pipes */ int mode; /* 1 tcp, 2 unix socket */
int status; int status;
int type1; /* 1 listener 2 server 3 client */ int type1; /* 1 listener 2 server 3 client */
ttrans_data_in trans_data_in; ttrans_data_in trans_data_in;
@ -46,6 +46,7 @@ struct trans
int header_size; int header_size;
struct stream* in_s; struct stream* in_s;
struct stream* out_s; struct stream* out_s;
char* listen_filename;
}; };
struct trans* APP_CC struct trans* APP_CC

Loading…
Cancel
Save