int init_socket(unsigned int loc_idx) { struct sockaddr_in v4addr; int status; status = uv_udp_init(loop,&udp_servers[loc_idx]); #if UV_VERSION_MAJOR == 0 v4addr = uv_ip4_addr(profile_socket[loc_idx].host, atoi(profile_socket[loc_idx].port)); #else status = uv_ip4_addr(profile_socket[loc_idx].host, atoi(profile_socket[loc_idx].port), &v4addr); #endif #if UV_VERSION_MAJOR == 0 status = uv_udp_bind(&udp_servers[loc_idx], v4addr,0); #else status = uv_udp_bind(&udp_servers[loc_idx], (struct sockaddr*)&v4addr, UV_UDP_REUSEADDR); #endif if(status < 0) { LERR( "capture: bind error"); return 2; } udp_servers[loc_idx].data = (void *) &loc_idx; status = uv_udp_recv_start(&udp_servers[loc_idx], on_alloc, on_recv); return 0; }
int udprelay_start(uv_loop_t *loop, struct server_context *server) { int rc; if (server->dest_addr->sa_family == AF_INET6) { addrlen = IPV6_HEADER_LEN; } uv_udp_init(loop, &server->udp); if ((rc = uv_udp_open(&server->udp, server->udp_fd))) { logger_stderr("udp open error: %s", uv_strerror(rc)); return 1; } rc = uv_udp_bind(&server->udp, server->local_addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("bind error: %s", uv_strerror(rc)); return 1; } uv_udp_recv_start(&server->udp, client_alloc_cb, client_recv_cb); return 0; }
int udp_start(struct tundev_context *ctx, uv_loop_t *loop) { int rc; ctx->network_buffer = malloc(ctx->tun->mtu + PRIMITIVE_BYTES); uv_udp_init(loop, &ctx->inet_udp); ctx->inet_udp_fd = create_socket(SOCK_DGRAM, mode == xTUN_SERVER ? 1 : 0); if ((rc = uv_udp_open(&ctx->inet_udp, ctx->inet_udp_fd))) { logger_log(LOG_ERR, "udp open error: %s", uv_strerror(rc)); exit(1); } #ifdef ANDROID rc = protect_socket(ctx->inet_udp_fd); logger_log(rc ? LOG_INFO : LOG_ERR, "Protect socket %s", rc ? "successful" : "failed"); #endif if (mode == xTUN_SERVER) { rc = uv_udp_bind(&ctx->inet_udp, &ctx->tun->addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("udp bind error: %s", uv_strerror(rc)); exit(1); } } return uv_udp_recv_start(&ctx->inet_udp, inet_alloc_cb, inet_recv_cb); }
static int udp_listener() { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", server_port); struct sockaddr sockname; int namelen; int r; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_udp_bind(&udpServer, addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; } memset(&sockname, -1, sizeof sockname); namelen = sizeof sockname; r = uv_udp_getsockname(&udpServer, &sockname, &namelen); ASSERT(r == 0); check_sockname(&sockname, "0.0.0.0", server_port, "udp listener socket"); getsocknamecount++; r = uv_udp_recv_start(&udpServer, alloc, udp_recv); ASSERT(r == 0); return 0; }
static PyObject * UDP_func_start_recv(UDP *self, PyObject *args) { int r; PyObject *tmp, *callback; tmp = NULL; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "O:start_recv", &callback)) { return NULL; } if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable is required"); return NULL; } r = uv_udp_recv_start((uv_udp_t *)UV_HANDLE(self), (uv_alloc_cb)on_udp_alloc, (uv_udp_recv_cb)on_udp_read); if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); return NULL; } tmp = self->on_read_cb; Py_INCREF(callback); self->on_read_cb = callback; Py_XDECREF(tmp); Py_RETURN_NONE; }
CAMLprim value uwt_udp_recv_start(value o_udp, value o_cb) { HANDLE_INIT2_NO_UNINIT(u, o_udp, o_cb); /* uninit allowed, but forbidden by me :D */ value ret; if ( u->cb_read != CB_INVALID ){ ret = VAL_UWT_INT_RESULT_EBUSY; } else { int erg = 0; uv_udp_t* ux = (uv_udp_t*)u->handle; if ( u->can_reuse_cb_read == 1 ){ erg = uv_udp_recv_stop(ux); } if ( erg >= 0 ){ erg = uv_udp_recv_start(ux, uwt__alloc_cb, uwt_udp_recv_cb); if ( erg >= 0 ){ u->c_read_size = DEF_ALLOC_SIZE; uwt__gr_register(&u->cb_read,o_cb); ++u->in_use_cnt; } u->can_reuse_cb_read = 0; u->read_waiting = 0; } ret = VAL_UWT_UNIT_RESULT(erg); } CAMLreturn(ret); }
void cjdnsadmin_start(cjdnsadmin_t *adm) { struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_ADDRCONFIG; struct addrinfo* res = 0; printf("Connecting to %s:%s\n",adm->host,adm->port); int err = getaddrinfo(adm->host, adm->port, &hints, &res); if (err != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err)); return; } uv_udp_init(uv_default_loop(),&adm->handle); adm->handle.data = adm; uv_connect_t* request = malloc(sizeof(uv_connect_t)); request->data = res; printf("connected to cjdns admin at %s\n", sprint_addrport(res->ai_addr)); adm->theaddr = (struct sockaddr*) malloc(res->ai_addrlen); memcpy(adm->theaddr, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); uv_udp_recv_start(&adm->handle,alloc_buffer,on_read); }
int io_start_read(uv_handle_t *handle) { if (handle->type == UV_UDP) { return uv_udp_recv_start((uv_udp_t *)handle, &handle_getbuf, &udp_recv); } else { return uv_read_start((uv_stream_t *)handle, &handle_getbuf, &tcp_recv); } }
static void client_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { struct server_context *server = container_of(handle, struct server_context, udp); if (nread > 0) { char key[KEY_BYTES + 1] = {0}; crypto_generickey((uint8_t *)key, sizeof(key) -1, (uint8_t*)addr, sizeof(*addr), NULL, 0); struct client_context *client = NULL; uv_mutex_lock(&mutex); cache_lookup(cache, key, (void *)&client); uv_mutex_unlock(&mutex); if (client == NULL) { client = new_client(); client->addr = *addr; client->local_handle = handle; memcpy(client->key, key, sizeof(key)); uv_timer_init(handle->loop, client->timer); uv_udp_init(handle->loop, &client->server_handle); client->server_handle.data = client; uv_udp_recv_start(&client->server_handle, server_alloc_cb, server_recv_cb); uv_mutex_lock(&mutex); cache_insert(cache, client->key, (void *)client); uv_mutex_unlock(&mutex); } int clen = nread + PRIMITIVE_BYTES + addrlen; int mlen = nread + addrlen; uint8_t *c = (uint8_t *)buf->base - PRIMITIVE_BYTES - addrlen; uint8_t *m = (uint8_t *)buf->base - addrlen; if (server->dest_addr->sa_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)server->dest_addr; m[0] = 1; memcpy(m + 1, &addr->sin_addr, 4); memcpy(m + 1 + 4, &addr->sin_port, 2); } else { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)server->dest_addr; m[0] = 4; memcpy(m + 1, &addr->sin6_addr, 16); memcpy(m + 1 + 16, &addr->sin6_port, 2); } int rc = crypto_encrypt(c, m, mlen); if (!rc) { reset_timer(client); forward_to_server(server->server_addr, client, c, clen); } } else { goto error; } return; error: free(buf->base - addrlen - PRIMITIVE_BYTES); }
static int luv_udp_recv_start(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); int ret; luv_check_callback(L, (luv_handle_t*)handle->data, LUV_RECV, 2); ret = uv_udp_recv_start(handle, luv_alloc_cb, luv_udp_recv_cb); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
//Initialize the Linux-specific part of the context. All is related to //libmnl/netfilter struct neat_ctx *nt_linux_init_ctx(struct neat_ctx *ctx) { //TODO: Consider allocator function if ((ctx->mnl_rcv_buf = calloc(1, MNL_SOCKET_BUFFER_SIZE)) == NULL) { nt_log(ctx, NEAT_LOG_ERROR, "Failed to allocate netlink buffer", __func__); return NULL; } //Configure netlink and start requesting addresses if ((ctx->mnl_sock = mnl_socket_open(NETLINK_ROUTE)) == NULL) { nt_log(ctx, NEAT_LOG_ERROR, "Failed to allocate netlink socket", __func__); return NULL; } if (mnl_socket_bind(ctx->mnl_sock, (1 << (RTNLGRP_IPV4_IFADDR - 1)) | (1 << (RTNLGRP_IPV6_IFADDR - 1)), 0)) { nt_log(ctx, NEAT_LOG_ERROR, "Failed to bind netlink socket", __func__); return NULL; } //We need to build a list of all available source addresses as soon as //possible. It is started here if (neat_linux_request_addrs(ctx->mnl_sock) <= 0) { nt_log(ctx, NEAT_LOG_ERROR, "Failed to request addresses", __func__); return NULL; } //Add socket to event loop if (uv_udp_init(ctx->loop, &(ctx->uv_nl_handle))) { nt_log(ctx, NEAT_LOG_ERROR, "Failed to initialize uv UDP handle", __func__); return NULL; } //TODO: We could use offsetof, but libuv has a pointer so ... ctx->uv_nl_handle.data = ctx; if (uv_udp_open(&(ctx->uv_nl_handle), mnl_socket_get_fd(ctx->mnl_sock))) { nt_log(ctx, NEAT_LOG_ERROR, "Could not add netlink socket to uv", __func__); return NULL; } if (uv_udp_recv_start(&(ctx->uv_nl_handle), neat_linux_nl_alloc, nt_linux_nl_recv)) { nt_log(ctx, NEAT_LOG_ERROR, "Could not start receiving netlink packets", __func__); return NULL; } ctx->cleanup = nt_linux_cleanup; #ifdef MPTCP_SUPPORT linux_read_sys_mptcp_enabled(ctx); #endif // MPTCP_SUPPORT //Configure netlink socket, add to event loop and start dumping return ctx; }
int UvUdpSocket::asyncReadStart(void* buffer, int size) { m_buffer = buffer; m_bufferSize = size; m_uv_udp->data = this; return uv_udp_recv_start( m_uv_udp, UvSocket::on_alloc_recv, UvUdpSocket::udp_recv_cb); }
int luv_udp_recv_start(lua_State* L) { uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp"); int rc = uv_udp_recv_start(handle, luv_on_alloc, luv_on_udp_recv); if (rc && uv_last_error(luv_get_loop(L)).code != UV_EALREADY) { uv_err_t err = uv_last_error(luv_get_loop(L)); return luaL_error(L, "udp_recv_start: %s", uv_strerror(err)); } luv_handle_ref(L, handle->data, 1); return 0; }
void uv_udp_handle_t::read(request_udp_read_t& request) { if (!m_read_started) { if (uv_udp_recv_start((uv_udp_t*)m_handle, on_read_alloc, on_read) != 0) { singleton_ref(node_lua_t).context_send(m_source, 0, m_lua_ref, RESPONSE_UDP_READ, singleton_ref(network_t).last_error()); return; } m_read_started = true; } }
/* * Class: com_oracle_libuv_handles_UDPHandle * Method: _recv_start * Signature: (J)I */ JNIEXPORT jint JNICALL Java_com_oracle_libuv_handles_UDPHandle__1recv_1start (JNIEnv *env, jobject that, jlong udp) { assert(udp); uv_udp_t* handle = reinterpret_cast<uv_udp_t*>(udp); int r = uv_udp_recv_start(handle, _alloc_cb, _recv_cb); // UV_EALREADY means that the socket is already bound but that's okay if (r && uv_last_error(handle->loop).code != UV_EALREADY) { ThrowException(env, handle->loop, "uv_udp_recv_start"); } return r; }
static int64_t HHVM_METHOD(UVUdp, setCallback, const Variant &onRecvCallback, const Variant &onSendCallback, const Variant &onErrorCallback) { int64_t ret; auto* data = Native::data<UVUdpData>(this_); if((ret = uv_udp_recv_start((uv_udp_t *) data->udp_handle, alloc_cb, (uv_udp_recv_cb) recv_cb)) == 0) { data->recvCallback = onRecvCallback; data->sendCallback = onSendCallback; data->errorCallback = onErrorCallback; data->udp_handle->flag |= (UV_UDP_HANDLE_START|UV_UDP_READ_START|UV_UDP_HANDLE_INTERNAL_REF); this_->incRefCount(); } return ret; }
static void cl_send_cb(uv_udp_send_t* req, int status) { int r; ASSERT(req != NULL); ASSERT(status == 0); CHECK_HANDLE(req->handle); r = uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb); ASSERT(r == 0); cl_send_cb_called++; }
bool UDPSocket::recvStart() { // UV_EALREADY means that the socket is already bound but that's okay // TODO: No need for boolean value as this method can throw exceptions // since it is called internally by bind(). int r = uv_udp_recv_start(ptr<uv_udp_t>(), UDPSocket::allocRecvBuffer, onRecv); if (r && r != UV_EALREADY) { setAndThrowError("Cannot start recv on invalid UDP socket", r); return false; } return true; }
//Initialize the Linux-specific part of the context. All is related to //libmnl/netfilter struct neat_ctx *neat_linux_init_ctx(struct neat_ctx *nc) { //TODO: Consider allocator function if ((nc->mnl_rcv_buf = calloc(MNL_SOCKET_BUFFER_SIZE, 1)) == NULL) { fprintf(stderr, "Failed to allocate netlink buffer\n"); return NULL; } //Configure netlink and start requesting addresses if ((nc->mnl_sock = mnl_socket_open(NETLINK_ROUTE)) == NULL) { fprintf(stderr, "Failed to allocate netlink socket\n"); return NULL; } if (mnl_socket_bind(nc->mnl_sock, (1 << (RTNLGRP_IPV4_IFADDR - 1)) | (1 << (RTNLGRP_IPV6_IFADDR - 1)), 0)) { fprintf(stderr, "Failed to bind netlink socket\n"); return NULL; } //We need to build a list of all available source addresses as soon as //possible. It is started here if (neat_linux_request_addrs(nc->mnl_sock) <= 0) { fprintf(stderr, "Failed to request addresses\n"); return NULL; } //Add socket to event loop if (uv_udp_init(nc->loop, &(nc->uv_nl_handle))) { fprintf(stderr, "Failed to initialize uv UDP handle\n"); return NULL; } //TODO: We could use offsetof, but libuv has a pointer so ... nc->uv_nl_handle.data = nc; if (uv_udp_open(&(nc->uv_nl_handle), mnl_socket_get_fd(nc->mnl_sock))) { fprintf(stderr, "Could not add netlink socket to uv\n"); return NULL; } if (uv_udp_recv_start(&(nc->uv_nl_handle), neat_linux_nl_alloc, neat_linux_nl_recv)) { fprintf(stderr, "Could not start receiving netlink packets\n"); return NULL; } nc->cleanup = neat_linux_cleanup; //Configure netlink socket, add to event loop and start dumping return nc; }
int Udp::RecvStart(/* [in] */ RecvHandler &handler) { assert(IsOpened()); assert(! IsRecvStarted()); int result = uv_udp_recv_start(*this, OnAlloc, OnRecv); if(! result) { Ref(); m_pRecvHandler = &handler; } return result; }
static int lluv_udp_start_recv(lua_State *L){ lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN); int err; lluv_check_args_with_cb(L, 2); LLUV_READ_CB(handle) = luaL_ref(L, LLUV_LUA_REGISTRY); err = uv_udp_recv_start(LLUV_H(handle, uv_udp_t), lluv_alloc_buffer_cb, lluv_on_udp_recv_cb); if(err >= 0) lluv_handle_lock(L, handle, LLUV_LOCK_READ); return lluv_return(L, handle, LLUV_READ_CB(handle), err); }
void UdpSocketBaton::start_recive() { if (!reciving) { int r = uv_udp_recv_start(&handle, &alloc_buffer, &on_udp_recive); if (r != 0) { throw std::logic_error(uv_strerror(r)); } reciving = true; } }
/* * * SOCKS5 UDP Request * +----+------+------+----------+----------+----------+ * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA | * +----+------+------+----------+----------+----------+ * | 2 | 1 | 1 | Variable | 2 | Variable | * +----+------+------+----------+----------+----------+ * */ static void client_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { struct server_context *server = container_of(handle, struct server_context, udp); if (nread > 0) { uint8_t frag = buf->base[2]; if (frag) { logger_log(LOG_ERR, "don't support udp dgram frag"); goto err; } char key[KEY_BYTES + 1] = {0}; crypto_generickey((uint8_t *)key, sizeof(key) -1, (uint8_t*)addr, sizeof(*addr), NULL, 0); struct client_context *client = NULL; uv_mutex_lock(&mutex); cache_lookup(cache, key, (void *)&client); uv_mutex_unlock(&mutex); if (client == NULL) { client = new_client(); client->addr = *addr; client->local_handle = handle; memcpy(client->key, key, sizeof(key)); uv_timer_init(handle->loop, client->timer); uv_udp_init(handle->loop, &client->server_handle); client->server_handle.data = client; uv_udp_recv_start(&client->server_handle, server_alloc_cb, server_recv_cb); uv_mutex_lock(&mutex); cache_insert(cache, client->key, (void *)client); uv_mutex_unlock(&mutex); } int clen = nread - 3 + PRIMITIVE_BYTES; uint8_t *c = (uint8_t *)buf->base - PRIMITIVE_BYTES; int rc = crypto_encrypt(c, (uint8_t*)buf->base + 3, nread - 3); if (!rc) { reset_timer(client); forward_to_server(server->server_addr, client, c, clen); } } else { goto err; } return; err: free(buf->base - PRIMITIVE_BYTES); }
static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) { struct sockaddr_in6 addr6; struct sockaddr_in addr; uv_buf_t buf; int r; ASSERT(0 == uv_ip6_addr("::0", TEST_PORT, &addr6)); r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); r = uv_udp_bind(&server, (const struct sockaddr*) &addr6, bind_flags); ASSERT(r == 0); r = uv_udp_recv_start(&server, alloc_cb, recv_cb); ASSERT(r == 0); r = uv_udp_init(uv_default_loop(), &client); ASSERT(r == 0); buf = uv_buf_init("PING", 4); ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); r = uv_udp_send(&req_, &client, &buf, 1, (const struct sockaddr*) &addr, send_cb); ASSERT(r == 0); r = uv_timer_init(uv_default_loop(), &timeout); ASSERT(r == 0); r = uv_timer_start(&timeout, timeout_cb, 500, 0); ASSERT(r == 0); ASSERT(close_cb_called == 0); ASSERT(send_cb_called == 0); ASSERT(recv_cb_called == 0); uv_run(uv_default_loop(), UV_RUN_DEFAULT); ASSERT(close_cb_called == 3); MAKE_VALGRIND_HAPPY(); }
int melo_udp_start(melo_udp_t * mudp, uv_loop_t* loop, melo_udp_config_t config) { assert(mudp && loop); mudp->uvloop = loop; memcpy(&mudp->config, &config, sizeof(melo_udp_config_t)); // init udp uv_udp_init(loop, &mudp->uvudp); mudp->uvudp.data = mudp; if (mudp->config.ip) { int r; melo_sockaddr_t sockaddr; melo_get_sock_addr(mudp->config.ip, mudp->config.port, &sockaddr); r = uv_udp_bind(&mudp->uvudp, (const struct sockaddr *)&sockaddr, 0); if(r >= 0) { char timestr[32]; time_t t; time(&t); strftime(timestr, sizeof(timestr), "[%Y-%m-%d %X]", localtime(&t)); // C99 only: %F = %Y-%m-%d MELO_INFO(config.log_out, "[uvx-udp] %s %s bind on %s:%d ...\n", timestr, mudp->config.name, mudp->config.ip, mudp->config.port); } else { MELO_ERR(config.log_err, "\n!!! [uvx-udp] %s bind on %s:%d failed: %s\n", mudp->config.name, mudp->config.ip, mudp->config.port, uv_strerror(r)); uv_close((uv_handle_t*) &mudp->uvudp, NULL); return ERROR; } } else { // if ip == NULL, default bind to local random port number (for UDP client) MELO_INFO(config.log_out, "[uvx-udp] %s bind on local random port ...\n", mudp->config.name); } uv_udp_recv_start(&mudp->uvudp, melo_alloc_buf, _cb_on_udp_recv); return OK; }
Status event_start (Server *server) { Event *event = &server->event; int rc = 0; debug ("udp recv start"); rc = uv_udp_recv_start ((uv_udp_t *) event, event_alloc_cb, event_cb); if (rc < 0) goto error; return G_OK; error: if (rc) debug ("error: %s", uv_strerror (rc)); return G_ERR; }
static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) { struct sockaddr_in6 addr6; struct sockaddr_in addr; uv_buf_t buf; int r; uv_init(); addr6 = uv_ip6_addr("::0", TEST_PORT); r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); r = uv_udp_bind6(&server, addr6, bind_flags); ASSERT(r == 0); r = uv_udp_recv_start(&server, alloc_cb, recv_cb); ASSERT(r == 0); r = uv_udp_init(uv_default_loop(), &client); ASSERT(r == 0); buf = uv_buf_init("PING", 4); addr = uv_ip4_addr("127.0.0.1", TEST_PORT); r = uv_udp_send(&req_, &client, &buf, 1, addr, send_cb); ASSERT(r == 0); r = uv_timer_init(uv_default_loop(), &timeout); ASSERT(r == 0); r = uv_timer_start(&timeout, timeout_cb, 500, 0); ASSERT(r == 0); ASSERT(close_cb_called == 0); ASSERT(send_cb_called == 0); ASSERT(recv_cb_called == 0); uv_run(uv_default_loop()); ASSERT(close_cb_called == 3); }
static int udp4_echo_start(int port) { int r; server = (uv_handle_t*)&udpServer; serverType = UDP; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "uv_udp_init: %s\n", uv_strerror(r)); return 1; } r = uv_udp_recv_start(&udpServer, echo_alloc, on_recv); if (r) { fprintf(stderr, "uv_udp_recv_start: %s\n", uv_strerror(r)); return 1; } return 0; }
static int udp_listener(int port) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port); struct sockaddr sockname; int namelen = sizeof(sockname); char ip[20]; int r; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_udp_bind(&udpServer, addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; } memset(&sockname, -1, sizeof sockname); r = uv_getsockname((uv_handle_t*)&udpServer, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (listening) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); ASSERT(ip[0] == '0'); ASSERT(ip[1] == '.'); ASSERT(ip[2] == '0'); printf("sockname = %s\n", ip); getsocknamecount++; r = uv_udp_recv_start(&udpServer, alloc, udp_recv); ASSERT(r == 0); return 0; }
int sg_etp_session_start(sg_etp_session_t * session, int interval_ms, uv_udp_t * udp) { int ret = ERROR; SG_ASSERT_RET(NULL != session, "session pointer is NULL", ret); session->interval = interval_ms; if (NULL == udp) /* self-contained udp, for client */ { /* init udp */ session->udp = &(session->udp_hdl); ret = uv_udp_init(session->loop, session->udp); SG_ASSERT_RET(ret >= 0, "init udp failed", ERROR); session->udp->data = session; ret = uv_udp_recv_start(session->udp, on_uv_alloc_buffer, on_client_recv_udp); SG_ASSERT_RET(ret >= 0, "start udp recv failed", ERROR); } else { session->udp = udp; } ret = ikcp_nodelay(session->kcp, 1, interval_ms, 2, 0); SG_ASSERT_RET(ret >= 0, "ikcp nodelay failed", ERROR); /* start a timer for kcp update and receiving */ ret = uv_timer_init(session->loop, &(session->timer)); SG_ASSERT_RET(ret >= 0, "init timer failed", ERROR); session->timer.data = session; /* link client pointer to timer */ ret = uv_timer_start(&(session->timer), on_uv_timer_cb, session->interval, session->interval); SG_ASSERT_RET(ret >= 0, "start timer failed", ERROR); uv_idle_init(session->loop, &(session->idle)); session->idle.data = session; SG_CALLBACK(session->on_open, session); return OK; }