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

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

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


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

1 stdarg 1.1 #include <stdio.h>
2 stdarg 1.2 #include <signal.h>
3 stdarg 1.1 #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 stdarg 1.2 //printf("read %d from %d\n", iobuf->len, idx);
15 stdarg 1.1 iobuf->data[iobuf->len] = 0;
16     snprintf(buf, 128, "<%d> %s\n", idx, iobuf->data);
17 stdarg 1.2 //printf("%s", buf);
18 stdarg 1.1 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 stdarg 1.2 printf("eof from %d\n", idx);
34 stdarg 1.1 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 stdarg 1.3 sslmode_on(newidx, 1);
68 stdarg 1.1 zipmode_on(newidx);
69     linemode_on(newidx);
70     sockbuf_set_handler(newidx, client_handler, NULL);
71    
72 stdarg 1.4 sockbuf_write(newidx, "Hello!\n", 7);
73 stdarg 1.1 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 stdarg 1.4 main (int argc, char *argv[])
92 stdarg 1.1 {
93 stdarg 1.4 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 stdarg 1.1
103 stdarg 1.2 signal(SIGPIPE, SIG_IGN);
104     srand(time(0));
105     sslmode_init();
106 stdarg 1.4 sock = socket_create(host, port, SOCKET_SERVER);
107 stdarg 1.1 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