srs.c
6.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "public.h"
#define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n")
int io_port = 1990;
st_mutex_t sync_start = NULL;
st_cond_t sync_cond = NULL;
st_mutex_t sync_mutex = NULL;
st_cond_t sync_end = NULL;
void* sync_master(void* arg)
{
// wait for main to sync_start this thread.
st_mutex_lock(sync_start);
st_mutex_unlock(sync_start);
st_usleep(100 * 1000);
st_cond_signal(sync_cond);
st_mutex_lock(sync_mutex);
srs_trace("2. st mutex is ok");
st_mutex_unlock(sync_mutex);
st_usleep(100 * 1000);
srs_trace("3. st thread is ok");
st_cond_signal(sync_cond);
return NULL;
}
void* sync_slave(void* arg)
{
// lock mutex to control thread.
st_mutex_lock(sync_mutex);
// wait for main to sync_start this thread.
st_mutex_lock(sync_start);
st_mutex_unlock(sync_start);
// wait thread to ready.
st_cond_wait(sync_cond);
srs_trace("1. st cond is ok");
// release mutex to control thread
st_usleep(100 * 1000);
st_mutex_unlock(sync_mutex);
// wait thread to exit.
st_cond_wait(sync_cond);
srs_trace("4. st is ok");
st_cond_signal(sync_end);
return NULL;
}
int sync_test()
{
srs_trace("===================================================");
srs_trace("sync test: start");
if ((sync_start = st_mutex_new()) == NULL) {
srs_trace("st_mutex_new sync_start failed");
return -1;
}
st_mutex_lock(sync_start);
if ((sync_cond = st_cond_new()) == NULL) {
srs_trace("st_cond_new cond failed");
return -1;
}
if ((sync_end = st_cond_new()) == NULL) {
srs_trace("st_cond_new end failed");
return -1;
}
if ((sync_mutex = st_mutex_new()) == NULL) {
srs_trace("st_mutex_new mutex failed");
return -1;
}
if (!st_thread_create(sync_master, NULL, 0, 0)) {
srs_trace("st_thread_create failed");
return -1;
}
if (!st_thread_create(sync_slave, NULL, 0, 0)) {
srs_trace("st_thread_create failed");
return -1;
}
// run all threads.
st_mutex_unlock(sync_start);
st_cond_wait(sync_end);
srs_trace("sync test: end");
return 0;
}
void* io_client(void* arg)
{
int fd;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
srs_trace("create linux socket error.");
return NULL;
}
srs_trace("6. client create linux socket success. fd=%d", fd);
st_netfd_t stfd;
if ((stfd = st_netfd_open_socket(fd)) == NULL){
srs_trace("st_netfd_open_socket open socket failed.");
return NULL;
}
srs_trace("7. client st open socket success. fd=%d", fd);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(io_port);
addr.sin_addr.s_addr = INADDR_ANY;
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1) {
srs_trace("bind socket error.");
return NULL;
}
char buf[1024];
if (st_read_fully(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
srs_trace("st_read_fully failed");
return NULL;
}
if (st_write(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
srs_trace("st_write failed");
return NULL;
}
return NULL;
}
int io_test()
{
srs_trace("===================================================");
srs_trace("io test: start, port=%d", io_port);
int fd;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
srs_trace("create linux socket error.");
return -1;
}
srs_trace("1. server create linux socket success. fd=%d", fd);
int reuse_socket = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
srs_trace("setsockopt reuse-addr error.");
return -1;
}
srs_trace("2. server setsockopt reuse-addr success. fd=%d", fd);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(io_port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) {
srs_trace("bind socket error.");
return -1;
}
srs_trace("3. server bind socket success. fd=%d", fd);
if (listen(fd, 10) == -1) {
srs_trace("listen socket error.");
return -1;
}
srs_trace("4. server listen socket success. fd=%d", fd);
st_netfd_t stfd;
if ((stfd = st_netfd_open_socket(fd)) == NULL){
srs_trace("st_netfd_open_socket open socket failed.");
return -1;
}
srs_trace("5. server st open socket success. fd=%d", fd);
if (!st_thread_create(io_client, NULL, 0, 0)) {
srs_trace("st_thread_create failed");
return -1;
}
st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
srs_trace("8. server get a client. fd=%d", st_netfd_fileno(client_stfd));
char buf[1024];
if (st_write(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
srs_trace("st_write failed");
return -1;
}
if (st_read_fully(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
srs_trace("st_read_fully failed");
return -1;
}
srs_trace("9. server io completed.");
srs_trace("io test: end");
return 0;
}
int main(int argc, char** argv)
{
if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) {
srs_trace("st_set_eventsys failed");
return -1;
}
if (st_init() < 0) {
srs_trace("st_init failed");
return -1;
}
if (sync_test() < 0) {
srs_trace("sync_test failed");
return -1;
}
if (io_test() < 0) {
srs_trace("io_test failed");
return -1;
}
// cleanup.
st_thread_exit(NULL);
return 0;
}