/[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.3 - (show annotations) (download) (as text)
Tue Jan 15 06:11:38 2002 UTC (18 years, 2 months ago) by stdarg
Branch: MAIN
Changes since 1.2: +2 -1 lines
File MIME type: text/x-chdr
* Added client/server option to sslmode.c and got rid of auto-negotation.
* Experimented with preset dictionaries in zipmode.c... not very successful.
* Cleaned up some stuff.

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 ()
92 {
93 int sock, idx;
94
95 signal(SIGPIPE, SIG_IGN);
96 srand(time(0));
97 sslmode_init();
98 sock = socket_create(NULL, 12345, SOCKET_SERVER);
99 if (sock < 0) {
100 perror("socket_create");
101 return(0);
102 }
103 idx = sockbuf_new(sock, SOCKBUF_SERVER);
104 sockbuf_set_handler(idx, server_handler, NULL);
105 while (1) {
106 sockbuf_update_all(-1);
107 }
108 return(0);
109 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23