/[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.6 - (hide annotations) (download) (as text)
Wed Jun 19 04:51:54 2002 UTC (17 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.5: +26 -30 lines
File MIME type: text/x-chdr
* I think the sockbuf api is almost done

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.6 static int server_newclient(void *client_data, int idx, int newsock, const char *peer_ip, int peer_port)
55 stdarg 1.1 {
56 stdarg 1.6 char buf[512];
57     int buflen, newidx, i;
58 stdarg 1.1
59     socket_set_nonblock(newsock, 1);
60 stdarg 1.6 newidx = sockbuf_new();
61     sockbuf_set_sock(newidx, newsock, 0);
62 stdarg 1.3 sslmode_on(newidx, 1);
63 stdarg 1.1 zipmode_on(newidx);
64     linemode_on(newidx);
65 stdarg 1.6 sockbuf_set_handler(newidx, &client_handler, NULL);
66 stdarg 1.1
67 stdarg 1.4 sockbuf_write(newidx, "Hello!\n", 7);
68 stdarg 1.1 printf("New connection %d\n", newidx);
69 stdarg 1.6 sprintf(buf, "New connection %d from %s %d\n", newidx, peer_ip, peer_port);
70 stdarg 1.1 buflen = strlen(buf);
71     for (i = 0; i < array_len; i++) {
72     sockbuf_write(idx_array[i], buf, buflen);
73     }
74     idx_array = (int *)realloc(idx_array, sizeof(int) * (array_len+1));
75     idx_array[array_len] = newidx;
76     array_len++;
77     return(0);
78     }
79    
80 stdarg 1.6 static sockbuf_handler_t server_handler = {
81     "server",
82     NULL, NULL, server_newclient,
83     NULL, NULL
84 stdarg 1.1 };
85    
86 stdarg 1.4 main (int argc, char *argv[])
87 stdarg 1.1 {
88 stdarg 1.4 int sock, idx, port;
89     char *host;
90    
91     if (argc > 1) host = argv[1];
92     else host = NULL;
93     if (argc > 2) port = atoi(argv[2]);
94     else port = 12345;
95    
96     if (host && !strcmp(host, "-")) host = NULL;
97 stdarg 1.1
98 stdarg 1.2 signal(SIGPIPE, SIG_IGN);
99     srand(time(0));
100     sslmode_init();
101 stdarg 1.5 sock = socket_create(NULL, 0, host, port, SOCKET_SERVER);
102 stdarg 1.1 if (sock < 0) {
103     perror("socket_create");
104     return(0);
105     }
106 stdarg 1.6 idx = sockbuf_new();
107     sockbuf_set_sock(idx, sock, SOCKBUF_SERVER);
108     sockbuf_set_handler(idx, &server_handler, NULL);
109 stdarg 1.1 while (1) {
110     sockbuf_update_all(-1);
111     }
112     return(0);
113     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23