| 1 |
/* talksrvr.c: this program implements the TalkServer. |
| 2 |
* |
| 3 |
* The server here also acts as a pass-through: all |
| 4 |
* other clients get told what any client said. |
| 5 |
* |
| 6 |
* Author: Dr Charles E Campbell, Jr |
| 7 |
* |
| 8 |
* Protocol |
| 9 |
* |
| 10 |
* server->client |
| 11 |
* m#...message message from #>0: client-id #==0: server |
| 12 |
* q tell client to quit |
| 13 |
* |
| 14 |
* client->server |
| 15 |
* m...message message from client |
| 16 |
* q client is quitting |
| 17 |
* Q client tells server to quit |
| 18 |
*/ |
| 19 |
#include <stdio.h> |
| 20 |
#include <ctype.h> |
| 21 |
#include "xtdio.h" |
| 22 |
#include "sockets.h" |
| 23 |
|
| 24 |
/* ------------------------------------------------------------------------ |
| 25 |
* Definitions: |
| 26 |
*/ |
| 27 |
#define BUFSIZE 256 |
| 28 |
#define SERVER "TalkServer" |
| 29 |
#define PROMPT "Server: " |
| 30 |
|
| 31 |
/* ------------------------------------------------------------------------ |
| 32 |
* Typedefs: |
| 33 |
*/ |
| 34 |
typedef struct Accept_str Accept; |
| 35 |
|
| 36 |
/* ------------------------------------------------------------------------ |
| 37 |
* Local Data Structures: |
| 38 |
*/ |
| 39 |
struct Accept_str { |
| 40 |
int id; |
| 41 |
Socket *skt; |
| 42 |
Accept *nxt; |
| 43 |
Accept *prv; |
| 44 |
}; |
| 45 |
|
| 46 |
/* ------------------------------------------------------------------------ |
| 47 |
* Global Data: |
| 48 |
*/ |
| 49 |
Accept *ahd= NULL; /* accept socket list head */ |
| 50 |
Accept *atl= NULL; /* accept socket list tail */ |
| 51 |
|
| 52 |
/* ------------------------------------------------------------------------ |
| 53 |
* Explanation: |
| 54 |
*/ |
| 55 |
|
| 56 |
/* ------------------------------------------------------------------------ |
| 57 |
* Prototypes: |
| 58 |
*/ |
| 59 |
int main( int, char **); /* talkserver.c */ |
| 60 |
void RemoveAccpt(Accept *); /* talkserver.c */ |
| 61 |
void SendAllbut(Accept *,char *); /* talkserver.c */ |
| 62 |
|
| 63 |
/* ------------------------------------------------------------------------ |
| 64 |
* Source Code: |
| 65 |
*/ |
| 66 |
|
| 67 |
/* main: */ |
| 68 |
int main( |
| 69 |
int argc, |
| 70 |
char **argv) |
| 71 |
{ |
| 72 |
Accept *accpt = NULL; |
| 73 |
Accept *accptnxt = NULL; |
| 74 |
Socket *srvr = NULL; |
| 75 |
char buf[BUFSIZE]; |
| 76 |
int didwork = 1; |
| 77 |
int doquit = 0; |
| 78 |
int result; |
| 79 |
static int id = 0; |
| 80 |
|
| 81 |
rdcolor(); |
| 82 |
|
| 83 |
/* attempt to open server */ |
| 84 |
srvr= Sopen(SERVER,"S"); |
| 85 |
|
| 86 |
/* this probably happens only when the PortMaster isn't up */ |
| 87 |
if(!srvr) error(XTDIO_ERROR,"unable to open <%s%s%s> server\n",YELLOW,SERVER,GREEN); |
| 88 |
Smaskset(srvr); /* enter server into mask */ |
| 89 |
Smaskfdset(fileno(stdin)); /* enter stdin into mask */ |
| 90 |
|
| 91 |
fputs(PROMPT,stdout); /* print out prompt */ |
| 92 |
fflush(stdout); /* force stdout out */ |
| 93 |
|
| 94 |
while(!doquit && didwork) { |
| 95 |
result = Smaskwait(); |
| 96 |
didwork = 0; |
| 97 |
|
| 98 |
if(result > 0) { /* if error, returns -1 */ |
| 99 |
|
| 100 |
if(Stest(srvr) > 0) { /* if error, returns -1 */ |
| 101 |
Socket *skt; |
| 102 |
|
| 103 |
didwork = 1; /* only indicate didwork when something was done */ |
| 104 |
/* accept a new Socket */ |
| 105 |
skt= Saccept(srvr); |
| 106 |
if(!skt) { |
| 107 |
error(XTDIO_WARNING,"server tested having data but unable to accept!\n"); |
| 108 |
break; |
| 109 |
} |
| 110 |
Smaskset(skt); |
| 111 |
|
| 112 |
/* allocate and doubly-link in new Accept */ |
| 113 |
accpt= (Accept *) malloc(sizeof(Accept)); |
| 114 |
if(atl) atl->nxt= accpt; |
| 115 |
else ahd = accpt; |
| 116 |
accpt->prv= atl; |
| 117 |
accpt->nxt= NULL; |
| 118 |
atl = accpt; |
| 119 |
|
| 120 |
/* initialize */ |
| 121 |
accpt->skt= skt; |
| 122 |
accpt->id = ++id; |
| 123 |
Smaskset(accpt->skt); |
| 124 |
} |
| 125 |
|
| 126 |
else { |
| 127 |
/* check out all accepted clients for any work to do */ |
| 128 |
for(accpt= ahd, accptnxt= NULL; accpt && !doquit; accpt= accptnxt) { |
| 129 |
accptnxt= accpt->nxt; /* note that RemoveAccpt may delete this accpt! */ |
| 130 |
if(accpt->skt) { |
| 131 |
result = Stest(accpt->skt); |
| 132 |
|
| 133 |
if(result > 0) { |
| 134 |
if(Sgets(buf,BUFSIZE,accpt->skt)) { |
| 135 |
char *b; |
| 136 |
Accept *snd; |
| 137 |
int fromid; |
| 138 |
didwork = 1; /* only indicate didwork when something was done */ |
| 139 |
|
| 140 |
/* translate command from client */ |
| 141 |
switch(buf[0]) { |
| 142 |
|
| 143 |
case 'q': /* client is quitting */ |
| 144 |
RemoveAccpt(accpt); |
| 145 |
break; |
| 146 |
|
| 147 |
case 'Q': /* client told server to quit */ |
| 148 |
doquit= 1; |
| 149 |
RemoveAccpt(accpt); |
| 150 |
break; |
| 151 |
|
| 152 |
case 'm': /* message from client */ |
| 153 |
for(b= buf+1; *b && isspace(*b); ++b) ; /* skip "m ..." */ |
| 154 |
printf("\nclient#%d: %s\n",accpt->id,b); |
| 155 |
SendAllbut(accpt,b); /* send message to all other clients */ |
| 156 |
fputs(PROMPT,stdout); /* print out prompt */ |
| 157 |
fflush(stdout); /* force stdout out */ |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
else RemoveAccpt(accpt); |
| 162 |
} |
| 163 |
else if(result < 0) RemoveAccpt(accpt); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/* ok, didn't do anything with Sockets, so must be stdin */ |
| 169 |
if(!didwork) { |
| 170 |
didwork= 1; |
| 171 |
|
| 172 |
fgets(buf,BUFSIZE,stdin); /* get string from stdin */ |
| 173 |
srmtrblk(buf); /* remove trailing whitespace */ |
| 174 |
|
| 175 |
if(!strcmp(buf,"QUIT")) { /* user command server to QUIT */ |
| 176 |
break; |
| 177 |
} |
| 178 |
|
| 179 |
SendAllbut((Accept *) NULL,buf); /* send to ALL accept Sockets */ |
| 180 |
fputs(PROMPT,stdout); /* print out prompt */ |
| 181 |
fflush(stdout); /* force stdout out */ |
| 182 |
} |
| 183 |
} |
| 184 |
else break; |
| 185 |
} |
| 186 |
|
| 187 |
/* close everything down */ |
| 188 |
while(ahd) RemoveAccpt(ahd); |
| 189 |
Sclose(srvr); |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
/* --------------------------------------------------------------------- */ |
| 195 |
|
| 196 |
/* RemoveAccpt: this function removes given Accept* from linked list */ |
| 197 |
void RemoveAccpt(Accept *accpt) |
| 198 |
{ |
| 199 |
/* sanity check */ |
| 200 |
if(!accpt || !accpt->skt) { |
| 201 |
error(XTDIO_SEVERE,"RemoveAccpt told to remove %s!\n", |
| 202 |
accpt? "accpt with null socket" : "null accpt"); |
| 203 |
} |
| 204 |
|
| 205 |
/* tell client to quit */ |
| 206 |
Sputs("q",accpt->skt); |
| 207 |
|
| 208 |
/* de-link */ |
| 209 |
if(accpt->prv) accpt->prv->nxt= accpt->nxt; |
| 210 |
else ahd = accpt->nxt; |
| 211 |
if(accpt->nxt) accpt->nxt->prv= accpt->prv; |
| 212 |
else atl = accpt->prv; |
| 213 |
|
| 214 |
Smaskunset(accpt->skt); /* remove from mask */ |
| 215 |
Sclose(accpt->skt); /* close socket */ |
| 216 |
free((char *) accpt); /* delete Accept */ |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/* --------------------------------------------------------------------- */ |
| 221 |
|
| 222 |
/* SendAllbut: this function sends a message to all clients except the |
| 223 |
* one on the argument list |
| 224 |
*/ |
| 225 |
void SendAllbut(Accept *notme,char *msg) |
| 226 |
{ |
| 227 |
Accept *accpt= NULL; |
| 228 |
|
| 229 |
for(accpt= ahd; accpt; accpt= accpt->nxt) if(accpt != notme) { |
| 230 |
Sprintf(accpt->skt,"m%d %s",notme? notme->id : 0,msg); |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/* --------------------------------------------------------------------- */ |