save pollfd offset in socket_t
[cnet.git] / test.c
blobcbd6776ced84bfe4c00696ff93b5fb763441bd29
1 /* Copyright (c) 2007 Zachery Hostens <zacheryph@gmail.com>
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include "cnet.h"
29 #define TESTSERVER_HOST "127.0.0.1"
30 #define TESTSERVER_PORT 64930
32 void sigpipe_handler (int err) {
33 printf ("DEBUG: recieved a SIGPIPE!!!\n");
36 /* test server: echo's anything it recieves to client & output */
37 #ifdef TESTSERVER
38 int ts_on_read (int sid, void *conn_data, char *data, int len);
39 int ts_on_eof (int sid, void *conn_data, int err);
40 int ts_on_close (int sid, void *conn_data);
41 int ts_on_newclient (int sid, void *conn_data, int newsid, char *host, int port);
43 cnet_handler_t testserver_handler = {
44 NULL, ts_on_read, ts_on_eof, ts_on_close, ts_on_newclient
47 int ts_on_read (int sid, void *conn_data, char *data, int len)
49 cnprintf (sid, "From SERVER: Got your Message !\n");
50 return 0;
53 int ts_on_eof (int sid, void *conn_data, int err)
55 return 0;
58 int ts_on_close (int sid, void *conn_data)
60 printf ("SERVER: on_close sid:%d\n", sid);
61 return 0;
64 int ts_on_newclient (int sid, void *conn_data, int newsid, char *host, int port)
66 printf ("SERVER: new_client sid:%d\n", newsid);
67 cnet_handler (newsid, &testserver_handler);
68 cnet_linemode (newsid, 1);
69 return 0;
72 int main (const int argc, const char **argv)
74 int ssid, ret;
76 signal (SIGPIPE, sigpipe_handler);
78 ssid = cnet_listen (TESTSERVER_HOST, TESTSERVER_PORT);
79 if (-1 == ssid) {
80 printf ("SERVER: ERROR failed to connect errno:%d\n", errno);
81 printf ("SERVER: %s\n", strerror(errno));
82 return errno;
85 cnet_handler (ssid, &testserver_handler);
86 printf ("SERVER: OUR PID: %d\n", getpid());
87 printf ("SERVER: started server sid:%d\n", ssid);
89 for (;;) {
90 ret = cnet_select (5000);
91 if (-1 == ret) {
92 printf ("SERVER: we are broken(%d): %s\n", errno, strerror(errno));
93 break;
97 return 0;
99 #endif
102 /* test client: starts x clients and echo's random text to server */
103 #ifdef TESTCLIENT
104 int tc_on_connect (int sid, void *conn_data)
106 printf ("CLIENT: on_connect sid:%d\n", sid);
107 return 0;
110 int tc_on_read (int sid, void *conn_data, char *data, int len)
112 return 0;
115 int tc_on_eof (int sid, void *conn_data, int err)
117 return 0;
120 int tc_on_close (int sid, void *conn_data)
122 printf ("CLIENT: on_close sid:%d\n", sid);
123 exit(0);
126 cnet_handler_t testclient_handler = {
127 tc_on_connect, tc_on_read, tc_on_eof, tc_on_close, NULL
130 int main (const int argc, const char **argv)
132 int i, sid;
134 sid = cnet_connect (TESTSERVER_HOST, TESTSERVER_PORT, NULL, 0);
135 cnet_handler (sid, &testclient_handler);
136 if (0 > sid) {
137 printf ("CLIENT: ERROR cnet_connect failed: %d\n", sid);
138 printf ("perror: %s\n", strerror(errno));
139 return 0;
142 for (i = 0; i < 2; i++) {
143 cnprintf (sid, "my formatted: %d --> %s\r\nOur second Info....\n", i, "Format String");
144 cnet_select(2000);
145 sleep(1);
147 cnet_close (sid);
149 return 0;
151 #endif