ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/contrib/gen_echoserver.C
Revision: 1.5
Committed: Sun Sep 16 18:54:43 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +10 -5 lines
Log Message:
#defines to enum

File Contents

# User Rev Content
1 pippijn 1.5 /**
2     * gen_echoserver.C: An echo server. (proof of concept for integrated XMLRPC HTTPD)
3     *
4     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5     * Rights to this code are as documented in COPYING.
6     *
7     *
8     * Portions of this file were derived from sources bearing the following license:
9 pippijn 1.3 * Copyright © 2005 William Pitcock <nenolod -at- nenolod.net>
10 pippijn 1.2 * Rights to this code are as documented in doc/pod/license.pod.
11 pippijn 1.1 *
12 pippijn 1.5 * $Id: gen_echoserver.C,v 1.4 2007-08-30 19:56:22 pippijn Exp $
13 pippijn 1.1 */
14    
15     #include "atheme.h"
16 pippijn 1.3 #include <ermyth/module.h>
17 pippijn 1.1 #include "datastream.h"
18 pippijn 1.4 #include "connection.h"
19 pippijn 1.1
20 pippijn 1.5 static char const rcsid[] = "$Id: gen_echoserver.C,v 1.4 2007-08-30 19:56:22 pippijn Exp $";
21 pippijn 1.1
22 pippijn 1.3 REGISTER_MODULE ("contrib/gen_echoserver", false, "William Pitcock <nenolod -at- nenolod.net>");
23    
24     static connection_t *listener;
25 pippijn 1.1
26     static int
27     my_read (connection_t *cptr, char *buf)
28     {
29     int n;
30    
31     if ((n = read (cptr->fd, buf, BUFSIZE)) > 0)
32     {
33     buf[n] = '\0';
34     cnt.bin += n;
35     }
36    
37     return n;
38     }
39    
40     static void
41     do_packet (connection_t *cptr, char *buf)
42     {
43     char *ptr, buf2[BUFSIZE * 2];
44     static char tmp[BUFSIZE * 2 + 1];
45    
46     while ((ptr = strchr (buf, '\n')))
47     {
48     snprintf (buf2, (BUFSIZE * 2), "%s%s", tmp, buf);
49     *tmp = '\0';
50    
51     slog (LG_DEBUG, "-{incoming}-> %s", buf2);
52 pippijn 1.4 cptr->write_raw (buf2);
53 pippijn 1.1
54     buf = ptr + 1;
55     }
56    
57     if (*buf)
58     {
59     strlcpy (tmp, buf, BUFSIZE * 2);
60     tmp[BUFSIZE * 2] = '\0';
61     }
62     }
63    
64     static void
65     my_rhandler (connection_t *cptr)
66     {
67     char buf[BUFSIZE * 2];
68    
69     if (!my_read (cptr, buf))
70 pippijn 1.4 cptr->close ();
71 pippijn 1.1
72     do_packet (cptr, buf);
73     }
74    
75     static void
76     do_listen (connection_t *cptr)
77     {
78 pippijn 1.4 connection_t::accept_tcp (cptr, my_rhandler, sendq_flush);
79 pippijn 1.1 slog (LG_DEBUG, "do_listen(): accepted %d", cptr->fd);
80     }
81    
82 pippijn 1.3 bool
83     _modinit (module *m)
84 pippijn 1.1 {
85 pippijn 1.4 listener = connection_t::open_listener_tcp ("127.0.0.1", 7100, do_listen);
86 pippijn 1.3
87     return true;
88 pippijn 1.1 }
89    
90     void
91     _moddeinit (void)
92     {
93 pippijn 1.4 listener->close ();
94 pippijn 1.1 }