chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / clients.c
1 /*
2  * clients.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.2  2008/02/14 00:57:58  james
14  * *** empty log message ***
15  *
16  * Revision 1.1  2008/02/13 18:05:06  james
17  * *** empty log message ***
18  *
19  */
20
21 #include <sympathy.h>
22 #include "clients.h"
23
24
25 void
26 client_free (Client * c)
27 {
28   if (c->s)
29     socket_free (c->s);
30
31   free (c);
32   fprintf(stderr,"Client at %p freed\n",c);
33 }
34
35 Client *
36 clients_new_client (Clients * cs, Socket * s, Context * ctx)
37 {
38   Client *c;
39
40   c = (Client *) malloc (sizeof (Client));
41
42   c->dead = 0;
43   c->s = s;
44   c->next = cs->head;
45
46   cs->head = c;
47   cs->n++;
48
49   fprintf(stderr,"Client at %p created\n",c);
50
51
52   if (ipc_msg_send_debug (s, "new_client")) 
53         c->dead++;
54
55   return c;
56 }
57
58 void
59 clients_reap (Clients * cs)
60 {
61   Client **p, *c;
62
63
64   for (p = &cs->head; *p;)
65     {
66       Client *c = *p;
67
68       if (c->dead)
69         {
70           *p = c->next;
71           client_free (c);
72           cs->n--;
73         }
74       else
75         {
76           p = &(c->next);
77         }
78     }
79 }
80
81 Clients *
82 clients_new (void)
83 {
84   Clients *ret = (Clients *) malloc (sizeof (Clients));
85
86   ret->n = 0;
87   ret->head = NULL;
88
89   return ret;
90 }
91
92 void
93 clients_pre_select (Clients * cs, fd_set * rfds, fd_set * wfds)
94 {
95   Client *c;
96
97   for (c = cs->head; c; c = c->next)
98     {
99       socket_pre_select (c->s, rfds, wfds);
100     }
101 }
102
103 void
104 clients_post_select (Clients * cs, Context * ctx, fd_set * rfds,
105                      fd_set * wfds)
106 {
107   Client *c;
108   int deaded = 0;
109
110   for (c = cs->head; c; c = c->next)
111     {
112       if (socket_post_select (c->s, rfds, wfds))
113         {
114           c->dead++;
115           deaded++;
116         }
117     }
118
119   if (deaded)
120     clients_reap (cs);
121 }
122
123 void
124 clients_output (Clients * cs, void *_buf, int len)
125 {
126 uint8_t *buf=(uint8_t *) _buf;
127 Client *c;
128
129 #define DEBUG_MSG_LEN 128
130
131   char mbuf[sizeof (IPC_Msg_hdr) + DEBUG_MSG_LEN];
132   IPC_Msg *m;
133 int i;
134
135 if (!len) return;
136
137   m = (IPC_Msg *) mbuf;
138   m->debug.type = IPC_MSG_TYPE_DEBUG;
139   i=sprintf(m->debug.msg,"buf[0]=%d len=%d",buf[0],len);
140   m->debug.size = sizeof (IPC_Msg_hdr) + i + 1;
141
142
143
144   for (c = cs->head; c; c = c->next)
145     {
146         if (!c->dead) 
147         if (ipc_msg_send(c->s,m)) 
148                 c->dead++;
149     }
150
151
152 }
153
154 void
155 clients_shutdown (Clients * cs)
156 {
157   Client *c;
158
159   for (c = cs->head; c; c = c->next)
160     {
161       c->dead++;
162     }
163
164
165   clients_reap (cs);
166 }