/[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.8 - (show annotations) (download) (as text)
Sun Dec 14 19:38:28 2003 UTC (15 years, 3 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +2 -2 lines
File MIME type: text/x-chdr
* More fixes for make dist.. now works on techmonkeys

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 newidx, const char *peer_ip, int peer_port)
55 {
56 char buf[512];
57 int buflen, i;
58
59 //sslmode_on(newidx, 1);
60 //zipmode_on(newidx);
61 linemode_on(newidx);
62 sockbuf_set_handler(newidx, &client_handler, NULL);
63
64 sockbuf_write(newidx, "Hello!\n", 7);
65 printf("New connection %d\n", newidx);
66 sprintf(buf, "New connection %d from %s %d\n", newidx, peer_ip, peer_port);
67 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 static sockbuf_handler_t server_handler = {
78 "server",
79 NULL, NULL, server_newclient,
80 NULL, NULL
81 };
82
83 main (int argc, char *argv[])
84 {
85 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
95 signal(SIGPIPE, SIG_IGN);
96 srand(time(0));
97 sslmode_init();
98 sock = socket_create(NULL, 0, host, port, SOCKET_SERVER);
99 if (sock < 0) {
100 perror("socket_create");
101 return(0);
102 }
103 idx = sockbuf_new();
104 sockbuf_set_sock(idx, sock, SOCKBUF_SERVER);
105 sockbuf_set_handler(idx, &server_handler, NULL);
106 while (1) {
107 sockbuf_update_all(-1);
108 }
109 return(0);
110 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23