/[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.6 - (show 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 #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(void *client_data, int idx, char *data, int len)
10 {
11 char buf[512];
12 int i, buflen;
13
14 //printf("read %d from %d\n", iobuf->len, idx);
15 data[len] = 0;
16 snprintf(buf, sizeof(buf), "<%d> %s\n", idx, data);
17 buf[sizeof(buf)-1] = 0;
18 //printf("%s", buf);
19 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 static int client_eof(void *client_data, int idx, int err, const char *errmsg)
29 {
30 char buf[128];
31 int i, buflen;
32
33 printf("eof from %d (%s)\n", idx, errmsg);
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 sprintf(buf, "%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_handler_t client_handler = {
49 "client",
50 NULL, client_eof, NULL,
51 client_read, NULL
52 };
53
54 static int server_newclient(void *client_data, int idx, int newsock, const char *peer_ip, int peer_port)
55 {
56 char buf[512];
57 int buflen, newidx, i;
58
59 socket_set_nonblock(newsock, 1);
60 newidx = sockbuf_new();
61 sockbuf_set_sock(newidx, newsock, 0);
62 sslmode_on(newidx, 1);
63 zipmode_on(newidx);
64 linemode_on(newidx);
65 sockbuf_set_handler(newidx, &client_handler, NULL);
66
67 sockbuf_write(newidx, "Hello!\n", 7);
68 printf("New connection %d\n", newidx);
69 sprintf(buf, "New connection %d from %s %d\n", newidx, peer_ip, peer_port);
70 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 static sockbuf_handler_t server_handler = {
81 "server",
82 NULL, NULL, server_newclient,
83 NULL, NULL
84 };
85
86 main (int argc, char *argv[])
87 {
88 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
98 signal(SIGPIPE, SIG_IGN);
99 srand(time(0));
100 sslmode_init();
101 sock = socket_create(NULL, 0, host, port, SOCKET_SERVER);
102 if (sock < 0) {
103 perror("socket_create");
104 return(0);
105 }
106 idx = sockbuf_new();
107 sockbuf_set_sock(idx, sock, SOCKBUF_SERVER);
108 sockbuf_set_handler(idx, &server_handler, NULL);
109 while (1) {
110 sockbuf_update_all(-1);
111 }
112 return(0);
113 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23