summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfcolin2005-07-13 11:27:40 +0000
committerfcolin2005-07-13 11:27:40 +0000
commita68db8d4f235c2ea59aea9cf7311474ac85f5124 (patch)
treee3189d4b3b59da02db394225f2814dadd85871e2 /src
parentab7132a03e9be01340c04d228731372ff40ddf16 (diff)
downloadivy-c-a68db8d4f235c2ea59aea9cf7311474ac85f5124.zip
ivy-c-a68db8d4f235c2ea59aea9cf7311474ac85f5124.tar.gz
ivy-c-a68db8d4f235c2ea59aea9cf7311474ac85f5124.tar.bz2
ivy-c-a68db8d4f235c2ea59aea9cf7311474ac85f5124.tar.xz
menage dans ivysocket
Diffstat (limited to 'src')
-rw-r--r--src/ivy.c4
-rw-r--r--src/ivysocket.c138
-rw-r--r--src/ivysocket.h5
3 files changed, 69 insertions, 78 deletions
diff --git a/src/ivy.c b/src/ivy.c
index ca55b92..edc2d0c 100644
--- a/src/ivy.c
+++ b/src/ivy.c
@@ -362,7 +362,7 @@ static int CheckConnected( IvyClientPtr clnt )
return 0;
}
-static char* Receive( Client client, void *data, char *message, unsigned long len )
+static char* Receive( Client client, void *data, char *message, unsigned int len )
{
IvyClientPtr clnt;
int id;
@@ -621,7 +621,7 @@ static void *ClientCreate( Client client )
return SendService (client);
}
-static char* BroadcastReceive( Client client, void *data, char *message, unsigned long len)
+static char* BroadcastReceive( Client client, void *data, char *message, unsigned int len)
{
Client app;
int err;
diff --git a/src/ivysocket.c b/src/ivysocket.c
index da5e7a8..4b9f279 100644
--- a/src/ivysocket.c
+++ b/src/ivysocket.c
@@ -61,9 +61,14 @@ struct _client {
struct sockaddr_in from;
SocketInterpretation interpretation;
SocketDelete handle_delete;
- long buffer_size;
+ /* input buffer */
+ int buffer_size;
char *buffer; /* dynamicaly reallocated */
char *ptr;
+ /* output buffer */
+ int out_buffer_size;
+ char *out_buffer; /* dynamicaly reallocated */
+
void *data;
};
@@ -78,7 +83,7 @@ WSADATA WsaData;
int make_message(char ** buffer, int *size, int offset, const char *fmt, va_list ap)
{
/* Guess we need no more than BUFFER_INIT_SIZE bytes. */
- int n;
+ long n;
if ( *size == 0 || *buffer == NULL )
{
*size = BUFFER_SIZE;
@@ -108,14 +113,12 @@ int make_message(char ** buffer, int *size, int offset, const char *fmt, va_lis
int make_message_var(char ** buffer, int *size, int offset, const char *fmt, ... )
{
va_list ap;
- int len;
+ long len;
va_start (ap, fmt );
len = make_message (buffer,size, offset, fmt, ap );
va_end (ap );
return len;
}
-
-
static void DeleteSocket(void *data)
{
Client client = (Client )data;
@@ -156,7 +159,7 @@ static void HandleSocket (Channel channel, HANDLE fd, void *data)
fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
exit(0);
}
- fprintf(stderr, "Buffer Limit reached realloc new size %ld\n", client->buffer_size );
+ fprintf(stderr, "Buffer Limit reached realloc new size %d\n", client->buffer_size );
nb_to_read = client->buffer_size - (client->ptr - client->buffer );
}
len = sizeof (client->from );
@@ -194,6 +197,36 @@ static void HandleSocket (Channel channel, HANDLE fd, void *data)
client->ptr = client->buffer;
}
}
+static Client CreateClient(int handle)
+{
+ Client client;
+ IVY_LIST_ADD (clients_list, client );
+ if (!client )
+ {
+ fprintf(stderr,"NOK Memory Alloc Error\n");
+ close ( handle );
+ exit(0);
+ }
+ client->buffer_size = BUFFER_SIZE;
+ client->buffer = malloc( client->buffer_size );
+ if (!client->buffer )
+ {
+ fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
+ exit(0);
+ }
+ client->ptr = client->buffer;
+ client->out_buffer_size = BUFFER_SIZE;
+ client->out_buffer = malloc( client->out_buffer_size );
+ if (!client->buffer )
+ {
+ fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
+ exit(0);
+ }
+ client->fd = handle;
+ client->channel = IvyChannelOpen (client->fd, client, DeleteSocket, HandleSocket );
+
+ return client;
+}
static void HandleServer(Channel channel, HANDLE fd, void *data)
{
@@ -214,29 +247,14 @@ static void HandleServer(Channel channel, HANDLE fd, void *data)
#ifdef DEBUG
printf( "Accepting Connection ret\n");
#endif //DEBUG
- IVY_LIST_ADD (clients_list, client );
- if (!client )
- {
- fprintf(stderr,"NOK Memory Alloc Error\n");
- close (fd );
- exit(0);
- }
- client->buffer_size = BUFFER_SIZE;
- client->buffer = malloc( client->buffer_size );
- if (!client->buffer )
- {
- fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
- exit(0);
- }
+ client = CreateClient(ns);
client->from = remote2;
- client->fd = ns;
- client->channel = IvyChannelOpen (ns, client, DeleteSocket, HandleSocket );
client->interpretation = server->interpretation;
- client->ptr = client->buffer;
client->handle_delete = server->handle_delete;
client->data = (*server->create) (client );
}
+
Server SocketServer(unsigned short port,
SocketCreate create,
SocketDelete handle_delete,
@@ -386,15 +404,13 @@ void SocketSetData (Client client, void *data )
void SocketSend (Client client, char *fmt, ... )
{
- static char *buffer = NULL; /* Use satic mem to eliminate multiple call to malloc /free */
- static int size = 0; /* donc non reentrant !!!! */
- va_list ap;
int len;
+ va_list ap;
if (!client)
return;
va_start (ap, fmt );
- len = make_message (&buffer,&size, 0, fmt, ap );
- SocketSendRaw (client, buffer, len );
+ len = make_message (&client->out_buffer, &client->out_buffer_size, 0, fmt, ap );
+ SocketSendRaw (client, client->out_buffer, len );
va_end (ap );
}
@@ -403,20 +419,18 @@ void *SocketGetData (Client client )
return client ? client->data : 0;
}
-void SocketBroadcast ( char *fmt, ... )
+void SocketSendToAll ( char *fmt, ... )
{
Client client;
- static char *buffer = NULL; /* Use satic mem to eliminate multiple call to malloc /free */
- static int size = 0; /* donc non reentrant !!!! */
va_list ap;
int len;
va_start (ap, fmt );
- len = make_message (&buffer, &size, 0, fmt, ap );
+ len = make_message (&client->out_buffer, &client->out_buffer_size, 0, fmt, ap );
va_end (ap );
IVY_LIST_EACH (clients_list, client )
{
- SocketSendRaw (client, buffer, len );
+ SocketSendRaw (client, client->out_buffer, len );
}
}
@@ -462,26 +476,10 @@ Client SocketConnectAddr (struct in_addr * addr, unsigned short port,
return NULL;
};
- IVY_LIST_ADD (clients_list, client );
- if (!client ) {
- fprintf(stderr,"NOK Memory Alloc Error\n");
- close (handle );
- exit(0);
- }
-
- client->buffer_size = BUFFER_SIZE;
- client->buffer = malloc( client->buffer_size );
- if (!client->buffer )
- {
- fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
- exit(0);
- }
- client->fd = handle;
- client->channel = IvyChannelOpen (handle, client, DeleteSocket, HandleSocket );
+ client = CreateClient(handle);
client->interpretation = interpretation;
- client->ptr = client->buffer;
- client->data = data;
client->handle_delete = handle_delete;
+ client->data = data;
client->from.sin_family = AF_INET;
client->from.sin_addr = *addr;
client->from.sin_port = htons (port);
@@ -537,41 +535,33 @@ Client SocketBroadcastCreate (
return NULL;
};
- IVY_LIST_ADD(clients_list, client );
- if (!client ) {
- fprintf(stderr,"NOK Memory Alloc Error\n");
- close (handle );
- exit(0);
- }
-
- client->buffer_size = BUFFER_SIZE;
- client->buffer = malloc( client->buffer_size );
- if (!client->buffer )
- {
- fprintf(stderr,"HandleSocket Buffer Memory Alloc Error\n");
- exit(0);
- }
- client->fd = handle;
- client->channel = IvyChannelOpen (handle, client, DeleteSocket, HandleSocket );
+ client = CreateClient(handle);
client->interpretation = interpretation;
- client->ptr = client->buffer;
client->data = data;
return client;
}
void SocketSendBroadcast (Client client, unsigned long host, unsigned short port, char *fmt, ... )
{
- struct sockaddr_in remote;
- static char *buffer = NULL; /* Use satic mem to eliminate multiple call to malloc /free */
- static int size = 0; /* donc non reentrant !!!! */
va_list ap;
- int err,len;
+ int len;
if (!client)
return;
va_start (ap, fmt );
- len = make_message (&buffer, &size, 0, fmt, ap );
+ len = make_message (&client->out_buffer, &client->out_buffer_size, 0, fmt, ap );
+ SocketSendBroadcastRaw( client, host, port, client->out_buffer, len );
+ va_end (ap );
+}
+void SocketSendBroadcastRaw (Client client, unsigned long host, unsigned short port, char *buffer, int len )
+{
+ struct sockaddr_in remote;
+ int err;
+
+ if (!client)
+ return;
+
/* Send UDP packet to the dest */
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = htonl (host );
@@ -581,7 +571,7 @@ void SocketSendBroadcast (Client client, unsigned long host, unsigned short port
(struct sockaddr *)&remote,sizeof(remote));
if (err != len) {
perror ("*** send ***");
- } va_end (ap );
+ }
}
void SocketKeepAlive( Client client,int keepalive )
{
diff --git a/src/ivysocket.h b/src/ivysocket.h
index 9bfb17f..3d8c991 100644
--- a/src/ivysocket.h
+++ b/src/ivysocket.h
@@ -53,7 +53,7 @@ extern int make_message_var(char ** buffer, int *size, int offset, const char *
/* Forward def */
typedef struct _client *Client;
-typedef char* (*SocketInterpretation) (Client client, void *data, char *ligne, unsigned long len);
+typedef char* (*SocketInterpretation) (Client client, void *data, char *ligne, unsigned int len);
typedef void* (*SocketCreate) (Client client);
typedef void (*SocketDelete) (Client client, void *data);
@@ -74,7 +74,7 @@ extern void SocketSendRaw( Client client, char *buffer, int len );
extern char *SocketGetPeerHost( Client client );
extern void SocketSetData( Client client, void *data );
extern void *SocketGetData( Client client );
-extern void SocketBroadcast( char *fmt, ... );
+extern void SocketSendToAll( char *fmt, ... );
extern Client SocketConnect( char * host, unsigned short port,
void *data,
SocketInterpretation interpretation,
@@ -101,6 +101,7 @@ extern struct in_addr * SocketGetRemoteAddr( Client client );
extern void SocketGetRemoteHost (Client client, char **host, unsigned short *port );
/* emmission d'un broadcast UDP */
extern void SocketSendBroadcast( Client client, unsigned long host, unsigned short port, char *fmt, ... );
+extern void SocketSendBroadcastRaw( Client client, unsigned long host, unsigned short port, char *buffer, int len );
#ifdef __cplusplus
}