/[cvs]/eggdrop1.9/testcode/chat.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/chat.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sun Mar 3 18:14:14 2002 UTC (18 years, 1 month ago) by stdarg
Branch: MAIN
Changes since 1.3: +12 -4 lines
File MIME type: text/x-chdr
*** empty log message ***

1 #include <stdio.h>
2 #include <signal.h>
3 #include "my_socket.h"
4 #include "sockbuf.h"
5
6 static int *idx_array = NULL;
7 static int array_len = 0;
8
9 static int client_read(int idx, sockbuf_iobuf_t *iobuf, void *client_data)
10 {
11 char buf[128];
12 int i, buflen;
13
14 //printf("read %d from %d\n", iobuf->len, idx);
15 iobuf->data[iobuf->len] = 0;
16 snprintf(buf, 128, "<%d> %s\n", idx, iobuf->data);
17 //printf("%s", buf);
18 buflen = strlen(buf);
19 iobuf->data[iobuf->len++] = '\n';
20 for (i = 0; i < array_len; i++) {
21 if (idx_array[i] != idx) {
22 sockbuf_write(idx_array[i], buf, buflen);
23 }
24 }
25 return(0);
26 }
27
28 static int client_eof(int idx, void *ignore, void *client_data)
29 {
30 char buf[128];
31 int i, buflen;
32
33 printf("eof from %d\n", idx);
34 for (i = 0; i < array_len; i++) {
35 if (idx_array[i] == idx) break;
36 }
37 array_len--;
38 memmove(idx_array+i, idx_array+i+1, sizeof(int) * (array_len-i));
39 sockbuf_delete(idx);
40 snprintf(buf, 128, "%d disconnected\n", idx);
41 buflen = strlen(buf);
42 for (i = 0; i < array_len; i++) {
43 sockbuf_write(idx_array[i], buf, buflen);
44 }
45 return(0);
46 }
47
48 static sockbuf_event_t client_handler = {
49 (Function) 4,
50 (Function) "client",
51 client_read,
52 NULL,
53 client_eof,
54 NULL
55 };
56
57 static int server_read(int idx, int serversock, void *client_data)
58 {
59 char buf[128];
60 int buflen, newsock, newidx, i;
61
62 newsock = accept(serversock, NULL, NULL);
63 if (newsock < 0) return(0);
64
65 socket_set_nonblock(newsock, 1);
66 newidx = sockbuf_new(newsock, 0);
67 sslmode_on(newidx, 1);
68 zipmode_on(newidx);
69 linemode_on(newidx);
70 sockbuf_set_handler(newidx, client_handler, NULL);
71
72 sockbuf_write(newidx, "Hello!\n", 7);
73 printf("New connection %d\n", newidx);
74 snprintf(buf, 128, "New connection %d\n", newidx);
75 buflen = strlen(buf);
76 for (i = 0; i < array_len; i++) {
77 sockbuf_write(idx_array[i], buf, buflen);
78 }
79 idx_array = (int *)realloc(idx_array, sizeof(int) * (array_len+1));
80 idx_array[array_len] = newidx;
81 array_len++;
82 return(0);
83 }
84
85 static sockbuf_event_t server_handler = {
86 (Function) 1,
87 (Function) "server",
88 server_read
89 };
90
91 main (int argc, char *argv[])
92 {
93 int sock, idx, port;
94 char *host;
95
96 if (argc > 1) host = argv[1];
97 else host = NULL;
98 if (argc > 2) port = atoi(argv[2]);
99 else port = 12345;
100
101 if (host && !strcmp(host, "-")) host = NULL;
102
103 signal(SIGPIPE, SIG_IGN);
104 srand(time(0));
105 sslmode_init();
106 sock = socket_create(host, port, SOCKET_SERVER);
107 if (sock < 0) {
108 perror("socket_create");
109 return(0);
110 }
111 idx = sockbuf_new(sock, SOCKBUF_SERVER);
112 sockbuf_set_handler(idx, server_handler, NULL);
113 while (1) {
114 sockbuf_update_all(-1);
115 }
116 return(0);
117 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23