mirror of
https://github.com/tezc/sc.git
synced 2025-01-28 07:03:06 +08:00
commit
12355dca9e
@ -1020,15 +1020,7 @@ int sc_sock_poll_del(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
|
||||
struct epoll_event ep_ev = {.data.ptr = data,
|
||||
.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP};
|
||||
|
||||
if (fdt->op == SC_SOCK_NONE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((fdt->op & events) == 0) {
|
||||
|
||||
}
|
||||
|
||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1208,7 +1200,7 @@ int sc_sock_poll_del(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
|
||||
struct kevent ev[2];
|
||||
int mask = fdt->op & events;
|
||||
|
||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
||||
if (mask == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1402,7 +1394,7 @@ int sc_sock_poll_add(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
|
||||
int sc_sock_poll_del(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
|
||||
enum sc_sock_ev events, void *data)
|
||||
{
|
||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
||||
if ((fdt->op & events) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -5,28 +5,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <synchapi.h>
|
||||
#define sleep(n) (Sleep(n * 1000))
|
||||
#include <synchapi.h>
|
||||
#define sleep(n) (Sleep(n * 1000))
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#include <windows.h>
|
||||
#include <windows.h>
|
||||
|
||||
struct sc_thread
|
||||
{
|
||||
HANDLE id;
|
||||
void* (*fn)(void*);
|
||||
void* arg;
|
||||
void* ret;
|
||||
void *(*fn)(void *);
|
||||
void *arg;
|
||||
void *ret;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct sc_thread
|
||||
{
|
||||
@ -35,28 +36,28 @@ struct sc_thread
|
||||
|
||||
#endif
|
||||
|
||||
void sc_thread_init(struct sc_thread* thread);
|
||||
int sc_thread_term(struct sc_thread* thread);
|
||||
int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg);
|
||||
int sc_thread_stop(struct sc_thread* thread, void** ret);
|
||||
void sc_thread_init(struct sc_thread *thread);
|
||||
int sc_thread_term(struct sc_thread *thread);
|
||||
int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg);
|
||||
int sc_thread_stop(struct sc_thread *thread, void **ret);
|
||||
|
||||
void sc_thread_init(struct sc_thread* thread)
|
||||
void sc_thread_init(struct sc_thread *thread)
|
||||
{
|
||||
thread->id = 0;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <process.h>
|
||||
#include <process.h>
|
||||
|
||||
unsigned int __stdcall sc_thread_fn(void* arg)
|
||||
unsigned int __stdcall sc_thread_fn(void *arg)
|
||||
{
|
||||
struct sc_thread* thread = arg;
|
||||
struct sc_thread *thread = arg;
|
||||
|
||||
thread->ret = thread->fn(thread->arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
||||
int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg)
|
||||
{
|
||||
int rc;
|
||||
|
||||
@ -64,13 +65,13 @@ int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
||||
thread->arg = arg;
|
||||
|
||||
thread->id =
|
||||
(HANDLE)_beginthreadex(NULL, 0, sc_thread_fn, thread, 0, NULL);
|
||||
(HANDLE) _beginthreadex(NULL, 0, sc_thread_fn, thread, 0, NULL);
|
||||
rc = thread->id == 0 ? -1 : 0;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int sc_thread_stop(struct sc_thread* thread, void** ret)
|
||||
int sc_thread_stop(struct sc_thread *thread, void **ret)
|
||||
{
|
||||
int rc = 0;
|
||||
DWORD rv;
|
||||
@ -99,7 +100,7 @@ int sc_thread_stop(struct sc_thread* thread, void** ret)
|
||||
}
|
||||
#else
|
||||
|
||||
int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
||||
int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg)
|
||||
{
|
||||
int rc;
|
||||
pthread_attr_t hndl;
|
||||
@ -121,10 +122,10 @@ int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
||||
return rc;
|
||||
}
|
||||
|
||||
int sc_thread_stop(struct sc_thread* thread, void** ret)
|
||||
int sc_thread_stop(struct sc_thread *thread, void **ret)
|
||||
{
|
||||
int rc;
|
||||
void* val;
|
||||
void *val;
|
||||
|
||||
if (thread->id == 0) {
|
||||
return -1;
|
||||
@ -142,12 +143,12 @@ int sc_thread_stop(struct sc_thread* thread, void** ret)
|
||||
|
||||
#endif
|
||||
|
||||
int sc_thread_term(struct sc_thread* thread)
|
||||
int sc_thread_term(struct sc_thread *thread)
|
||||
{
|
||||
return sc_thread_stop(thread, NULL);
|
||||
}
|
||||
|
||||
void* server_ip4(void* arg)
|
||||
void *server_ip4(void *arg)
|
||||
{
|
||||
char buf[5];
|
||||
char tmp[128];
|
||||
@ -168,13 +169,17 @@ void* server_ip4(void* arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* client_ip4(void* arg)
|
||||
void *client_ip4(void *arg)
|
||||
{
|
||||
int rc;
|
||||
char tmp[128];
|
||||
struct sc_sock sock;
|
||||
|
||||
sc_sock_init(&sock, 0, true, AF_INET);
|
||||
assert(sc_sock_set_sndtimeo(&sock, 10000) != 0);
|
||||
printf("%s \n", sc_sock_error(&sock));
|
||||
assert(sc_sock_set_rcvtimeo(&sock, 10000) != 0);
|
||||
printf("%s \n", sc_sock_error(&sock));
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
rc = sc_sock_connect(&sock, "127.0.0.1", "8004", NULL, NULL);
|
||||
@ -187,6 +192,8 @@ void* client_ip4(void* arg)
|
||||
|
||||
assert(rc == 0);
|
||||
sc_sock_print(&sock, tmp, sizeof(tmp));
|
||||
assert(sc_sock_set_sndtimeo(&sock, 10000) == 0);
|
||||
assert(sc_sock_set_rcvtimeo(&sock, 10000) == 0);
|
||||
assert(sc_sock_send(&sock, "test", 5, 0) == 5);
|
||||
assert(sc_sock_term(&sock) == 0);
|
||||
|
||||
@ -208,7 +215,7 @@ void test_ip4()
|
||||
assert(sc_thread_term(&thread2) == 0);
|
||||
}
|
||||
|
||||
void* server_ip6(void* arg)
|
||||
void *server_ip6(void *arg)
|
||||
{
|
||||
char buf[5];
|
||||
char tmp[128];
|
||||
@ -228,7 +235,7 @@ void* server_ip6(void* arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* client_ip6(void* arg)
|
||||
void *client_ip6(void *arg)
|
||||
{
|
||||
int rc;
|
||||
struct sc_sock sock;
|
||||
@ -266,7 +273,7 @@ void test_ip6()
|
||||
assert(sc_thread_term(&thread2) == 0);
|
||||
}
|
||||
|
||||
void* server_unix(void* arg)
|
||||
void *server_unix(void *arg)
|
||||
{
|
||||
char buf[5];
|
||||
char tmp[128];
|
||||
@ -291,7 +298,7 @@ void* server_unix(void* arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* client_unix(void* arg)
|
||||
void *client_unix(void *arg)
|
||||
{
|
||||
int rc;
|
||||
struct sc_sock sock;
|
||||
@ -414,7 +421,7 @@ void pipe_fail_test()
|
||||
#endif
|
||||
|
||||
|
||||
void* server(void* arg)
|
||||
void *server(void *arg)
|
||||
{
|
||||
int write_triggered = 2;
|
||||
int rc, received = 0;
|
||||
@ -441,7 +448,7 @@ void* server(void* arg)
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
int ev = sc_sock_poll_event(&poll, i);
|
||||
struct sc_sock* sock = sc_sock_poll_data(&poll, i);
|
||||
struct sc_sock *sock = sc_sock_poll_data(&poll, i);
|
||||
|
||||
if (ev == 0) {
|
||||
continue;
|
||||
@ -453,27 +460,23 @@ void* server(void* arg)
|
||||
printf("accepted \n");
|
||||
|
||||
assert(rc == 0);
|
||||
assert(sc_sock_poll_add(&poll, &accepted.fdt,
|
||||
SC_SOCK_WRITE,
|
||||
assert(sc_sock_poll_add(&poll, &accepted.fdt, SC_SOCK_WRITE,
|
||||
&accepted) == 0);
|
||||
}
|
||||
|
||||
if (ev & SC_SOCK_WRITE) {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (ev & SC_SOCK_READ) {
|
||||
char buf[8];
|
||||
rc = sc_sock_recv(&accepted, buf, sizeof(buf), 0);
|
||||
if (rc == 8) {
|
||||
assert(strcmp(buf, "dataxxx") == 0);
|
||||
received++;
|
||||
}
|
||||
else if (rc == 0 || rc < 0) {
|
||||
} else if (rc == 0 || rc < 0) {
|
||||
rc = sc_sock_poll_del(&poll, &accepted.fdt,
|
||||
SC_SOCK_READ |
|
||||
SC_SOCK_WRITE,
|
||||
SC_SOCK_READ | SC_SOCK_WRITE,
|
||||
&accepted);
|
||||
assert(rc == 0);
|
||||
rc = sc_sock_term(&accepted);
|
||||
@ -486,11 +489,12 @@ void* server(void* arg)
|
||||
if (write_triggered > 0) {
|
||||
write_triggered--;
|
||||
if (write_triggered == 0) {
|
||||
rc = sc_sock_poll_del(&poll, &accepted.fdt, SC_SOCK_WRITE, &accepted);
|
||||
rc = sc_sock_poll_del(&poll, &accepted.fdt,
|
||||
SC_SOCK_WRITE, &accepted);
|
||||
assert(rc == 0);
|
||||
}
|
||||
else {
|
||||
rc = sc_sock_poll_add(&poll, &accepted.fdt, SC_SOCK_READ, &accepted);
|
||||
} else {
|
||||
rc = sc_sock_poll_add(&poll, &accepted.fdt,
|
||||
SC_SOCK_READ, &accepted);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -507,7 +511,7 @@ void* server(void* arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* client(void* arg)
|
||||
void *client(void *arg)
|
||||
{
|
||||
int rc;
|
||||
struct sc_sock sock;
|
||||
@ -550,6 +554,40 @@ void test_poll()
|
||||
assert(sc_thread_term(&thread2) == 0);
|
||||
}
|
||||
|
||||
void test_err()
|
||||
{
|
||||
struct sc_sock sock;
|
||||
char buf[128];
|
||||
|
||||
sc_sock_init(&sock, 0, true, AF_INET);
|
||||
assert(sc_sock_listen(&sock, "127.0.0.1x", "8004") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_UNIX);
|
||||
assert(sc_sock_listen(&sock, "/", "8004") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET6);
|
||||
assert(sc_sock_listen(&sock, "/", "8004") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET6);
|
||||
assert(sc_sock_listen(&sock, "0.0.0.0", "99999") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET);
|
||||
assert(sc_sock_listen(&sock, "0.0.0.3", "99999") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET6);
|
||||
assert(sc_sock_connect(&sock, "::1", "8006", NULL, NULL) != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_UNIX);
|
||||
assert(sc_sock_connect(&sock, "/", "8006", NULL, NULL) != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET);
|
||||
assert(sc_sock_connect(&sock, "0.0.0.0", "8006", NULL, NULL) != 0);
|
||||
sc_sock_print(&sock, buf, 128);
|
||||
sc_sock_local_str(&sock, buf, 128);
|
||||
assert(*buf == '\0');
|
||||
sc_sock_remote_str(&sock, buf, 128);
|
||||
assert(*buf == '\0');
|
||||
|
||||
sc_sock_init(&sock, 0, true, AF_INET);
|
||||
assert(sc_sock_connect(&sock, "0.0.0.0", "8006", "127.0.0.1", "8080") != 0);
|
||||
sc_sock_init(&sock, 0, true, AF_INET6);
|
||||
assert(sc_sock_connect(&sock, "0.0.0.0", "8006", "::1", "8080") != 0);
|
||||
assert(sc_sock_term(&sock) == 0);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
@ -573,6 +611,7 @@ int main()
|
||||
test_pipe();
|
||||
pipe_fail_test();
|
||||
test_poll();
|
||||
test_err();
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
rc = WSACleanup();
|
||||
|
@ -170,18 +170,18 @@ void sc_timer_cancel(struct sc_timer *timer, uint64_t *id)
|
||||
*id = SC_TIMER_INVALID;
|
||||
}
|
||||
|
||||
#define sc_timer_min(a, b) (a) < (b) ? (a) : (b)
|
||||
|
||||
uint64_t sc_timer_timeout(struct sc_timer *timer, uint64_t timestamp, void *arg,
|
||||
void (*callback)(void *, uint64_t, uint64_t, void *))
|
||||
{
|
||||
#define min(a, b) (a) < (b) ? (a) : (b)
|
||||
|
||||
const uint64_t time = timestamp - timer->timestamp;
|
||||
uint32_t wheel, base;
|
||||
uint32_t head = timer->head;
|
||||
uint32_t wheels = (uint32_t) (min(time / TICK, WHEEL_COUNT));
|
||||
uint32_t wheels = (uint32_t) (sc_timer_min(time / TICK, WHEEL_COUNT));
|
||||
|
||||
if (wheels == 0) {
|
||||
return min(TICK - time, TICK);
|
||||
return sc_timer_min(TICK - time, TICK);
|
||||
}
|
||||
|
||||
timer->timestamp = timestamp;
|
||||
@ -211,5 +211,5 @@ uint64_t sc_timer_timeout(struct sc_timer *timer, uint64_t timestamp, void *arg,
|
||||
head = (head + 1) & (WHEEL_COUNT - 1);
|
||||
}
|
||||
|
||||
return min(TICK - time, TICK);
|
||||
return sc_timer_min(TICK - time, TICK);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user