mirror of
https://github.com/tezc/sc.git
synced 2025-02-04 07:13:10 +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,
|
struct epoll_event ep_ev = {.data.ptr = data,
|
||||||
.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP};
|
.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP};
|
||||||
|
|
||||||
if (fdt->op == SC_SOCK_NONE) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((fdt->op & events) == 0) {
|
if ((fdt->op & events) == 0) {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
|
||||||
return 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];
|
struct kevent ev[2];
|
||||||
int mask = fdt->op & events;
|
int mask = fdt->op & events;
|
||||||
|
|
||||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
if (mask == 0) {
|
||||||
return 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,
|
int sc_sock_poll_del(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
|
||||||
enum sc_sock_ev events, void *data)
|
enum sc_sock_ev events, void *data)
|
||||||
{
|
{
|
||||||
if (fdt->op == SC_SOCK_NONE || (fdt->op & events) == 0) {
|
if ((fdt->op & events) == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,28 +5,29 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#include <synchapi.h>
|
#include <synchapi.h>
|
||||||
#define sleep(n) (Sleep(n * 1000))
|
#define sleep(n) (Sleep(n * 1000))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
struct sc_thread
|
struct sc_thread
|
||||||
{
|
{
|
||||||
HANDLE id;
|
HANDLE id;
|
||||||
void* (*fn)(void*);
|
void *(*fn)(void *);
|
||||||
void* arg;
|
void *arg;
|
||||||
void* ret;
|
void *ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
struct sc_thread
|
struct sc_thread
|
||||||
{
|
{
|
||||||
@ -35,28 +36,28 @@ struct sc_thread
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void sc_thread_init(struct sc_thread* thread);
|
void sc_thread_init(struct sc_thread *thread);
|
||||||
int sc_thread_term(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_start(struct sc_thread *thread, void *(*fn)(void *), void *arg);
|
||||||
int sc_thread_stop(struct sc_thread* thread, void** ret);
|
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;
|
thread->id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#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);
|
thread->ret = thread->fn(thread->arg);
|
||||||
return 0;
|
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;
|
int rc;
|
||||||
|
|
||||||
@ -64,13 +65,13 @@ int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
|||||||
thread->arg = arg;
|
thread->arg = arg;
|
||||||
|
|
||||||
thread->id =
|
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;
|
rc = thread->id == 0 ? -1 : 0;
|
||||||
|
|
||||||
return rc;
|
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;
|
int rc = 0;
|
||||||
DWORD rv;
|
DWORD rv;
|
||||||
@ -99,7 +100,7 @@ int sc_thread_stop(struct sc_thread* thread, void** ret)
|
|||||||
}
|
}
|
||||||
#else
|
#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;
|
int rc;
|
||||||
pthread_attr_t hndl;
|
pthread_attr_t hndl;
|
||||||
@ -121,10 +122,10 @@ int sc_thread_start(struct sc_thread* thread, void* (*fn)(void*), void* arg)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sc_thread_stop(struct sc_thread* thread, void** ret)
|
int sc_thread_stop(struct sc_thread *thread, void **ret)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
void* val;
|
void *val;
|
||||||
|
|
||||||
if (thread->id == 0) {
|
if (thread->id == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -142,12 +143,12 @@ int sc_thread_stop(struct sc_thread* thread, void** ret)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int sc_thread_term(struct sc_thread* thread)
|
int sc_thread_term(struct sc_thread *thread)
|
||||||
{
|
{
|
||||||
return sc_thread_stop(thread, NULL);
|
return sc_thread_stop(thread, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* server_ip4(void* arg)
|
void *server_ip4(void *arg)
|
||||||
{
|
{
|
||||||
char buf[5];
|
char buf[5];
|
||||||
char tmp[128];
|
char tmp[128];
|
||||||
@ -168,13 +169,17 @@ void* server_ip4(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* client_ip4(void* arg)
|
void *client_ip4(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
char tmp[128];
|
char tmp[128];
|
||||||
struct sc_sock sock;
|
struct sc_sock sock;
|
||||||
|
|
||||||
sc_sock_init(&sock, 0, true, AF_INET);
|
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++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
rc = sc_sock_connect(&sock, "127.0.0.1", "8004", NULL, NULL);
|
rc = sc_sock_connect(&sock, "127.0.0.1", "8004", NULL, NULL);
|
||||||
@ -187,6 +192,8 @@ void* client_ip4(void* arg)
|
|||||||
|
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
sc_sock_print(&sock, tmp, sizeof(tmp));
|
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_send(&sock, "test", 5, 0) == 5);
|
||||||
assert(sc_sock_term(&sock) == 0);
|
assert(sc_sock_term(&sock) == 0);
|
||||||
|
|
||||||
@ -208,7 +215,7 @@ void test_ip4()
|
|||||||
assert(sc_thread_term(&thread2) == 0);
|
assert(sc_thread_term(&thread2) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* server_ip6(void* arg)
|
void *server_ip6(void *arg)
|
||||||
{
|
{
|
||||||
char buf[5];
|
char buf[5];
|
||||||
char tmp[128];
|
char tmp[128];
|
||||||
@ -228,7 +235,7 @@ void* server_ip6(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* client_ip6(void* arg)
|
void *client_ip6(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
struct sc_sock sock;
|
struct sc_sock sock;
|
||||||
@ -266,7 +273,7 @@ void test_ip6()
|
|||||||
assert(sc_thread_term(&thread2) == 0);
|
assert(sc_thread_term(&thread2) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* server_unix(void* arg)
|
void *server_unix(void *arg)
|
||||||
{
|
{
|
||||||
char buf[5];
|
char buf[5];
|
||||||
char tmp[128];
|
char tmp[128];
|
||||||
@ -291,7 +298,7 @@ void* server_unix(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* client_unix(void* arg)
|
void *client_unix(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
struct sc_sock sock;
|
struct sc_sock sock;
|
||||||
@ -414,7 +421,7 @@ void pipe_fail_test()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void* server(void* arg)
|
void *server(void *arg)
|
||||||
{
|
{
|
||||||
int write_triggered = 2;
|
int write_triggered = 2;
|
||||||
int rc, received = 0;
|
int rc, received = 0;
|
||||||
@ -441,7 +448,7 @@ void* server(void* arg)
|
|||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
int ev = sc_sock_poll_event(&poll, 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) {
|
if (ev == 0) {
|
||||||
continue;
|
continue;
|
||||||
@ -453,27 +460,23 @@ void* server(void* arg)
|
|||||||
printf("accepted \n");
|
printf("accepted \n");
|
||||||
|
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
assert(sc_sock_poll_add(&poll, &accepted.fdt,
|
assert(sc_sock_poll_add(&poll, &accepted.fdt, SC_SOCK_WRITE,
|
||||||
SC_SOCK_WRITE,
|
|
||||||
&accepted) == 0);
|
&accepted) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ev & SC_SOCK_WRITE) {
|
if (ev & SC_SOCK_WRITE) {
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (ev & SC_SOCK_READ) {
|
if (ev & SC_SOCK_READ) {
|
||||||
char buf[8];
|
char buf[8];
|
||||||
rc = sc_sock_recv(&accepted, buf, sizeof(buf), 0);
|
rc = sc_sock_recv(&accepted, buf, sizeof(buf), 0);
|
||||||
if (rc == 8) {
|
if (rc == 8) {
|
||||||
assert(strcmp(buf, "dataxxx") == 0);
|
assert(strcmp(buf, "dataxxx") == 0);
|
||||||
received++;
|
received++;
|
||||||
}
|
} else if (rc == 0 || rc < 0) {
|
||||||
else if (rc == 0 || rc < 0) {
|
|
||||||
rc = sc_sock_poll_del(&poll, &accepted.fdt,
|
rc = sc_sock_poll_del(&poll, &accepted.fdt,
|
||||||
SC_SOCK_READ |
|
SC_SOCK_READ | SC_SOCK_WRITE,
|
||||||
SC_SOCK_WRITE,
|
|
||||||
&accepted);
|
&accepted);
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
rc = sc_sock_term(&accepted);
|
rc = sc_sock_term(&accepted);
|
||||||
@ -486,11 +489,12 @@ void* server(void* arg)
|
|||||||
if (write_triggered > 0) {
|
if (write_triggered > 0) {
|
||||||
write_triggered--;
|
write_triggered--;
|
||||||
if (write_triggered == 0) {
|
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);
|
assert(rc == 0);
|
||||||
}
|
} else {
|
||||||
else {
|
rc = sc_sock_poll_add(&poll, &accepted.fdt,
|
||||||
rc = sc_sock_poll_add(&poll, &accepted.fdt, SC_SOCK_READ, &accepted);
|
SC_SOCK_READ, &accepted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -507,7 +511,7 @@ void* server(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* client(void* arg)
|
void *client(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
struct sc_sock sock;
|
struct sc_sock sock;
|
||||||
@ -550,6 +554,40 @@ void test_poll()
|
|||||||
assert(sc_thread_term(&thread2) == 0);
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
@ -573,6 +611,7 @@ int main()
|
|||||||
test_pipe();
|
test_pipe();
|
||||||
pipe_fail_test();
|
pipe_fail_test();
|
||||||
test_poll();
|
test_poll();
|
||||||
|
test_err();
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
rc = WSACleanup();
|
rc = WSACleanup();
|
||||||
|
@ -170,18 +170,18 @@ void sc_timer_cancel(struct sc_timer *timer, uint64_t *id)
|
|||||||
*id = SC_TIMER_INVALID;
|
*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,
|
uint64_t sc_timer_timeout(struct sc_timer *timer, uint64_t timestamp, void *arg,
|
||||||
void (*callback)(void *, uint64_t, uint64_t, void *))
|
void (*callback)(void *, uint64_t, uint64_t, void *))
|
||||||
{
|
{
|
||||||
#define min(a, b) (a) < (b) ? (a) : (b)
|
|
||||||
|
|
||||||
const uint64_t time = timestamp - timer->timestamp;
|
const uint64_t time = timestamp - timer->timestamp;
|
||||||
uint32_t wheel, base;
|
uint32_t wheel, base;
|
||||||
uint32_t head = timer->head;
|
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) {
|
if (wheels == 0) {
|
||||||
return min(TICK - time, TICK);
|
return sc_timer_min(TICK - time, TICK);
|
||||||
}
|
}
|
||||||
|
|
||||||
timer->timestamp = timestamp;
|
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);
|
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