正在显示
1 个修改的文件
包含
120 行增加
和
0 行删除
| 1 | #include <unistd.h> | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | 3 | ||
| 4 | +#include <sys/types.h> | ||
| 5 | +#include <sys/socket.h> | ||
| 6 | +#include <arpa/inet.h> | ||
| 7 | +#include <signal.h> | ||
| 8 | +#include <sys/types.h> | ||
| 9 | +#include <sys/stat.h> | ||
| 10 | +#include <fcntl.h> | ||
| 11 | + | ||
| 4 | #include "public.h" | 12 | #include "public.h" |
| 5 | 13 | ||
| 6 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") | 14 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") |
| 7 | 15 | ||
| 16 | +int io_port = 1990; | ||
| 17 | + | ||
| 8 | st_mutex_t sync_start = NULL; | 18 | st_mutex_t sync_start = NULL; |
| 9 | st_cond_t sync_cond = NULL; | 19 | st_cond_t sync_cond = NULL; |
| 10 | st_mutex_t sync_mutex = NULL; | 20 | st_mutex_t sync_mutex = NULL; |
| @@ -58,6 +68,7 @@ void* sync_slave(void* arg) | @@ -58,6 +68,7 @@ void* sync_slave(void* arg) | ||
| 58 | 68 | ||
| 59 | int sync_test() | 69 | int sync_test() |
| 60 | { | 70 | { |
| 71 | + srs_trace("==================================================="); | ||
| 61 | srs_trace("sync test: start"); | 72 | srs_trace("sync test: start"); |
| 62 | 73 | ||
| 63 | if ((sync_start = st_mutex_new()) == NULL) { | 74 | if ((sync_start = st_mutex_new()) == NULL) { |
| @@ -100,6 +111,110 @@ int sync_test() | @@ -100,6 +111,110 @@ int sync_test() | ||
| 100 | return 0; | 111 | return 0; |
| 101 | } | 112 | } |
| 102 | 113 | ||
| 114 | +void* io_client(void* arg) | ||
| 115 | +{ | ||
| 116 | + | ||
| 117 | + int fd; | ||
| 118 | + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | ||
| 119 | + srs_trace("create linux socket error."); | ||
| 120 | + return NULL; | ||
| 121 | + } | ||
| 122 | + srs_trace("6. client create linux socket success. fd=%d", fd); | ||
| 123 | + | ||
| 124 | + st_netfd_t stfd; | ||
| 125 | + if ((stfd = st_netfd_open_socket(fd)) == NULL){ | ||
| 126 | + srs_trace("st_netfd_open_socket open socket failed."); | ||
| 127 | + return NULL; | ||
| 128 | + } | ||
| 129 | + srs_trace("7. client st open socket success. fd=%d", fd); | ||
| 130 | + | ||
| 131 | + struct sockaddr_in addr; | ||
| 132 | + addr.sin_family = AF_INET; | ||
| 133 | + addr.sin_port = htons(io_port); | ||
| 134 | + addr.sin_addr.s_addr = INADDR_ANY; | ||
| 135 | + if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1) { | ||
| 136 | + srs_trace("bind socket error."); | ||
| 137 | + return NULL; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + char buf[1024]; | ||
| 141 | + if (st_read_fully(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { | ||
| 142 | + srs_trace("st_read_fully failed"); | ||
| 143 | + return NULL; | ||
| 144 | + } | ||
| 145 | + if (st_write(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { | ||
| 146 | + srs_trace("st_write failed"); | ||
| 147 | + return NULL; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + return NULL; | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +int io_test() | ||
| 154 | +{ | ||
| 155 | + srs_trace("==================================================="); | ||
| 156 | + srs_trace("io test: start, port=%d", io_port); | ||
| 157 | + | ||
| 158 | + int fd; | ||
| 159 | + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | ||
| 160 | + srs_trace("create linux socket error."); | ||
| 161 | + return -1; | ||
| 162 | + } | ||
| 163 | + srs_trace("1. server create linux socket success. fd=%d", fd); | ||
| 164 | + | ||
| 165 | + int reuse_socket = 1; | ||
| 166 | + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) { | ||
| 167 | + srs_trace("setsockopt reuse-addr error."); | ||
| 168 | + return -1; | ||
| 169 | + } | ||
| 170 | + srs_trace("2. server setsockopt reuse-addr success. fd=%d", fd); | ||
| 171 | + | ||
| 172 | + struct sockaddr_in addr; | ||
| 173 | + addr.sin_family = AF_INET; | ||
| 174 | + addr.sin_port = htons(io_port); | ||
| 175 | + addr.sin_addr.s_addr = INADDR_ANY; | ||
| 176 | + if (bind(fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) { | ||
| 177 | + srs_trace("bind socket error."); | ||
| 178 | + return -1; | ||
| 179 | + } | ||
| 180 | + srs_trace("3. server bind socket success. fd=%d", fd); | ||
| 181 | + | ||
| 182 | + if (listen(fd, 10) == -1) { | ||
| 183 | + srs_trace("listen socket error."); | ||
| 184 | + return -1; | ||
| 185 | + } | ||
| 186 | + srs_trace("4. server listen socket success. fd=%d", fd); | ||
| 187 | + | ||
| 188 | + st_netfd_t stfd; | ||
| 189 | + if ((stfd = st_netfd_open_socket(fd)) == NULL){ | ||
| 190 | + srs_trace("st_netfd_open_socket open socket failed."); | ||
| 191 | + return -1; | ||
| 192 | + } | ||
| 193 | + srs_trace("5. server st open socket success. fd=%d", fd); | ||
| 194 | + | ||
| 195 | + if (!st_thread_create(io_client, NULL, 0, 0)) { | ||
| 196 | + srs_trace("st_thread_create failed"); | ||
| 197 | + return -1; | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT); | ||
| 201 | + srs_trace("8. server get a client. fd=%d", st_netfd_fileno(client_stfd)); | ||
| 202 | + | ||
| 203 | + char buf[1024]; | ||
| 204 | + if (st_write(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { | ||
| 205 | + srs_trace("st_write failed"); | ||
| 206 | + return -1; | ||
| 207 | + } | ||
| 208 | + if (st_read_fully(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { | ||
| 209 | + srs_trace("st_read_fully failed"); | ||
| 210 | + return -1; | ||
| 211 | + } | ||
| 212 | + srs_trace("9. server io completed."); | ||
| 213 | + | ||
| 214 | + srs_trace("io test: end"); | ||
| 215 | + return 0; | ||
| 216 | +} | ||
| 217 | + | ||
| 103 | int main(int argc, char** argv) | 218 | int main(int argc, char** argv) |
| 104 | { | 219 | { |
| 105 | if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { | 220 | if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { |
| @@ -117,6 +232,11 @@ int main(int argc, char** argv) | @@ -117,6 +232,11 @@ int main(int argc, char** argv) | ||
| 117 | return -1; | 232 | return -1; |
| 118 | } | 233 | } |
| 119 | 234 | ||
| 235 | + if (io_test() < 0) { | ||
| 236 | + srs_trace("io_test failed"); | ||
| 237 | + return -1; | ||
| 238 | + } | ||
| 239 | + | ||
| 120 | // cleanup. | 240 | // cleanup. |
| 121 | st_thread_exit(NULL); | 241 | st_thread_exit(NULL); |
| 122 | 242 |
-
请 注册 或 登录 后发表评论