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
|
#define __EXTENSIONS__
#define _BSD_COMPAT
#include <lib9.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "styxserver.h"
#include "styxaux.h"
typedef struct Fdset Fdset;
struct Fdset
{
fd_set infds, outfds, excfds, r_infds, r_outfds, r_excfds;
};
int
styxinitsocket(void)
{
return 0;
}
void
styxendsocket(void)
{
}
void
styxclosesocket(int fd)
{
close(fd);
}
int
styxannounce(Styxserver *server, char *port)
{
struct sockaddr_in sin;
int s, one;
USED(server);
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0)
return s;
one = 1;
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0)
fprint(2, "setsockopt failed\n");
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(atoi(port));
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
close(s);
return -1;
}
if(listen(s, 20) < 0){
close(s);
return -1;
}
return s;
}
int
styxaccept(Styxserver *server)
{
struct sockaddr_in sin;
int s;
socklen_t len;
len = sizeof(sin);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
s = accept(server->connfd, (struct sockaddr *)&sin, &len);
if(s < 0){
if(errno != EINTR)
fprint(2, "error in accept: %s\n", strerror(errno));
}
return s;
}
void
styxinitwait(Styxserver *server)
{
Fdset *fs;
server->priv = fs = malloc(sizeof(Fdset));
FD_ZERO(&fs->infds);
FD_ZERO(&fs->outfds);
FD_ZERO(&fs->excfds);
FD_SET(server->connfd, &fs->infds);
}
int
styxnewcall(Styxserver *server)
{
Fdset *fs;
fs = server->priv;
return FD_ISSET(server->connfd, &fs->r_infds);
}
void
styxnewclient(Styxserver *server, int s)
{
Fdset *fs;
fs = server->priv;
FD_SET(s, &fs->infds);
}
void
styxfreeclient(Styxserver *server, int s)
{
Fdset *fs;
fs = server->priv;
FD_CLR(s, &fs->infds);
}
int
styxnewmsg(Styxserver *server, int s)
{
Fdset *fs;
fs = server->priv;
return FD_ISSET(s, &fs->r_infds) || FD_ISSET(s, &fs->r_excfds);
}
char*
styxwaitmsg(Styxserver *server)
{
struct timeval seltime;
int nfds;
Fdset *fs;
fs = server->priv;
fs->r_infds = fs->infds;
fs->r_outfds = fs->outfds;
fs->r_excfds = fs->excfds;
seltime.tv_sec = 10;
seltime.tv_usec = 0L;
nfds = select(sizeof(fd_set)*8, &fs->r_infds, &fs->r_outfds, &fs->r_excfds, &seltime);
if(nfds < 0 && errno != EINTR)
return"error in select";
return nil;
}
int
styxrecv(Styxserver *server, int fd, char *buf, int n, int m)
{
return recv(fd, buf, n, m);
}
int
styxsend(Styxserver *server, int fd, char *buf, int n, int m)
{
return send(fd, buf, n, m);
}
void
styxexit(int n)
{
exit(n);
}
|