/[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.2 - (show annotations) (download) (as text)
Thu Jan 10 16:43:51 2002 UTC (18 years, 2 months ago) by stdarg
Branch: MAIN
Changes since 1.1: +8 -0 lines
File MIME type: text/x-chdr
* Initial ssl code (doesn't work perfectly)
* Example of using ssl, zip, and linemode filters stacked
* Makefile to generate DSA key/certificate pair (you can use RSA too)

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);
68 zipmode_on(newidx);
69 linemode_on(newidx);
70 sockbuf_set_handler(newidx, client_handler, NULL);
71
72 printf("New connection %d\n", newidx);
73 snprintf(buf, 128, "New connection %d\n", newidx);
74 buflen = strlen(buf);
75 for (i = 0; i < array_len; i++) {
76 sockbuf_write(idx_array[i], buf, buflen);
77 }
78 idx_array = (int *)realloc(idx_array, sizeof(int) * (array_len+1));
79 idx_array[array_len] = newidx;
80 array_len++;
81 return(0);
82 }
83
84 static sockbuf_event_t server_handler = {
85 (Function) 1,
86 (Function) "server",
87 server_read
88 };
89
90 main ()
91 {
92 int sock, idx;
93
94 signal(SIGPIPE, SIG_IGN);
95 srand(time(0));
96 sslmode_init();
97 sock = socket_create(NULL, 12345, SOCKET_SERVER);
98 if (sock < 0) {
99 perror("socket_create");
100 return(0);
101 }
102 idx = sockbuf_new(sock, SOCKBUF_SERVER);
103 sockbuf_set_handler(idx, server_handler, NULL);
104 while (1) {
105 sockbuf_update_all(-1);
106 }
107 return(0);
108 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23