static void recv_cb(uv_udp_t* handle,
                       ssize_t nread,
                       const uv_buf_t* buf,
                       const struct sockaddr* addr,
                       unsigned flags) {
  int r;

  if (nread < 0) {
    ASSERT(0 && "unexpected error");
  }

  if (nread == 0) {
    /* Returning unused buffer */
    /* Don't count towards sv_recv_cb_called */
    ASSERT(addr == NULL);
    return;
  }

  ASSERT(flags == 0);

  ASSERT(addr != NULL);
  ASSERT(nread == 4);
  ASSERT(memcmp("PING", buf->base, nread) == 0);

  r = uv_udp_recv_stop(handle);
  ASSERT(r == 0);

  uv_close((uv_handle_t*) handle, close_cb);
}
Beispiel #2
0
int xPLController::discoverxPLPort()
{
    int portTCP;
    uv_udp_t sock;
    struct sockaddr_in addr;
    int r;


    r = uv_udp_init(uv_default_loop(), &sock);
    if(r!=0) return XPL_DEFAULT_PORT;

    portTCP = XPL_DEFAULT_PORT;
    do
    {
        uv_ip4_addr("0.0.0.0", portTCP, &addr);
        r = uv_udp_bind(&sock, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
        if(r==0) break;
        if(portTCP == XPL_DEFAULT_PORT) portTCP = XPL_PORT_LOWER_BOUND-1;
        portTCP++;
    } while(portTCP != XPL_PORT_UPPER_BOUND);
    uv_udp_recv_stop(&sock);

    if(r!=0) return XPL_DEFAULT_PORT;
    return portTCP;
}
Beispiel #3
0
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);
}
Beispiel #4
0
Datei: udp.c Projekt: luvit/luv
static int luv_udp_recv_stop(lua_State* L) {
    uv_udp_t* handle = luv_check_udp(L, 1);
    int ret = uv_udp_recv_stop(handle);
    if (ret < 0) return luv_error(L, ret);
    lua_pushinteger(L, ret);
    return 1;
}
Beispiel #5
0
bool UDPSocket::recvStop()
{
    // This method must not throw since it is called internally via libuv
    // callbacks.
    if (!ptr())
        return false;
    return uv_udp_recv_stop(ptr<uv_udp_t>()) == 0;
}
Beispiel #6
0
int io_stop_read(uv_handle_t *handle)
{
	if (handle->type == UV_UDP) {
		return uv_udp_recv_stop((uv_udp_t *)handle);
	} else {
		return uv_read_stop((uv_stream_t *)handle);
	}
}
Beispiel #7
0
int luv_udp_recv_stop(lua_State* L) {
  uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp");
  if (uv_udp_recv_stop(handle)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "udp_recv_stop: %s", uv_strerror(err));
  }
  luv_handle_unref(L, handle->data);
  return 0;
}
Beispiel #8
0
 void UdpSocketBaton::close()
 {
     uv_udp_recv_stop(&handle);
     uv_close((uv_handle_t*)&handle, NULL);
     
     set_timeout(2000, [this] () {
         delete this;
     }); 
 }
Beispiel #9
0
Datei: udp.c Projekt: Ankso/node
void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle) {
  uv_udp_recv_stop(handle);
  closesocket(handle->socket);

  uv__handle_start(handle);

  if (handle->reqs_pending == 0) {
    uv_want_endgame(loop, (uv_handle_t*) handle);
  }
}
Beispiel #10
0
/*
 * Class:     com_oracle_libuv_handles_UDPHandle
 * Method:    _recv_stop
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_oracle_libuv_handles_UDPHandle__1recv_1stop
  (JNIEnv *env, jobject that, jlong udp) {

  assert(udp);
  uv_udp_t* handle = reinterpret_cast<uv_udp_t*>(udp);
  int r = uv_udp_recv_stop(handle);
  if (r) {
    ThrowException(env, handle->loop, "uv_udp_recv_stop");
  }
  return r;
}
Beispiel #11
0
Datei: udp.c Projekt: ajafff/node
void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle) {
  uv_udp_recv_stop(handle);
  closesocket(handle->socket);
  handle->socket = INVALID_SOCKET;

  uv__handle_closing(handle);

  if (handle->reqs_pending == 0) {
    uv_want_endgame(loop, (uv_handle_t*) handle);
  }
}
Beispiel #12
0
    int Udp::RecvStop()
    {
        assert(IsOpened() || IsClosing());
        assert(IsRecvStarted());

        int result = uv_udp_recv_stop(*this);

        m_pRecvHandler = NULL;
        Unref();

        return result;
    }
Beispiel #13
0
        void UDPClient::disconnect(bool wait = false) {
            if (not isConnected()) {
                return;
            }
            int i = uv_udp_recv_stop(&server);
            uvEXT(i, "Unable to disconnect");

            if (wait) {
                joinListenThread();
            }
            _connected = false;
        }
Beispiel #14
0
UvUdpSocket::~UvUdpSocket()
{
	if (m_udp_req)
	{
		free(m_udp_req);
	}
	if (m_uv_udp)
	{
		uv_udp_recv_stop(m_uv_udp);
		uv_close((uv_handle_t*)m_uv_udp,UvSocket::on_close);

	}
}
Beispiel #15
0
static void lluv_on_udp_recv_cb(uv_udp_t *arg, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags){
  lluv_handle_t *handle = lluv_handle_byptr((uv_handle_t*)arg);
  lua_State *L = LLUV_HCALLBACK_L(handle);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);

  if((nread == 0) && (addr == NULL)){
    /*
    ** The receive callback will be called with 
    ** nread == 0 and addr == NULL when there is 
    ** nothing to read
    */
    lluv_free_buffer((uv_handle_t*)arg, buf);
    return;
  }

  lua_rawgeti(L, LLUV_LUA_REGISTRY, LLUV_READ_CB(handle));
  assert(!lua_isnil(L, -1));

  lluv_handle_pushself(L, handle);

  if(nread >= 0){
    assert(addr);
    lua_pushnil(L);
    lua_pushlstring(L, buf->base, nread);
    lluv_free_buffer((uv_handle_t*)arg, buf);
  }
  else{
    lluv_free_buffer((uv_handle_t*)arg, buf);

    /* The callee is responsible for stopping closing the stream 
     *  when an error happens by calling uv_read_stop() or uv_close().
     *  Trying to read from the stream again is undefined.
     */
    uv_udp_recv_stop(arg);

    luaL_unref(L, LLUV_LUA_REGISTRY, LLUV_READ_CB(handle));
    LLUV_READ_CB(handle) = LUA_NOREF;

    lluv_error_create(L, LLUV_ERR_UV, (uv_errno_t)nread, NULL);
    lua_pushnil(L);

    lluv_handle_unlock(L, handle, LLUV_LOCK_READ);
  }
  lua_pushinteger(L, flags);

  LLUV_HANDLE_CALL_CB(L, handle, 4 + lluv_push_addr(L, (const struct sockaddr_storage*)addr));

  LLUV_CHECK_LOOP_CB_INVARIANT(L);
}
static void sv_recv_cb(uv_udp_t* handle,
                       ssize_t nread,
                       uv_buf_t buf,
                       struct sockaddr* addr,
                       unsigned flags) {
  uv_udp_send_t* req;
  int r;

  if (nread < 0) {
    ASSERT(0 && "unexpected error");
  }

  if (nread == 0) {
    /* Returning unused buffer */
    /* Don't count towards sv_recv_cb_called */
    ASSERT(addr == NULL);
    return;
  }

  CHECK_HANDLE(handle);
  ASSERT(flags == 0);

  ASSERT(addr != NULL);
  ASSERT(nread == 4);
  ASSERT(!memcmp("PING", buf.base, nread));

  /* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
    * anymore. That's problematic because the read buffer won't be returned
    * either... Not sure I like that but it's consistent with `uv_read_stop`.
    */
  r = uv_udp_recv_stop(handle);
  ASSERT(r == 0);

  req = malloc(sizeof *req);
  ASSERT(req != NULL);

  buf = uv_buf_init("PONG", 4);

  r = uv_udp_send(req,
                  handle,
                  &buf,
                  1,
                  *(struct sockaddr_in*)addr,
                  sv_send_cb);
  ASSERT(r == 0);

  sv_recv_cb_called++;
}
Beispiel #17
0
 ALWAYS_INLINE void releaseHandle(uv_udp_ext_t *handle) {
     if(handle->flag & UV_UDP_READ_START){
         handle->flag &= ~UV_UDP_READ_START;        
         uv_udp_recv_stop((uv_udp_t *) handle);
     }
     
     if(handle->flag & UV_UDP_HANDLE_START){
         handle->flag &= ~UV_UDP_HANDLE_START;        
         uv_unref((uv_handle_t *) handle);
     }    
     
     if(handle->flag & UV_UDP_HANDLE_INTERNAL_REF){
         handle->flag &= ~UV_UDP_HANDLE_INTERNAL_REF;
         ((uv_udp_ext_t *) handle)->udp_object_data->decRefAndRelease();
     }
 }
Beispiel #18
0
static PyObject *
UDP_func_stop_recv(UDP *self)
{
    int r;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    r = uv_udp_recv_stop((uv_udp_t *)UV_HANDLE(self));
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError);
        return NULL;
    }

    Py_XDECREF(self->on_read_cb);
    self->on_read_cb = NULL;

    Py_RETURN_NONE;
}
Beispiel #19
0
static int lluv_udp_stop_recv(lua_State *L){
  lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  int err;

  lluv_check_none(L, 2);

  err = uv_udp_recv_stop(LLUV_H(handle, uv_udp_t));
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  if(LLUV_READ_CB(handle) != LUA_NOREF){
    luaL_unref(L, LLUV_LUA_REGISTRY, LLUV_READ_CB(handle));
    LLUV_READ_CB(handle) = LUA_NOREF;
    lluv_handle_unlock(L, handle, LLUV_LOCK_READ);
  }

  lua_settop(L, 1);
  return 1;
}
Beispiel #20
0
// event handler for closing a tcp socket 
static void utb_on_close(uv_handle_t *handle)
{
	uv_stream_t *stream = (uv_stream_t *)handle;
	utb_t *utb;
	// free the data object
	if (stream->data != 0) {
		UTB_PRINTF("free %p",stream->data);

		utb = (utb_t *)stream->data;

		// stop udp receiving
		uv_udp_recv_stop(&utb->udp_sock);

		// mark tcp is closed
		utb->tcp_closed = 1;

		// close udp socket
		uv_close((uv_handle_t *)&utb->udp_sock,on_udp_close);
	}
	UTB_PRINTF("free %p",stream);
}
Beispiel #21
0
Datei: udp.c Projekt: imclab/pyuv
static PyObject *
UDP_func_stop_recv(UDP *self)
{
    int err;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    err = uv_udp_recv_stop(&self->udp_h);
    if (err < 0) {
        RAISE_UV_EXCEPTION(err, PyExc_UDPError);
        return NULL;
    }

    Py_XDECREF(self->on_read_cb);
    self->on_read_cb = NULL;

    PYUV_HANDLE_DECREF(self);

    Py_RETURN_NONE;
}
Beispiel #22
0
CAMLprim value
uwt_udp_recv_stop(value o_udp, value o_abort)
{
  HANDLE_NO_UNINIT_CLOSED_INT_RESULT(o_udp);
  HANDLE_INIT(u,o_udp);
  value ret;
  if ( u->cb_read == CB_INVALID || /* see comment to uwt_read_stop */
       (u->read_waiting == 1 && Long_val(o_abort) == 0 )){
    ret = Val_long(0);
  }
  else {
    const int erg = uv_udp_recv_stop((uv_udp_t*)u->handle);
    if ( erg >= 0 ){
      u->can_reuse_cb_read = 0;
      u->read_waiting = 0;
      --u->in_use_cnt;
      uwt__gr_unregister(&u->cb_read);
    }
    ret = VAL_UWT_UNIT_RESULT(erg);
  }
  CAMLreturn(ret);
}
Beispiel #23
0
int melo_udp_shutdown(melo_udp_t * mudp)
{
    uv_udp_recv_stop(&mudp->uvudp);
    uv_close((uv_handle_t*) &mudp->uvudp, NULL);
    return OK;
}
Beispiel #24
0
Datei: udp.c Projekt: Ankso/node
void uv_process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle,
    uv_req_t* req) {
  uv_buf_t buf;
  int partial;

  assert(handle->type == UV_UDP);

  handle->flags &= ~UV_HANDLE_READ_PENDING;

  if (!REQ_SUCCESS(req)) {
    DWORD err = GET_REQ_SOCK_ERROR(req);
    if (err == WSAEMSGSIZE) {
      /* Not a real error, it just indicates that the received packet */
      /* was bigger than the receive buffer. */
    } else if (err == WSAECONNRESET || err == WSAENETRESET) {
      /* A previous sendto operation failed; ignore this error. If */
      /* zero-reading we need to call WSARecv/WSARecvFrom _without_ the */
      /* MSG_PEEK flag to clear out the error queue. For nonzero reads, */
      /* immediately queue a new receive. */
      if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
        goto done;
      }
    } else {
      /* A real error occurred. Report the error to the user only if we're */
      /* currently reading. */
      if (handle->flags & UV_HANDLE_READING) {
        uv__set_sys_error(loop, err);
        uv_udp_recv_stop(handle);
        buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
              uv_buf_init(NULL, 0) : handle->recv_buffer;
        handle->recv_cb(handle, -1, buf, NULL, 0);
      }
      goto done;
    }
  }

  if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
    /* Successful read */
    partial = !REQ_SUCCESS(req);
    handle->recv_cb(handle,
                    req->overlapped.InternalHigh,
                    handle->recv_buffer,
                    (struct sockaddr*) &handle->recv_from,
                    partial ? UV_UDP_PARTIAL : 0);
  } else if (handle->flags & UV_HANDLE_READING) {
    DWORD bytes, err, flags;
    struct sockaddr_storage from;
    int from_len;

    /* Do a nonblocking receive */
    /* TODO: try to read multiple datagrams at once. FIONREAD maybe? */
    buf = handle->alloc_cb((uv_handle_t*) handle, 65536);
    assert(buf.len > 0);

    memset(&from, 0, sizeof from);
    from_len = sizeof from;

    flags = 0;

    if (WSARecvFrom(handle->socket,
                    (WSABUF*)&buf,
                    1,
                    &bytes,
                    &flags,
                    (struct sockaddr*) &from,
                    &from_len,
                    NULL,
                    NULL) != SOCKET_ERROR) {

      /* Message received */
      handle->recv_cb(handle, bytes, buf, (struct sockaddr*) &from, 0);
    } else {
      err = WSAGetLastError();
      if (err == WSAEMSGSIZE) {
        /* Message truncated */
        handle->recv_cb(handle,
                        bytes,
                        buf,
                        (struct sockaddr*) &from,
                        UV_UDP_PARTIAL);
      } if (err == WSAEWOULDBLOCK) {
        /* Kernel buffer empty */
        uv__set_sys_error(loop, WSAEWOULDBLOCK);
        handle->recv_cb(handle, 0, buf, NULL, 0);
      } else if (err != WSAECONNRESET && err != WSAENETRESET) {
        /* Serious error. WSAECONNRESET/WSANETRESET is ignored because this */
        /* just indicates that a previous sendto operation failed. */
        uv_udp_recv_stop(handle);
        uv__set_sys_error(loop, err);
        handle->recv_cb(handle, -1, buf, NULL, 0);
      }
    }
  }

done:
  /* Post another read if still reading and not closing. */
  if ((handle->flags & UV_HANDLE_READING) &&
      !(handle->flags & UV_HANDLE_READ_PENDING)) {
    uv_udp_queue_recv(loop, handle);
  }

  DECREASE_PENDING_REQ_COUNT(handle);
}
Beispiel #25
0
static void
uwt_udp_recv_own_cb(uv_udp_t* handle,
                    ssize_t nread,
                    const uv_buf_t* buf,
                    const struct sockaddr* addr,
                    unsigned int flags)
{
  HANDLE_CB_INIT_WITH_CLEAN(uh, handle);
  value exn = Val_unit;
#ifndef UWT_NO_COPY_READ
  bool buf_not_cleaned = true;
  const int read_ba = uh->use_read_ba;
#else
  (void) buf;
#endif
  if ( uh->close_called == 0 && (nread != 0 || addr != NULL) ){
    /* nread == 0 && addr == NULL only means we need to clear
       the buffer */
    assert ( uh->cb_read != CB_INVALID );
    value param;
    if ( nread < 0 ){
      param = caml_alloc_small(1,Error_tag);
      Field(param,0) = Val_uwt_error(nread);
    }
    else {
      value triple = Val_unit;
      value sockaddr = Val_unit;
      param = Val_unit;
      Begin_roots3(triple,sockaddr,param);
      value is_partial;
      if ( addr != NULL ){
        param = uwt__alloc_sockaddr(addr);
        if ( param != Val_unit ){
          sockaddr = caml_alloc_small(1,Some_tag);
          Field(sockaddr,0) = param;
        }
      }
      if ( flags & UV_UDP_PARTIAL ){
        is_partial = Val_long(1);
      }
      else {
        is_partial = Val_long(0);
      }
#ifndef UWT_NO_COPY_READ
      if ( nread != 0 && read_ba == 0 ){
        value o = Field(GET_CB_VAL(uh->cb_read),0);
        memcpy(String_val(o) + uh->x.obuf_offset, buf->base, nread);
      }
#endif
      triple = caml_alloc_small(3,0);
      Field(triple,0) = Val_long(nread);
      Field(triple,1) = is_partial;
      Field(triple,2) = sockaddr;
      param = caml_alloc_small(1,Ok_tag);
      Field(param,0) = triple;
      End_roots();
    }
#ifndef UWT_NO_COPY_READ
    if ( buf->base && read_ba == 0 ){
      buf_not_cleaned = false;
      uwt__free_uv_buf_t_const(buf);
    }
#endif
    uh->can_reuse_cb_read = 1;
    uh->read_waiting = 0;
    uh->in_use_cnt--;
    exn = Field(GET_CB_VAL(uh->cb_read),1);
    uwt__gr_unregister(&uh->cb_read);
    exn = caml_callback2_exn(*uwt__global_wakeup,exn,param);
    if ( uh->close_called == 0 && uh->can_reuse_cb_read == 1 ){
      uv_udp_recv_stop(handle);
      uh->can_reuse_cb_read = 0;
    }
  }
#ifndef UWT_NO_COPY_READ
  if ( read_ba == 0 && buf_not_cleaned && buf->base ){
    uwt__free_uv_buf_t_const(buf);
  }
#endif
  HANDLE_CB_RET(exn);
}
Beispiel #26
0
int close_socket(unsigned int loc_idx) {         
	
	uv_udp_recv_stop(&udp_servers[loc_idx]);	  
	uv_close((uv_handle_t*)&udp_servers[loc_idx], NULL);	  
	return 0;
}
Beispiel #27
0
void uv_close(uv_handle_t* handle, uv_close_cb cb) {
    uv_pipe_t* pipe;
    uv_udp_t* udp;
    uv_process_t* process;

    uv_loop_t* loop = handle->loop;

    if (handle->flags & UV_HANDLE_CLOSING) {
        return;
    }

    handle->flags |= UV_HANDLE_CLOSING;
    handle->close_cb = cb;

    /* Handle-specific close actions */
    switch (handle->type) {
    case UV_TCP:
        uv_tcp_close((uv_tcp_t*)handle);
        return;

    case UV_NAMED_PIPE:
        pipe = (uv_pipe_t*)handle;
        pipe->flags &= ~(UV_HANDLE_READING | UV_HANDLE_LISTENING);
        close_pipe(pipe, NULL, NULL);
        if (pipe->reqs_pending == 0) {
            uv_want_endgame(loop, handle);
        }
        return;

    case UV_TTY:
        uv_tty_close((uv_tty_t*) handle);
        return;

    case UV_UDP:
        udp = (uv_udp_t*) handle;
        uv_udp_recv_stop(udp);
        closesocket(udp->socket);
        if (udp->reqs_pending == 0) {
            uv_want_endgame(loop, handle);
        }
        return;

    case UV_POLL:
        uv_poll_close(handle->loop, (uv_poll_t*) handle);
        return;

    case UV_TIMER:
        uv_timer_stop((uv_timer_t*)handle);
        uv_want_endgame(loop, handle);
        return;

    case UV_PREPARE:
        uv_prepare_stop((uv_prepare_t*)handle);
        uv_want_endgame(loop, handle);
        return;

    case UV_CHECK:
        uv_check_stop((uv_check_t*)handle);
        uv_want_endgame(loop, handle);
        return;

    case UV_IDLE:
        uv_idle_stop((uv_idle_t*)handle);
        uv_want_endgame(loop, handle);
        return;

    case UV_ASYNC:
        if (!((uv_async_t*)handle)->async_sent) {
            uv_want_endgame(loop, handle);
        }
        return;

    case UV_PROCESS:
        process = (uv_process_t*)handle;
        uv_process_close(loop, process);
        return;

    case UV_FS_EVENT:
        uv_fs_event_close(loop, (uv_fs_event_t*)handle);
        return;

    default:
        /* Not supported */
        abort();
    }
}
Beispiel #28
0
Datei: udp.c Projekt: Darkie/node
void uv_process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle,
    uv_req_t* req) {
  uv_buf_t buf;
  int partial;

  assert(handle->type == UV_UDP);

  handle->flags &= ~UV_HANDLE_READ_PENDING;

  if (!REQ_SUCCESS(req) &&
      GET_REQ_STATUS(req) != STATUS_RECEIVE_EXPEDITED) {
    /* An error occurred doing the read. */
    if ((handle->flags & UV_HANDLE_READING)) {
      uv__set_sys_error(loop, GET_REQ_SOCK_ERROR(req));      
      uv_udp_recv_stop(handle);
#if 0
      buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
            uv_buf_init(NULL, 0) : handle->recv_buffer;
#else
      buf = handle->recv_buffer;
#endif
      handle->recv_cb(handle, -1, buf, NULL, 0);
    }
    goto done;
  }

#if 0
  if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
#endif
    /* Successful read */
    partial = (GET_REQ_STATUS(req) == STATUS_RECEIVE_EXPEDITED);
    handle->recv_cb(handle,
                    req->overlapped.InternalHigh,
                    handle->recv_buffer,
                    (struct sockaddr*) &handle->recv_from,
                    partial ? UV_UDP_PARTIAL : 0);
#if 0
  } else {
    DWORD bytes, err, flags;
    struct sockaddr_storage from;
    int from_len;

    /* Do a nonblocking receive */
    /* TODO: try to read multiple datagrams at once. FIONREAD maybe? */
    buf = handle->alloc_cb((uv_handle_t*) handle, 65536);
    assert(buf.len > 0);

    memset(&from, 0, sizeof from);
    from_len = sizeof from;
    flags = MSG_PARTIAL;

    if (WSARecvFrom(handle->socket,
                    (WSABUF*)&buf,
                    1,
                    &bytes,
                    &flags,
                    (struct sockaddr*) &from,
                    &from_len,
                    NULL,
                    NULL) != SOCKET_ERROR) {

      /* Message received */
      handle->recv_cb(handle,
                      bytes,
                      buf,
                      (struct sockaddr*) &from,
                        (flags & MSG_PARTIAL) ? UV_UDP_PARTIAL : 0);
    } else {
      err = WSAGetLastError();
      if (err == WSAEWOULDBLOCK) {
        uv__set_sys_error(loop, WSAEWOULDBLOCK);
        handle->recv_cb(handle, 0, buf, NULL, 0);
      } else {
        /* Ouch! serious error. */
        uv__set_sys_error(loop, err);
        handle->recv_cb(handle, -1, buf, NULL, 0);
      }
    }
  }
#endif

done:
  /* Post another read if still reading and not closing. */
  if ((handle->flags & UV_HANDLE_READING) &&
      !(handle->flags & UV_HANDLE_READ_PENDING)) {
    uv_udp_queue_recv(loop, handle);
  }

  DECREASE_PENDING_REQ_COUNT(handle);
}
Beispiel #29
0
void uv_close(uv_handle_t* handle, uv_close_cb cb) {
  uv_tcp_t* tcp;
  uv_pipe_t* pipe;
  uv_udp_t* udp;
  uv_process_t* process;

  if (handle->flags & UV_HANDLE_CLOSING) {
    return;
  }

  handle->flags |= UV_HANDLE_CLOSING;
  handle->close_cb = cb;

  /* Handle-specific close actions */
  switch (handle->type) {
    case UV_TCP:
      tcp = (uv_tcp_t*)handle;
      /* If we don't shutdown before calling closesocket, windows will */
      /* silently discard the kernel send buffer and reset the connection. */
      if (!(tcp->flags & UV_HANDLE_SHUT)) {
        shutdown(tcp->socket, SD_SEND);
        tcp->flags |= UV_HANDLE_SHUT;
      }
      tcp->flags &= ~(UV_HANDLE_READING | UV_HANDLE_LISTENING);
      closesocket(tcp->socket);
      if (tcp->reqs_pending == 0) {
        uv_want_endgame(handle);
      }
      return;

    case UV_NAMED_PIPE:
      pipe = (uv_pipe_t*)handle;
      pipe->flags &= ~(UV_HANDLE_READING | UV_HANDLE_LISTENING);
      close_pipe(pipe, NULL, NULL);
      if (pipe->reqs_pending == 0) {
        uv_want_endgame(handle);
      }
      return;

    case UV_UDP:
      udp = (uv_udp_t*) handle;
      uv_udp_recv_stop(udp);
      closesocket(udp->socket);
      if (udp->reqs_pending == 0) {
        uv_want_endgame(handle);
      }
      return;

    case UV_TIMER:
      uv_timer_stop((uv_timer_t*)handle);
      uv_want_endgame(handle);
      return;

    case UV_PREPARE:
      uv_prepare_stop((uv_prepare_t*)handle);
      uv_want_endgame(handle);
      return;

    case UV_CHECK:
      uv_check_stop((uv_check_t*)handle);
      uv_want_endgame(handle);
      return;

    case UV_IDLE:
      uv_idle_stop((uv_idle_t*)handle);
      uv_want_endgame(handle);
      return;

    case UV_ASYNC:
      if (!((uv_async_t*)handle)->async_sent) {
        uv_want_endgame(handle);
      }
      return;

    case UV_PROCESS:
      process = (uv_process_t*)handle;
      uv_process_close(process);
      return;

    default:
      /* Not supported */
      abort();
  }
}
Beispiel #30
0
extern "C" int
rust_uv_udp_recv_stop(uv_udp_t* server) {
    return uv_udp_recv_stop(server);
}