aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-03-29 13:07:03 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-03-29 13:07:03 +0000
commitcedd3449faaafba0101b44c581c905ba7cd701c7 (patch)
treed3b52c55de450b5830acc42c0d70c1667cb93681
parente9bad3d81d309cb6562031b5ddf8d4350bc9b34b (diff)
clean up more properly on disconnect
disconnecting has the awful tendency to trigger itself; for now, this is handled in the calling object's code, but later iemnet_(sender|receiver) should take care themselves svn path=/trunk/externals/iem/iemnet/; revision=13298
-rw-r--r--iemnet.c21
-rw-r--r--tcpclient.c10
-rw-r--r--tcpserver.c19
3 files changed, 38 insertions, 12 deletions
diff --git a/iemnet.c b/iemnet.c
index 497c036..76f48e6 100644
--- a/iemnet.c
+++ b/iemnet.c
@@ -2,7 +2,7 @@
* copyright (c) 2010 IOhannes m zmlnig, IEM
*/
-#define DEBUG
+//#define DEBUG
#include "iemnet.h"
@@ -308,10 +308,13 @@ t_iemnet_chunk* queue_pop(t_queue* const _this) {
void queue_finish(t_queue* q) {
+ DEBUG("queue_finish: %x", q);
if(NULL==q)
return;
q->done=1;
+ DEBUG("queue signaling: %x", q);
pthread_cond_signal(&q->cond);
+ DEBUG("queue signaled", q);
}
void queue_destroy(t_queue* q) {
@@ -418,10 +421,11 @@ void iemnet__sender_destroy(t_iemnet_sender*s) {
DEBUG("destroy sender %x", s);
s->cont=0;
queue_finish(s->queue);
+ DEBUG("queue finished");
s->sockfd = -1;
pthread_join(s->thread, NULL);
+ DEBUG("thread joined");
queue_destroy(s->queue);
-
freebytes(s, sizeof(t_iemnet_sender));
s=NULL;
DEBUG("destroyed sender");
@@ -499,6 +503,10 @@ static void iemnet_signalNewData(t_iemnet_receiver*x) {
pthread_mutex_lock(&x->newdatamtx);
already=x->newdataflag;
x->newdataflag=1;
+
+ /* don't schedule ticks at the end of life */
+ if(x->sockfd<0)already=1;
+
pthread_mutex_unlock(&x->newdatamtx);
if(already)return;
@@ -603,17 +611,18 @@ t_iemnet_receiver*iemnet__receiver_create(int sock, void*userdata, t_iemnet_rece
return rec;
}
void iemnet__receiver_destroy(t_iemnet_receiver*rec) {
+ int sockfd;
DEBUG("destroy receiver %x", rec);
if(NULL==rec)return;
+ sockfd=rec->sockfd;
+ rec->sockfd=-1;
- shutdown(rec->sockfd, 2); /* needed on linux, since the recv won't shutdown on sys_closesocket() alone */
- sys_closesocket(rec->sockfd);
+ shutdown(sockfd, 2); /* needed on linux, since the recv won't shutdown on sys_closesocket() alone */
+ sys_closesocket(sockfd);
- rec->sockfd=0;
pthread_join(rec->thread, NULL);
iemnet__receiver_tick(rec);
-
if(rec->data)iemnet__chunk_destroy(rec->data);
if(rec->flist)iemnet__floatlist_destroy(rec->flist);
diff --git a/tcpclient.c b/tcpclient.c
index 5c396db..37434b7 100644
--- a/tcpclient.c
+++ b/tcpclient.c
@@ -23,6 +23,7 @@
/* */
/* ---------------------------------------------------------------------------- */
+//#define DEBUG
#include "iemnet.h"
#include <string.h>
@@ -147,11 +148,16 @@ static void tcpclient_disconnect(t_tcpclient *x)
{
if (x->x_fd >= 0)
{
+ int fd=x->x_fd;
+ x->x_fd = -1;
+
+ DEBUG("disconnecting %x", x);
if(x->x_sender)iemnet__sender_destroy(x->x_sender); x->x_sender=NULL;
if(x->x_receiver)iemnet__receiver_destroy(x->x_receiver); x->x_receiver=NULL;
+ DEBUG("disconnect cleaning up %x", x);
+ sys_closesocket(fd);
+
- sys_closesocket(x->x_fd);
- x->x_fd = -1;
x->x_connectstate = 0;
outlet_float(x->x_connectout, 0);
}
diff --git a/tcpserver.c b/tcpserver.c
index ebfd2d5..8a5673b 100644
--- a/tcpserver.c
+++ b/tcpserver.c
@@ -89,20 +89,29 @@ static t_tcpserver_socketreceiver *tcpserver_socketreceiver_new(t_tcpserver *own
static void tcpserver_socketreceiver_free(t_tcpserver_socketreceiver *x)
{
+ DEBUG("freeing %x", x);
if (x != NULL)
{
- if(x->sr_sender) iemnet__sender_destroy(x->sr_sender);
- if(x->sr_receiver)iemnet__receiver_destroy(x->sr_receiver);
+ int sockfd=x->sr_fd;
+ t_iemnet_sender*sender=x->sr_sender;
+ t_iemnet_receiver*receiver=x->sr_receiver;
+
- sys_closesocket(x->sr_fd);
x->sr_owner=NULL;
x->sr_sender=NULL;
x->sr_receiver=NULL;
+
x->sr_fd=-1;
freebytes(x, sizeof(*x));
+
+ if(sender) iemnet__sender_destroy(sender);
+ if(receiver)iemnet__receiver_destroy(receiver);
+
+ sys_closesocket(sockfd);
}
+ DEBUG("freeed %x", x);
}
static int tcpserver_socket2index(t_tcpserver*x, int sockfd)
@@ -257,7 +266,7 @@ static void tcpserver_send_socket(t_tcpserver *x, t_symbol *s, int argc, t_atom
static void tcpserver_disconnect(t_tcpserver *x, int client)
{
int k;
-
+ DEBUG("disconnect %x %d", x, client);
tcpserver_socketreceiver_free(x->x_sr[client]);
x->x_sr[client]=NULL;
@@ -419,7 +428,9 @@ static void tcpserver_free(t_tcpserver *x)
for(i = 0; i < MAX_CONNECT; i++)
{
if (NULL!=x->x_sr[i]) {
+ DEBUG("tcpserver_free %x", x);
tcpserver_socketreceiver_free(x->x_sr[i]);
+ x->x_sr[i]=NULL;
}
}
if (x->x_connectsocket >= 0)