add UDP support, and TCP listen support ipv6 now

This commit is contained in:
Cloud Wu
2014-11-12 23:00:44 +08:00
parent fa78623b1c
commit cfe9506a5a
7 changed files with 773 additions and 98 deletions

View File

@@ -91,6 +91,9 @@ skynet_socket_poll() {
case SOCKET_ACCEPT:
forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result);
break;
case SOCKET_UDP:
forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result);
break;
default:
skynet_error(NULL, "Unknown socket message type %d.",type);
return -1;
@@ -101,9 +104,8 @@ skynet_socket_poll() {
return 1;
}
int
skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) {
int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz);
static int
check_wsz(struct skynet_context *ctx, int id, void *buffer, int64_t wsz) {
if (wsz < 0) {
skynet_free(buffer);
return -1;
@@ -116,6 +118,12 @@ skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) {
return 0;
}
int
skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) {
int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz);
return check_wsz(ctx, id, buffer, wsz);
}
void
skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) {
socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz);
@@ -155,3 +163,33 @@ void
skynet_socket_nodelay(struct skynet_context *ctx, int id) {
socket_server_nodelay(SOCKET_SERVER, id);
}
int
skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) {
uint32_t source = skynet_context_handle(ctx);
return socket_server_udp(SOCKET_SERVER, source, addr, port);
}
int
skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port) {
return socket_server_udp_connect(SOCKET_SERVER, id, addr, port);
}
int
skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) {
int64_t wsz = socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz);
return check_wsz(ctx, id, (void *)buffer, wsz);
}
const char *
skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) {
if (msg->type != SKYNET_SOCKET_TYPE_UDP) {
return NULL;
}
struct socket_message sm;
sm.id = msg->id;
sm.opaque = 0;
sm.ud = msg->ud;
sm.data = msg->buffer;
return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz);
}