示例#1
0
文件: lluv_udp.c 项目: kidaa/lua-lluv
static int lluv_udp_send_(lua_State *L, lluv_handle_t *handle, struct sockaddr *sa, uv_buf_t *buf, size_t n){
  int err; lluv_req_t *req;

  if(lua_gettop(L) == 6){
    int ctx;
    lluv_check_callable(L, -2);
    ctx = luaL_ref(L, LLUV_LUA_REGISTRY);
    req = lluv_req_new(L, UV_UDP_SEND, handle);
    lluv_req_ref(L, req); /* string/table */
    req->ctx = ctx;
  }
  else{
    if(lua_gettop(L) == 4)
      lua_settop(L, 5);
    else
      lluv_check_args_with_cb(L, 5);

    req = lluv_req_new(L, UV_UDP_SEND, handle);
    lluv_req_ref(L, req); /* string/table */
  }

  err = uv_udp_send(LLUV_R(req, udp_send), LLUV_H(handle, uv_udp_t), buf, n, sa, lluv_on_udp_send_cb);

  return lluv_return_req(L, handle, req, err);
}
示例#2
0
int32_t uv_udp_handle_t::write_handle(request_udp_write_t& request)
{
	int result;
	uv_buf_t uv_buf;
	write_uv_request_t* uv_request = get_write_cached_request();
	uv_request->m_source = request.m_source;
	uv_request->m_session = request.m_session;
	uv_request->m_length = request.m_length;
	if (request.m_length > 0) {
		uv_request->m_string = request.m_string;
		uv_buf.len = request.m_length;
		uv_buf.base = (char*)request.m_string;
	} else {
		uv_request->m_buffer = request.m_buffer;
		uv_buf.len = buffer_data_length(request.m_buffer);
		uv_buf.base = buffer_data_ptr(request.m_buffer);
	}
	if (!request.m_ipv6) {
		result = uv_udp_send(&uv_request->m_write_req, (uv_udp_t*)m_handle, &uv_buf, 1, uv_ip4_addr(REQUEST_SPARE_PTR(request), request.m_port), on_write);
	} else {
		result = uv_udp_send6(&uv_request->m_write_req, (uv_udp_t*)m_handle, &uv_buf, 1, uv_ip6_addr(REQUEST_SPARE_PTR(request), request.m_port), on_write);
	}
	if (result == 0) return UV_OK;
	put_write_cached_request(uv_request);
	return singleton_ref(network_t).last_error(); /* write error occurs */
}
示例#3
0
文件: udp.c 项目: luvit/luv
static int luv_udp_send(lua_State* L) {
    uv_udp_t* handle = luv_check_udp(L, 1);
    uv_udp_send_t* req;
    uv_buf_t buf;
    int ret, port, ref;
    const char* host;
    struct sockaddr_storage addr;
    luv_check_buf(L, 2, &buf);
    host = luaL_checkstring(L, 3);
    port = luaL_checkinteger(L, 4);
    if (uv_ip4_addr(host, port, (struct sockaddr_in*)&addr) &&
            uv_ip6_addr(host, port, (struct sockaddr_in6*)&addr)) {
        return luaL_error(L, "Invalid IP address or port [%s:%d]", host, port);
    }
    ref = luv_check_continuation(L, 5);
    req = (uv_udp_send_t*)lua_newuserdata(L, sizeof(*req));
    req->data = luv_setup_req(L, ref);
    ret = uv_udp_send(req, handle, &buf, 1, (struct sockaddr*)&addr, luv_udp_send_cb);
    if (ret < 0) {
        luv_cleanup_req(L, (luv_req_t*)req->data);
        lua_pop(L, 1);
        return luv_error(L, ret);
    }
    return 1;

}
示例#4
0
文件: melo_udp.c 项目: susemm/melo
int melo_udp_send_to_addr(melo_udp_t * mudp, const struct sockaddr* addr, const void* data, unsigned int datalen)
{
    uv_udp_send_t* req = (uv_udp_send_t*) malloc(sizeof(uv_udp_send_t) + datalen);
    uv_buf_t buf = uv_buf_init((char*)req + sizeof(uv_udp_send_t), datalen);
    memcpy(buf.base, data, datalen); // copy data to the end of req
    req->data = mudp;
    return (uv_udp_send(req, &mudp->uvudp, &buf, 1, addr, _cb_after_udp_send) == 0 ? 1 : 0);
}
示例#5
0
static void
forward_to_client(struct client_context *client, uint8_t *data, ssize_t len) {
    uv_udp_send_t *write_req = malloc(sizeof(*write_req) + sizeof(uv_buf_t));
    uv_buf_t *buf = (uv_buf_t *)(write_req + 1);
    buf->base = (char *)data;
    buf->len = len;
    write_req->data = client;
    uv_udp_send(write_req, client->local_handle, buf, 1, &client->addr, client_send_cb);
}
示例#6
0
/*
 *
 * xsocks UDP Request
 * +------+----------+----------+----------+
 * | ATYP | DST.ADDR | DST.PORT |   DATA   |
 * +------+----------+----------+----------+
 * |  1   | Variable |    2     | Variable |
 * +------+----------+----------+----------+
 *
 */
static void
forward_to_server(struct sockaddr *server_addr, struct client_context *client, uint8_t *data, ssize_t datalen) {
    uv_udp_send_t *write_req = malloc(sizeof(*write_req) + sizeof(uv_buf_t));
    uv_buf_t *buf = (uv_buf_t *)(write_req + 1);
    buf->base = (char *)data;
    buf->len = datalen;
    write_req->data = client;;
    uv_udp_send(write_req, &client->server_handle, buf, 1, server_addr, server_send_cb);
}
示例#7
0
int UDPSocket::send(const char* data, std::size_t len,
                    const Address& peerAddress, int /* flags */)
{
    TraceS(this) << "Send: " << len << ": " << peerAddress << endl;
    assert(Thread::currentID() == tid());
    // assert(len <= net::MAX_UDP_PACKET_SIZE);

    if (_peer.valid() && _peer != peerAddress) {
        ErrorS(this) << "Peer not authorized: " << peerAddress << endl;
        return -1;
    }

    if (!peerAddress.valid()) {
        ErrorS(this) << "Peer not valid: " << peerAddress << endl;
        return -1;
    }

    int r;
    auto sr = new internal::SendRequest;
    sr->buf = uv_buf_init((char*)data, len); // TODO: memcpy data?
    r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(),
                    UDPSocket::afterSend);

#if 0
    switch (peerAddress.af()) {
    case AF_INET:
        r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(), UDPSocket::afterSend);
        break;
    case AF_INET6:
        r = uv_udp_send6(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1,
            *reinterpret_cast<const sockaddr_in6*>(peerAddress.addr()), UDPSocket::afterSend);
        break;
    default:
        throw std::runtime_error("Unexpected address family");
    }
#endif
    if (r) {
        ErrorS(this) << "Send failed: " << uv_err_name(r) << endl;
        setUVError("Invalid UDP socket", r);
    }

    // R is -1 on error, otherwise return len
    return r ? r : len;
}
示例#8
0
int UvUdpSocket::asyncWrite(const uv_buf_t data[], int count,  const struct sockaddr* addr )
{
	if (!m_udp_req)
	{
		m_udp_req = (uv_udp_send_t*)malloc(sizeof uv_udp_send_t);
	}
	m_udp_req->data = this;
	return uv_udp_send(m_udp_req,m_uv_udp, data, count,addr,UvUdpSocket::udp_send_cb);

}
示例#9
0
文件: etp.c 项目: canmor-lam/libsg
/* for kcp callback */
static int on_kcp_output(const char *buf, int len, struct IKCPCB *kcp, void *user)
{
    sg_etp_t * session = (sg_etp_t *)user;
    int ret = -1;
    send_req_t * req = NULL;

    /*LOG_D("udp send: %d\n", len);*/

    do
    {
        /* 限速 */
        if (session->max_speed_limit > 0 && session->current_speed > session->max_speed_limit)
        {
            return ret;
        }

        req = (send_req_t *)malloc(sizeof(send_req_t));
        SG_ASSERT_BRK(NULL != req, "create send_req_t failed");

        memset(req, 0, sizeof(send_req_t));

        req->buf.base = malloc(sizeof(char) * len);
        SG_ASSERT_BRK(NULL != req->buf.base, "create buf failed");
        req->buf.len = len;

        req->session = session;   /* remember the client pointer to used in on_udp_send_done */

        memcpy(req->buf.base, buf, len);

        ret = uv_udp_send((uv_udp_send_t*)req, session->udp, &req->buf, 1, &session->addr, on_udp_send_done);
        if (ret < 0)
        {
            free(req->buf.base); /* TODO: ensure free */
            free(req); /* TODO: ensure free ? */
            return -1;
        }

        return ret;
    } while (0);

    if (NULL != req)
    {
        if (NULL != req->buf.base)
        {
            free(req->buf.base);
            req->buf.base = NULL;
        }

        free(req);
        req = NULL;
    }

    return ret;
}
示例#10
0
文件: utb001.c 项目: dmh2000/utb
// forward read data to udp client
void utb_read(utb_t *utb,char *inbuf,size_t len)
{
	utb_send_req_t *req;
	int             status;

	// set up request buffer
	req = utb_req_send_buffer(utb,inbuf,len);

	// write data to the udp socket
	status = uv_udp_send((uv_udp_send_t *)req,&utb->udp_sock,&req->buf,1,utb->rmt_addr,utb_on_send);
	assert(status == 0);
}
示例#11
0
 static int64_t HHVM_METHOD(UVUdp, sendTo, const String &dest, int64_t port, const String &message) {
     auto* data = Native::data<UVUdpData>(this_);
     int64_t ret;
     send_req_t *req;
     req = new send_req_t();
     req->buf.base = new char[message.size()];
     req->buf.len = message.size();
     memcpy((void *) req->buf.base, message.c_str(), message.size());
     if((ret = uv_ip4_addr(dest.c_str(), port, &req->addr)) != 0){
         return ret;
     }
     return uv_udp_send(req, data->udp_handle, &req->buf, 1, (const struct sockaddr *) &req->addr, send_cb);
 }
示例#12
0
/*
 * Class:     com_oracle_libuv_handles_UDPHandle
 * Method:    _send
 * Signature: (JLjava/nio/ByteBuffer;[BIIILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_oracle_libuv_handles_UDPHandle__1send
  (JNIEnv *env, jobject that, jlong udp, jobject buffer, jbyteArray data, jint offset, jint length, jint port, jstring host, jobject context) {

  assert(udp);
  uv_udp_t* handle = reinterpret_cast<uv_udp_t*>(udp);
  const char* h = env->GetStringUTFChars(host, 0);
  sockaddr_in addr = uv_ip4_addr(h, port);
  uv_udp_send_t* req = new uv_udp_send_t();
  req->handle = handle;
  ContextHolder* req_data = NULL;
  int r;
  if (data) {
    jbyte* base = (jbyte*) env->GetPrimitiveArrayCritical(data, NULL);
    OOME(env, base);
    uv_buf_t buf;
    buf.base = reinterpret_cast<char*>(base + offset);
    buf.len = length;
    req_data = new ContextHolder(env, context);
    req->data = req_data;
    r = uv_udp_send(req, handle, &buf, 1, addr, _send_cb);
    env->ReleasePrimitiveArrayCritical(data, base, 0);
  } else {
    jbyte* base = (jbyte*) env->GetDirectBufferAddress(buffer);
    uv_buf_t buf;
    buf.base = reinterpret_cast<char*>(base + offset);
    buf.len = length;
    req_data = new ContextHolder(env, buffer, context);
    req->data = req_data;
    r = uv_udp_send(req, handle, &buf, 1, addr, _send_cb);
  }
  if (r) {
    delete req_data;
    delete req;
    ThrowException(env, handle->loop, "uv_udp_send", h);
  }
  env->ReleaseStringUTFChars(host, h);
  return r;
}
示例#13
0
文件: jl_uv.c 项目: jskDr/julia
DLLEXPORT int jl_udp_send6(uv_udp_t *handle, uint16_t port, void *host, void *data, uint32_t size)
{
    struct sockaddr_in6 addr;
    memset(&addr, 0, sizeof(struct sockaddr_in6));
    addr.sin6_port = port;
    memcpy(&addr.sin6_addr, host, 16);
    addr.sin6_family = AF_INET6;
    uv_buf_t buf[1];
    buf[0].base = (char *) data;
    buf[0].len = size;
    uv_udp_send_t *req = (uv_udp_send_t *) malloc(sizeof(uv_udp_send_t));
    req->data = handle->data;
    return uv_udp_send(req, handle, buf, 1, (struct sockaddr*)&addr, &jl_uv_sendcb);
}
示例#14
0
文件: jl_uv.c 项目: Wilfred/julia
DLLEXPORT int jl_udp_send(uv_udp_t *handle, uint16_t port, uint32_t host, void *data, uint32_t size, uv_udp_send_cb cb)
{
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_port = port;
    addr.sin_addr.s_addr = host;
    addr.sin_family = AF_INET;
    uv_buf_t buf[1];
    buf[0].base = (char *) data;
    buf[0].len = size;
    uv_udp_send_t *req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
    req->data = handle->data;
    return uv_udp_send(req, handle, buf, 1, (struct sockaddr*)&addr, cb);
}
示例#15
0
 void UdpSocketBaton::send(IpAddress adr, const char* data, size_t len)
 {
     UdpSend* baton = new UdpSend;
     baton->handle.data = baton;
     baton->buffer      = memdup(data, len);
     
     int r = uv_udp_send(&(baton->handle), &handle, &(baton->buffer), 1, adr.c_obj(), &on_udp_send);
     if (r != 0) 
     {
         throw std::logic_error(uv_strerror(r));                
     }
     
     start_recive();
 }
示例#16
0
void
tun_to_udp(struct tundev_context *ctx, uint8_t *buf, int len,
           struct sockaddr *addr) {
    uv_udp_send_t *write_req = malloc(sizeof(*write_req) + sizeof(uv_buf_t));
    uv_buf_t *outbuf = (uv_buf_t *) (write_req + 1);
    outbuf->base = (char *) buf;
    outbuf->len = len;
    if (write_req) {
        write_req->data = ctx;
        uv_udp_send(write_req, &ctx->inet_udp, outbuf, 1, addr, inet_send_cb);
    } else {
        free(buf);
    }
}
示例#17
0
static void udp_sender(void) {
  struct sockaddr_in server_addr;
  uv_buf_t buf;
  int r;

  r = uv_udp_init(loop, &udp);
  ASSERT(!r);

  buf = uv_buf_init("PING", 4);
  server_addr = uv_ip4_addr("127.0.0.1", server_port);

  r = uv_udp_send(&send_req, &udp, &buf, 1, server_addr, udp_send);
  ASSERT(!r);
}
示例#18
0
static int luv_udp__send(lua_State* L, int family) {
  uv_buf_t buf;
  uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp");
  size_t len;
  const char* chunk = luaL_checklstring(L, 2, &len);
  luv_udp_ref_t *ref;

  uv_udp_send_t* req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
  int port = luaL_checkint(L, 3);
  const char* host = luaL_checkstring(L, 4);
  struct sockaddr_in dest;
  struct sockaddr_in6 dest6;
  int rc;

  /* Store a reference to the callback */
  lua_pushvalue(L, 5);
  ref = malloc(sizeof(*ref));
  ref->ref = luaL_ref(L, LUA_REGISTRYINDEX);
  req->data = ref;

  luv_handle_ref(L, handle->data, 1);

  /* Store the chunk
   * TODO: this is probably unsafe, should investigate
   */
  buf = uv_buf_init((char*)chunk, len);

  switch(family) {
  case AF_INET:
    dest = uv_ip4_addr(host, port);
    rc = uv_udp_send(req, handle, &buf, 1, dest, luv_on_udp_send);
    break;
  case AF_INET6:
    dest6 = uv_ip6_addr(host, port);
    rc = uv_udp_send6(req, handle, &buf, 1, dest6, luv_on_udp_send);
    break;
  default:
    assert(0 && "unexpected family type");
    abort();
  }

  if (rc) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "udp_send: %s", uv_strerror(err));
  }

  return 0;
}
示例#19
0
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++;
}
示例#20
0
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();
}
示例#21
0
static void on_recv(uv_udp_t* handle,
                    ssize_t nread,
                    const uv_buf_t* rcvbuf,
                    const struct sockaddr* addr,
                    unsigned flags) {
  uv_udp_send_t* req;
  uv_buf_t sndbuf;

  ASSERT(nread > 0);
  ASSERT(addr->sa_family == AF_INET);

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

  sndbuf = *rcvbuf;
  ASSERT(0 == uv_udp_send(req, handle, &sndbuf, 1, addr, on_send));
}
示例#22
0
static void on_recv(uv_udp_t* handle,
                    ssize_t nread,
                    uv_buf_t buf,
                    struct sockaddr* addr,
                    unsigned flags) {
  uv_udp_send_t* req;
  int r;

  ASSERT(nread > 0);
  ASSERT(addr->sa_family == AF_INET);

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

  r = uv_udp_send(req, handle, &buf, 1, *(struct sockaddr_in*)addr, on_send);
  ASSERT(r == 0);
}
示例#23
0
static void
forward_to_server(struct sockaddr *server_addr, struct client_context *client, uint8_t *data, ssize_t datalen) {
    if (verbose) {
        char src[INET6_ADDRSTRLEN + 1] = {0};
        char dst[INET6_ADDRSTRLEN + 1] = {0};
        uint16_t dst_port = 0, src_port = 0;
        src_port = ip_name(&client->addr, src, sizeof src);
        dst_port = ip_name(&client->dest_addr, dst, sizeof dst);
        logger_log(LOG_INFO, "%s:%d -> %s:%d", src, src_port, dst, dst_port);
    }
    uv_udp_send_t *write_req = malloc(sizeof(*write_req) + sizeof(uv_buf_t));
    uv_buf_t *buf = (uv_buf_t *)(write_req + 1);
    buf->base = (char *)data;
    buf->len = datalen;
    write_req->data = client;;
    uv_udp_send(write_req, &client->server_handle, buf, 1, server_addr, server_send_cb);
}
示例#24
0
        void UDPClient::send(std::string address, int port, char *data, int dataLength) {
            if (dataLength > 65000) {
                lWarnig("Data size bigger than 65KB. dest addr: " + address + " Port: " + std::to_string(port));
            }
            uv_udp_send_t send_req;

            uv_buf_t *bufw = (uv_buf_t *) malloc(sizeof(uv_buf_t));//alocated memory
            bufw->base = data;

            bufw->len = dataLength;
            //memcpy(bufw->base, data.c_str(), data.size());
            struct sockaddr *temp = networkUtility::uv_resolveAddress(&loop, address, std::to_string(port));
            uv_udp_send(&send_req, &client, bufw, 1, temp, on_send);
            uv_run(&loop, UV_RUN_DEFAULT);

            free(bufw->base);
            free(bufw);
            free(temp);
        }
示例#25
0
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);
}
示例#26
0
int Conn::send_udp(const char* buf, uint32_t len) {
	int r = -1;
	send_req_s* req = new send_req_s;
	CHK_COND(req);

	req->buf.base = new char[len];
	req->buf.len = len;

	memcpy(req->buf.base, buf, len);

	r = uv_udp_send((uv_udp_send_t*)req, _udp, &req->buf, 1, &_addr, on_send_done);
	if (r < 0) {
		SAFE_DELETE_ARRAY(req->buf.base);
		SAFE_DELETE(req);
		return -1;
	}

	return 0;
Exit0:
	return r;
}
示例#27
0
    int Udp::Send(/* [in] */ Buffer &buf,
                  /* [in] */ const Address &addr,
                  /* [in] */ SendHandler *handler)
    {
        assert(IsOpened());
        assert(! IsSending());

        int result = uv_udp_send(&m_sendReq,
                                 *this,
                                 buf,
                                 1,
                                 addr,
                                 OnSend);
        if(! result) {
            Ref();
            m_pSendBuffer = buf.Ref();
            m_pSendBuffer->Lock();
            m_pSendHandler = handler;
        }

        return result;
    }
示例#28
0
int rxs_sender_send(rxs_sender* net, uint8_t* buffer, uint32_t nbytes) {
  
  char* tmp = NULL;
  int r;
  uv_buf_t b;
  uv_udp_send_t* req = NULL;

  if (!net) { return -1; } 
  if (!buffer) { return -2; } 
  if (!nbytes) { return -3; } 

  /* we should copy the buf here... */
  tmp = (char*)malloc(nbytes);
  if (NULL == tmp) {
    printf("Error: cannot allocate BUF.\n");
    return -4;
  }
  memcpy(tmp, buffer, nbytes);

  /* @todo - the tests of libuv use this, but I'm pretty sure we want to allocate on the heap here */

  b = uv_buf_init((char*)tmp, nbytes);
  req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
  if (NULL == req) {
    printf("Error: cannot allocate REQ.\n");
    return -4;
  }
  memset(req, 0x00, sizeof(uv_udp_send_t));

  req->data = tmp;
  r = uv_udp_send(req, &net->send_sock, &b, 1, (const struct sockaddr*)&net->saddr, send_cb);

  if (r != 0) {
    printf("Error: cannot send udp: %s\n", uv_strerror(r));
    /* @todo -> shouldn't we free the allocated buffer here? */
    return -1;
  }
  return 0;
}
示例#29
0
文件: conn.c 项目: susemm/ckcpuv
/*@ckcpuv*/
int conn_send_udp(conn_t * thiz, const char* buf, uint32_t len)
{
	int r = -1;
	send_req_t * req = malloc(sizeof(send_req_t));
	CHK_COND(req);

	req->buf.base = malloc(sizeof(char) * len);
	req->buf.len = len;

	memcpy(req->buf.base, buf, len);

	r = uv_udp_send((uv_udp_send_t*)req, thiz->_udp, &req->buf, 1, &thiz->_addr, on_send_done);
	if (r < 0) {
		free(req->buf.base); /* TODO: ensure free */
		free(req); /* TODO: ensure free ? */
		return -1;
	}

	return 0;
Exit0:
	return r;
}
示例#30
0
void cjdnsadmin_fetch_peers(cjdnsadmin_t *adm)
{
    struct bencode *b = ben_dict();
    struct bencode *args = ben_dict();
    // TODO: fix memory leak
    ben_dict_set(b, ben_str("q"), ben_str("NodeStore_dumpTable"));
    ben_dict_set(b, ben_str("args"), args);
    ben_dict_set(args, ben_str("page"), ben_int(adm->fetch_peers_page));

    uv_buf_t buf;
    static char msg[256];
    buf.base = msg;
    buf.len = ben_encode2(msg, sizeof msg, b);

    AMNEW(uv_udp_send_t,writer);
    writer->data = b;
    uv_udp_send(writer,
            &adm->handle,
            &buf,1,
            adm->theaddr,
            on_written);

}