/[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.7 - (hide annotations) (download) (as text)
Fri Aug 9 23:42:04 2002 UTC (17 years, 7 months ago) by stdarg
Branch: MAIN
Changes since 1.6: +2 -5 lines
File MIME type: text/x-chdr
* Some updates to network code

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 stdarg 1.6 static int client_read(void *client_data, int idx, char *data, int len)
10 stdarg 1.1 {
11 stdarg 1.6 char buf[512];
12 stdarg 1.1 int i, buflen;
13    
14 stdarg 1.2 //printf("read %d from %d\n", iobuf->len, idx);
15 stdarg 1.6 data[len] = 0;
16     snprintf(buf, sizeof(buf), "<%d> %s\n", idx, data);
17     buf[sizeof(buf)-1] = 0;
18 stdarg 1.2 //printf("%s", buf);
19 stdarg 1.1 buflen = strlen(buf);
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 stdarg 1.6 static int client_eof(void *client_data, int idx, int err, const char *errmsg)
29 stdarg 1.1 {
30     char buf[128];
31     int i, buflen;
32    
33 stdarg 1.6 printf("eof from %d (%s)\n", idx, errmsg);
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 stdarg 1.6 sprintf(buf, "%d disconnected\n", idx);
41 stdarg 1.1 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 stdarg 1.6 static sockbuf_handler_t client_handler = {
49     "client",
50     NULL, client_eof, NULL,
51     client_read, NULL
52 stdarg 1.1 };
53    
54 stdarg 1.7 static int server_newclient(void *client_data, int idx, int newidx, const char *peer_ip, int peer_port)
55 stdarg 1.1 {
56 stdarg 1.6 char buf[512];
57 stdarg 1.7 int buflen, i;
58 stdarg 1.1
59 stdarg 1.3 sslmode_on(newidx, 1);
60 stdarg 1.1 zipmode_on(newidx);
61     linemode_on(newidx);
62 stdarg 1.6 sockbuf_set_handler(newidx, &client_handler, NULL);
63 stdarg 1.1
64 stdarg 1.4 sockbuf_write(newidx, "Hello!\n", 7);
65 stdarg 1.1 printf("New connection %d\n", newidx);
66 stdarg 1.6 sprintf(buf, "New connection %d from %s %d\n", newidx, peer_ip, peer_port);
67 stdarg 1.1 buflen = strlen(buf);
68     for (i = 0; i < array_len; i++) {
69     sockbuf_write(idx_array[i], buf, buflen);
70     }
71     idx_array = (int *)realloc(idx_array, sizeof(int) * (array_len+1));
72     idx_array[array_len] = newidx;
73     array_len++;
74     return(0);
75     }
76    
77 stdarg 1.6 static sockbuf_handler_t server_handler = {
78     "server",
79     NULL, NULL, server_newclient,
80     NULL, NULL
81 stdarg 1.1 };
82    
83 stdarg 1.4 main (int argc, char *argv[])
84 stdarg 1.1 {
85 stdarg 1.4 int sock, idx, port;
86     char *host;
87    
88     if (argc > 1) host = argv[1];
89     else host = NULL;
90     if (argc > 2) port = atoi(argv[2]);
91     else port = 12345;
92    
93     if (host && !strcmp(host, "-")) host = NULL;
94 stdarg 1.1
95 stdarg 1.2 signal(SIGPIPE, SIG_IGN);
96     srand(time(0));
97     sslmode_init();
98 stdarg 1.5 sock = socket_create(NULL, 0, host, port, SOCKET_SERVER);
99 stdarg 1.1 if (sock < 0) {
100     perror("socket_create");
101     return(0);
102     }
103 stdarg 1.6 idx = sockbuf_new();
104     sockbuf_set_sock(idx, sock, SOCKBUF_SERVER);
105     sockbuf_set_handler(idx, &server_handler, NULL);
106 stdarg 1.1 while (1) {
107     sockbuf_update_all(-1);
108     }
109     return(0);
110     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23