multiserv.c
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <scot/socket.h>
#include <scot/socket_in.h>
#include <scot/stream.h>
#include <scot/stream_pool.h>
#include <scot/event.h>
#include <scot/event_listener.h>
#include <scot/exception.h>
#include <scot/scot_types.h>
#include <scot_common.h>
struct scot_stream_pool * con_pool;
struct scot_socket * sock;
struct scot_socket * con_sock;
void
on_exit_func (void)
{
scot_stream_pool_free (con_pool);
if (con_sock != NULL)
scot_socket_free (con_sock);
scot_socket_free (sock);
scot_socket_fini ();
exc_end ();
}
void
exit_func (int inum)
{
exit (0);
}
unsigned short
stream_read_cb (struct scot_event * _e)
{
SSIZE_T i, n;
char puffer[1024] = "";
struct scot_stream_pool_event * e = (struct scot_stream_pool_event *) _e;
n = scot_stream_read (e->st, puffer, 1024);
if (puffer [0] == 'q')
{
scot_stream_close (e->st);
return SCOT_EVENT_END;
}
printf ("Server: ");
for (i=0; i<n-1; i++)
printf ("%c=%02x ", puffer[i], puffer[i]);
printf ("\n");
fflush (stdout);
return SCOT_EVENT_CONT;
}
int
main (int argc, char * argv [])
{
excenv_t * ee;
scot_socket_init (2,2);
#ifndef WIN32
signal (SIGINT, exit_func);
#endif
atexit (on_exit_func);
TRY
{
con_pool = scot_stream_pool_new (con_pool);
scot_stream_pool_free_s (con_pool, SCOT_STREAM_POOL_FREE_STREAMS);
printf ("STREAM_POOL_READ_EVENT_NO: %d, %d\n",
(SCOT_EVENT_STREAM_POOL_READ >> 8),
((struct scot_event_listener *) con_pool)->group);
fflush (stdout);
scot_event_listener_register_cb (
(struct scot_event_listener *) con_pool,
SCOT_EVENT_STREAM_POOL_READ,
stream_read_cb, NULL);
scot_event_listener_start ((struct scot_event_listener *) con_pool);
}
CATCH (ee)
{
print_all_exceptions (ee);
exit (EXIT_FAILURE);
}
TRY
{
sock = scot_socket_in_new ("tcp", "7777");
scot_socket_listen (sock);
}
CATCH (ee)
{
exception_t *e;
while (e = retrive_exception (ee))
{
print_exception (e);
if (EXC_IN_THIS_TRY (e))
{
switch (exc_errnum_get (e))
{
case SCOT_SOCKET_LISTEN_FAIL:
scot_socket_free (sock);
case SCOT_SOCKET_NEW_FAIL:
break;
}
}
free_exception (e);
}
free_catched (ee);
}
TRY
{
while ((con_sock = scot_socket_accept (sock)) >= 0)
{
struct scot_stream * sock_stream;
int i, n;
char puffer [1024];
sock_stream = (struct scot_stream *) con_sock;
scot_stream_pool_add (
con_pool, sock_stream, SCOT_STREAM_POOL_FD_ADD_RMASK);
con_sock = NULL;
}
}
CATCH (ee)
{
exception_t *e;
while (e = retrive_exception (ee))
{
print_exception (e);
free_exception (e);
}
free_catched (ee);
}
scot_socket_free (sock);
scot_event_listener_stop ((struct scot_event_listener *) con_pool);
scot_stream_pool_free (con_pool);
return 0;
}