Exemplo n.º 1
0
void
source_accept_cb(uv_stream_t *server, int status) {
    struct source_context *source = new_source();
    struct target_context *target = new_target();

    source->target = target;
    target->source = source;

    uv_tcp_init(server->loop, &source->handle.tcp);
    uv_tcp_init(server->loop, &target->handle.tcp);

    uv_tcp_nodelay(&source->handle.tcp, 0);
    uv_tcp_nodelay(&target->handle.tcp, 0);
    uv_tcp_keepalive(&source->handle.tcp, 1, 60);
    uv_tcp_keepalive(&target->handle.tcp, 1, 60);

    int rc = uv_accept(server, &source->handle.stream);
    if (rc == 0) {
        connect_to_target(target);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_source(source);
        close_target(target);
    }
}
Exemplo n.º 2
0
static void connect_cb(uv_stream_t* listener, int status)
{
	int n;

	if (status) {
		SHOW_UV_ERROR(listener->loop);
		return;
	}

	server_ctx *ctx = calloc(1, sizeof(server_ctx));
	ctx->client.data = ctx;
	ctx->remote.data = ctx;

	ctx->handshake_buffer = calloc(1, HANDSHAKE_BUFFER_SIZE);
	if (!ctx || !ctx->handshake_buffer)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_init(listener->loop, &ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_accept(listener, (uv_stream_t *)(void *)&ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_nodelay(&ctx->client, 1);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_read_start((uv_stream_t *)(void *)&ctx->client, client_handshake_alloc_cb, client_handshake_read_cb);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);
}
Exemplo n.º 3
0
void on_new_connection(uv_stream_t *server, int status) {
  if (status == -1) {
    printf("on_new_connection: ERROR\n");
    // error!
    return;
  }

  Connection *conn = new Connection(10000);

  int err;

  err = uv_tcp_init(loop, (uv_tcp_t*) conn);
  assert(err == 0);
  err = uv_tcp_nodelay((uv_tcp_t*) conn, 1);
  assert(err == 0);

  if (uv_accept(server, (uv_stream_t*) conn) == 0) {
    err = uv_read_start((uv_stream_t*) conn, alloc_read_buffer, on_read_cb);
    assert(err == 0);
  } else {
    printf("ACCEPT failed\n");
    delete conn;
  }
  /*else {
    uv_close((uv_handle_t*) client, NULL);
  }*/
}
Exemplo n.º 4
0
Arquivo: tcp_uv.c Projeto: yugui/grpc
grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle,
                               grpc_resource_quota *resource_quota,
                               char *peer_string) {
  grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));

  if (grpc_tcp_trace) {
    gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp);
  }

  /* Disable Nagle's Algorithm */
  uv_tcp_nodelay(handle, 1);

  memset(tcp, 0, sizeof(grpc_tcp));
  tcp->base.vtable = &vtable;
  tcp->handle = handle;
  handle->data = tcp;
  gpr_ref_init(&tcp->refcount, 1);
  tcp->peer_string = gpr_strdup(peer_string);
  tcp->shutting_down = false;
  tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
  /* Tell network status tracking code about the new endpoint */
  grpc_network_status_register_endpoint(&tcp->base);

#ifndef GRPC_UV_TCP_HOLD_LOOP
  uv_unref((uv_handle_t *)handle);
#endif

  return &tcp->base;
}
Exemplo n.º 5
0
void ipc_connection_cb(uv_stream_t* ipc_pipe, int status)
{
    int rc;
    struct ipc_server_ctx* sc;
    struct ipc_peer_ctx* pc;
    uv_loop_t* loop;
    uv_buf_t buf;
    
    loop = ipc_pipe->loop;
    buf = uv_buf_init("PING", 4);
    sc = container_of(ipc_pipe, struct ipc_server_ctx, ipc_pipe);
    pc = calloc(1, sizeof(*pc));
    //ASSERT(pc != NULL);
    
    if (ipc_pipe->type == UV_TCP) {
        rc = uv_tcp_init(loop, (uv_tcp_t*) &pc->peer_handle);
        if (sc->tcp_nodelay) {
            rc = uv_tcp_nodelay((uv_tcp_t*) &pc->peer_handle, 1);
        }
    }
    else if (ipc_pipe->type == UV_NAMED_PIPE)
        rc = uv_pipe_init(loop, (uv_pipe_t*) &pc->peer_handle, 1);
    
    rc = uv_accept(ipc_pipe, (uv_stream_t*) &pc->peer_handle);
    rc = uv_write2(&pc->write_req,
                          (uv_stream_t*) &pc->peer_handle,
                          &buf,
                          1,
                          (uv_stream_t*) &sc->server_handle,
                          ipc_write_cb);
    
    if (--sc->num_connects == 0)
        uv_close((uv_handle_t*) ipc_pipe, NULL);
}
Exemplo n.º 6
0
void io_create(uv_loop_t *loop, uv_handle_t *handle, int type)
{
	if (type == SOCK_DGRAM) {
		uv_udp_init(loop, (uv_udp_t *)handle);
	} else {
		uv_tcp_init(loop, (uv_tcp_t *)handle);
		uv_tcp_nodelay((uv_tcp_t *)handle, 1);
	}
}
Exemplo n.º 7
0
Arquivo: tcp.c Projeto: senlinms/luv
static int luv_tcp_nodelay(lua_State* L) {
  uv_tcp_t* handle = luv_check_tcp(L, 1);
  int ret, enable;
  luaL_checktype(L, 2, LUA_TBOOLEAN);
  enable = lua_toboolean(L, 2);
  ret = uv_tcp_nodelay(handle, enable);
  if (ret < 0) return luv_error(L, ret);
  lua_pushinteger(L, ret);
  return 1;
}
Exemplo n.º 8
0
int luv_tcp_nodelay (lua_State* L) {
  uv_tcp_t* handle = (uv_tcp_t*)luv_checkudata(L, 1, "tcp");
  int enable = lua_toboolean(L, 2);

  if (uv_tcp_nodelay(handle, enable)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "tcp_nodelay: %s", uv_strerror(err));
  }
  return 0;
}
Exemplo n.º 9
0
bool TCPClient::SetNoDelay(bool enable)
{
    //http://blog.csdn.net/u011133100/article/details/21485983
    int iret = uv_tcp_nodelay(&client_handle_->tcphandle, enable ? 1 : 0);
    if (iret) {
        errmsg_ = GetUVError(iret);
        LOG(ERROR)<<(errmsg_);
        return false;
    }
    return true;
}
Exemplo n.º 10
0
static int lluv_tcp_nodelay(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  int enable = lua_toboolean(L, 2);
  int err = uv_tcp_nodelay(LLUV_H(handle, uv_tcp_t), enable);

  lua_settop(L, 1);

  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return 1;
}
Exemplo n.º 11
0
void io_create(uv_loop_t *loop, uv_handle_t *handle, int type)
{
	if (type == SOCK_DGRAM) {
		uv_udp_init(loop, (uv_udp_t *)handle);
	} else {
		uv_tcp_init(loop, (uv_tcp_t *)handle);
		uv_tcp_nodelay((uv_tcp_t *)handle, 1);
	}

	struct worker_ctx *worker = loop->data;
	handle->data = session_borrow(worker);
	assert(handle->data);
}
Exemplo n.º 12
0
static void
on_connection(uv_stream_t* server, int status) {
  uv_stream_t* stream;
  int r;

  if (status != 0) {
    fprintf(stderr, "Connect error: %s: %s\n", uv_err_name(status), uv_strerror(status));
    return;
  }

  stream = malloc(sizeof(uv_tcp_t));
  if (stream == NULL) {
    fprintf(stderr, "Allocate error: %s\n", strerror(errno));
    return;
  }

  r = uv_tcp_init(loop, (uv_tcp_t*) stream);
  if (r) {
    fprintf(stderr, "Socket creation error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    return;
  }

  r = uv_tcp_simultaneous_accepts((uv_tcp_t*) stream, 1);
  if (r) {
    fprintf(stderr, "Flag error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    return;
  }

  r = uv_accept(server, stream);
  if (r) {
    fprintf(stderr, "Accept error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    return;
  }

  r = uv_tcp_nodelay((uv_tcp_t*) stream, 1);
  if (r) {
    fprintf(stderr, "Flag error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    return;
  }

  r = uv_read_start(stream, on_alloc, on_read);
  if (r) {
    fprintf(stderr, "Read error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    uv_close((uv_handle_t*) stream, on_close);
  }
}
Exemplo n.º 13
0
Connection::Connection(uv_loop_t* loop,
                       const Config& config,
                       Metrics* metrics,
                       const Address& address,
                       const std::string& keyspace,
                       int protocol_version,
                       Listener* listener)
    : state_(CONNECTION_STATE_NEW)
    , is_defunct_(false)
    , is_invalid_protocol_(false)
    , is_registered_for_events_(false)
    , is_available_(false)
    , ssl_error_code_(CASS_OK)
    , pending_writes_size_(0)
    , loop_(loop)
    , config_(config)
    , metrics_(metrics)
    , address_(address)
    , addr_string_(address.to_string())
    , keyspace_(keyspace)
    , protocol_version_(protocol_version)
    , listener_(listener)
    , response_(new ResponseMessage())
    , version_("3.0.0")
    , connect_timer_(NULL)
    , ssl_session_(NULL) {
  socket_.data = this;
  uv_tcp_init(loop_, &socket_);

  if (uv_tcp_nodelay(&socket_,
                     config.tcp_nodelay_enable() ? 1 : 0) != 0) {
    LOG_WARN("Unable to set tcp nodelay");
  }

  if (uv_tcp_keepalive(&socket_,
                      config.tcp_keepalive_enable() ? 1 : 0,
                      config.tcp_keepalive_delay_secs()) != 0) {
    LOG_WARN("Unable to set tcp keepalive");
  }

  SslContext* ssl_context = config_.ssl_context();
  if (ssl_context != NULL) {
    ssl_session_.reset(ssl_context->create_session(address_));
  }
}
Exemplo n.º 14
0
static void connect_cb(uv_stream_t* listener, int status)
{
	int n;

	if (status) {
		SHOW_UV_ERROR(listener->loop);
		return;
	}

	server_ctx *ctx = calloc(1, sizeof(server_ctx));
	ctx->handshake_buffer = calloc(1, HANDSHAKE_BUFFER_SIZE);

	if (!ctx || !ctx->handshake_buffer)
		FATAL("malloc() failed!");

	ctx->client.data = ctx;
	ctx->remote.data = ctx;
	
	make_encryptor(&crypto, &ctx->encoder, 0, NULL);

	n = uv_tcp_init(listener->loop, &ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_accept(listener, (uv_stream_t *)(void *)&ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_nodelay(&ctx->client, 1);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	#ifdef KEEPALIVE_TIMEOUT
	n = uv_tcp_keepalive(&ctx->client, 1, KEEPALIVE_TIMEOUT);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);
	#endif /* KEEPALIVE_TIMEOUT */

	n = uv_read_start((uv_stream_t *)(void *)&ctx->client, client_handshake_alloc_cb, client_handshake_read_cb);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	LOGCONN(&ctx->client, "Accepted connection from %s");
}
Exemplo n.º 15
0
static int tcp_nodelay(lua_State *l)
{
    tcp_udata_t        *udata = connection_udata(l);
    ls_tcp_t           *handle = udata->handle;
    int                 enable;

    if (handle == NULL)
        return ls_error_return(l, LS_ERRCODE_EOF, "tcp connection closed");

    enable = lua_toboolean(l, 2);

    if (uv_tcp_nodelay(&handle->handle, enable))
    {
        return ls_error_return(l, LS_ERRCODE_ERROR, "set nodelay failed.");
    }
    
    lua_pushboolean(l, 1);
    return 1;
}
Exemplo n.º 16
0
Error TLSConnectionPrivate::Connect(const std::string &ipaddr, int port, TLSConnectionOptions *opts) {
	int err;

	loop_ = uv_loop_new();

	struct sockaddr_in addr = uv_ip4_addr(ipaddr.c_str(), port);

	err = uv_tcp_init(loop_, &tcpsock_);
	if (err != UV_OK) {
		return UVUtils::ErrorFromLastUVError(loop_);
	}

	if (opts != nullptr) {
		uv_tcp_nodelay(&tcpsock_, opts->tcp_no_delay ? 1 : 0);
	}

	tcpconn_.data = static_cast<void *>(this);
	tcpsock_.data = static_cast<void *>(this);
	err = uv_tcp_connect(&tcpconn_, &tcpsock_, addr, TLSConnectionPrivate::OnConnect);
	if (err != UV_OK) {
		return UVUtils::ErrorFromLastUVError(loop_);
	}

	uv_mutex_init(&wqlock_);
	uv_async_init(loop_, &wqasync_, TLSConnectionPrivate::OnDrainWriteQueue);
	wqasync_.data = this;

	uv_async_init(loop_, &dcasync_, TLSConnectionPrivate::OnDisconnectRequest);
	dcasync_.data = this;

	state_ = TLS_CONNECTION_STATE_PRE_CONNECT;

	err = uv_thread_create(&thread_, TLSConnectionPrivate::TLSConnectionThread, this);
	if (err  == -1) {
		return Error::ErrorFromDescription(
			std::string("TLSConnection"),
			0L,
			std::string("unable to create connection thread")
		);
	}

	return Error::NoError();
}
Exemplo n.º 17
0
Connection::Connection(uv_loop_t* loop,
                       const Config& config,
                       Metrics* metrics,
                       const Host::ConstPtr& host,
                       const std::string& keyspace,
                       int protocol_version,
                       Listener* listener)
    : state_(CONNECTION_STATE_NEW)
    , error_code_(CONNECTION_OK)
    , ssl_error_code_(CASS_OK)
    , pending_writes_size_(0)
    , loop_(loop)
    , config_(config)
    , metrics_(metrics)
    , host_(host)
    , keyspace_(keyspace)
    , protocol_version_(protocol_version)
    , listener_(listener)
    , response_(new ResponseMessage())
    , stream_manager_(protocol_version)
    , ssl_session_(NULL)
    , heartbeat_outstanding_(false) {
  socket_.data = this;
  uv_tcp_init(loop_, &socket_);

  if (uv_tcp_nodelay(&socket_,
                     config.tcp_nodelay_enable() ? 1 : 0) != 0) {
    LOG_WARN("Unable to set tcp nodelay");
  }

  if (uv_tcp_keepalive(&socket_,
                      config.tcp_keepalive_enable() ? 1 : 0,
                      config.tcp_keepalive_delay_secs()) != 0) {
    LOG_WARN("Unable to set tcp keepalive");
  }

  SslContext* ssl_context = config_.ssl_context();
  if (ssl_context != NULL) {
    ssl_session_.reset(ssl_context->create_session(host));
  }
}
Exemplo n.º 18
0
Arquivo: tcp.c Projeto: eagles125/pyuv
static PyObject *
TCP_func_nodelay(TCP *self, PyObject *args)
{
    int r;
    PyObject *enable;

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

    if (!PyArg_ParseTuple(args, "O!:nodelay", &PyBool_Type, &enable)) {
        return NULL;
    }

    r = uv_tcp_nodelay(&self->tcp_h, (enable == Py_True) ? 1 : 0);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Exemplo n.º 19
0
/* Set up an IPC pipe server that hands out listen sockets to the worker
 * threads. It's kind of cumbersome for such a simple operation, maybe we
 * should revive uv_import() and uv_export().
 */
void start_connection_dispatching(uv_handle_type type, unsigned int num_servers, struct server_ctx* servers, char* listen_address, int listen_port, bool tcp_nodelay)
{
    int rc;
    struct ipc_server_ctx ctx;
    uv_loop_t* loop;
    unsigned int i;
    
    loop = uv_default_loop();
    ctx.num_connects = num_servers;
    ctx.tcp_nodelay = tcp_nodelay;
    
    if (type == UV_TCP)
    {
        uv_ip4_addr(listen_address, listen_port, &listen_addr);
        
        rc = uv_tcp_init(loop, (uv_tcp_t*) &ctx.server_handle);
        
        if (ctx.tcp_nodelay) {
            rc = uv_tcp_nodelay((uv_tcp_t*) &ctx.server_handle, 1);
        }

        rc = uv_tcp_bind((uv_tcp_t*) &ctx.server_handle, (const struct sockaddr*)&listen_addr, 0);
        print_configuration();
        printf("Listening...\n");
    }
    
    rc = uv_pipe_init(loop, &ctx.ipc_pipe, 1);
    rc = uv_pipe_bind(&ctx.ipc_pipe, "HAYWIRE_CONNECTION_DISPATCH_PIPE_NAME");
    rc = uv_listen((uv_stream_t*) &ctx.ipc_pipe, 128, ipc_connection_cb);
    
    for (i = 0; i < num_servers; i++)
        uv_sem_post(&servers[i].semaphore);
    
    rc = uv_run(loop, UV_RUN_DEFAULT);
    uv_close((uv_handle_t*) &ctx.server_handle, NULL);
    rc = uv_run(loop, UV_RUN_DEFAULT);
    
    for (i = 0; i < num_servers; i++)
        uv_sem_wait(&servers[i].semaphore);
}
Exemplo n.º 20
0
void Client::connect(sockaddr *addr)
{
    setState(ConnectingState);

    reinterpret_cast<struct sockaddr_in*>(addr)->sin_port = htons(m_url.port());
    delete m_socket;

    uv_connect_t *req = new uv_connect_t;
    req->data = this;

    m_socket = new uv_tcp_t;
    m_socket->data = this;

    uv_tcp_init(uv_default_loop(), m_socket);
    uv_tcp_nodelay(m_socket, 1);

#   ifndef WIN32
    uv_tcp_keepalive(m_socket, 1, 60);
#   endif

    uv_tcp_connect(req, m_socket, reinterpret_cast<const sockaddr*>(addr), Client::onConnect);
}
Exemplo n.º 21
0
static int cntl_socket(lcb_io_opt_t iobase, lcb_sockdata_t *sockbase,
    int mode, int option, void *arg)
{
    my_sockdata_t *sd = (my_sockdata_t *)sockbase;
    int rv;

    switch (option) {
    case LCB_IO_CNTL_TCP_NODELAY:
        if (mode == LCB_IO_CNTL_GET) {
            rv = uv_tcp_nodelay(&sd->tcp.t, *(int *)arg);
            if (rv != 0) {
                set_last_error((my_iops_t*)iobase, rv);
            }
            return rv;
        } else {
            LCB_IOPS_ERRNO(iobase) = ENOTSUP;
            return -1;
        }
    default:
        LCB_IOPS_ERRNO(iobase) = ENOTSUP;
        return -1;
    }
}
Exemplo n.º 22
0
static void
accept_cb(uv_stream_t *stream, int status) {
    struct tundev_context *ctx = stream->data;
    struct client_context *client = new_client(ctx->tun->mtu);

    uv_tcp_init(stream->loop, &client->handle.tcp);
    int rc = uv_accept(stream, &client->handle.stream);
    if (rc == 0) {
        int len = sizeof(struct sockaddr);
        uv_tcp_getpeername(&client->handle.tcp, &client->addr, &len);
        client->handle.stream.data = ctx;
        uv_tcp_nodelay(&client->handle.tcp, 1);
        uv_tcp_keepalive(&client->handle.tcp, 1, 60);

        client->packet.size = 512;
        ctx->connect = AUTHING;

        uv_read_start(&client->handle.stream, alloc_cb, recv_cb);

    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_client(client);
    }
}
Exemplo n.º 23
0
void on_new_connection(uv_stream_t *stream, int status) {
  struct tcp_client *client;
  int r;
  
  if (status != 0) {
    // TODO(lijie3): ignore this error ?
    fprintf(stderr, "create new client connection failed!\n");
    return;
  }

  client = (struct tcp_client *)malloc(sizeof(struct tcp_client));
  r = uv_tcp_init(&loop, &client->handle);
  assert(r == 0);
  uv_tcp_nodelay(&client->handle, 1);

  r = uv_accept(stream, (uv_stream_t *)&client->handle);
  assert(r == 0);

  client->recvbuf = (char *)malloc(kMaxRecvBufferSize);
  client->rdpos = 0;

  r = uv_read_start((uv_stream_t *)&client->handle, on_alloc, on_read);
  assert(r == 0);
}
Exemplo n.º 24
0
void TCPSocket::setNoDelay(bool enable) 
{
    init();
    int r = uv_tcp_nodelay(ptr<uv_tcp_t>(), enable ? 1 : 0);
    if (r) setUVError("TCP socket error", r);
}
Exemplo n.º 25
0
extern "C" int
rust_uv_tcp_nodelay
(uv_tcp_t* handle, int enable) {
    return uv_tcp_nodelay(handle, enable);
}
Exemplo n.º 26
0
Arquivo: tcp.cpp Projeto: Xsoda/uvplus
int uvplus_tcp::nodelay(int enable) {
  auto tcp = (uv_tcp_t *)context_ptr();
  return uv_tcp_nodelay(tcp, enable);
}
Exemplo n.º 27
0
void bud_client_create(bud_config_t* config, uv_stream_t* stream) {
  int r;
  bud_client_t* client;
  bud_client_error_t cerr;
  BIO* enc_in;
  BIO* enc_out;
#ifdef SSL_MODE_RELEASE_BUFFERS
  long mode;
#endif  /* SSL_MODE_RELEASE_BUFFERS */

  client = malloc(sizeof(*client));
  if (client == NULL)
    return;

  client->config = config;
  client->ssl = NULL;
  client->last_handshake = 0;
  client->handshakes = 0;
  client->connect = kBudProgressNone;
  client->close = kBudProgressNone;
  client->cycle = kBudProgressNone;
  client->recycle = 0;
  client->destroy_waiting = 0;

  client->id = bud_config_get_client_id(config);

  client->async_hello = kBudProgressDone;
  if (config->sni.enabled || config->stapling.enabled)
    client->async_hello = kBudProgressNone;

  /* SNI */
  client->sni_req = NULL;
  client->sni_ctx.ctx = NULL;

  /* Stapling */
  client->stapling_cache_req = NULL;
  client->stapling_req = NULL;
  client->stapling_ocsp_resp = NULL;

  /* Availability */
  client->retry = kBudProgressNone;
  client->retry_count = 0;
  client->retry_timer.data = client;
  client->backend_list = NULL;
  client->selected_backend = NULL;

  /* Proxyline */
  client->proxyline_waiting = 2;

  /* X-Forward */
  client->xforward.skip = 0;
  client->xforward.crlf = 0;

  r = uv_timer_init(config->loop, &client->retry_timer);
  if (r != 0)
    goto failed_timer_init;
  client->destroy_waiting++;

  /* Initialize buffers */
  bud_client_side_init(&client->frontend, kBudFrontend, client);
  bud_client_side_init(&client->backend, kBudBackend, client);

  /**
   * Accept client on frontend
   */
  r = uv_tcp_init(config->loop, &client->frontend.tcp);
  if (r != 0)
    goto failed_tcp_in_init;

  client->destroy_waiting++;
  r = uv_accept(stream, (uv_stream_t*) &client->frontend.tcp);
  if (r != 0)
    goto failed_accept;

  cerr = bud_client_read_start(client, &client->frontend);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;
  client->frontend.reading = kBudProgressRunning;

  /* Fill hosts */
  cerr = bud_client_fill_host(client, &client->local);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  cerr = bud_client_fill_host(client, &client->remote);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  /*
   * Select a backend and connect to it, or wait for a backend to become
   * alive again.
   */
  /* SNI backend comes from `backend` or sni callback */
  client->backend_list = &config->contexts[0].backend;
  client->balance = config->balance_e;
  if (client->balance == kBudBalanceSNI) {
    client->selected_backend = NULL;
    client->connect = kBudProgressRunning;
  } else {
    client->selected_backend = bud_select_backend(client);
  }

  /* No backend can be selected yet, wait for SNI */
  if (client->selected_backend == NULL) {
    client->backend.close = kBudProgressDone;
    cerr = bud_client_ok(&client->backend);

  /* No backend alive, try reconnecting */
  } else if (client->selected_backend->dead) {
    DBG_LN(&client->backend, "all backends dead, scheduling reconnection");
    cerr = bud_client_retry(client);

  /* Backend alive - connect immediately */
  } else {
    cerr = bud_client_connect(client);
  }
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  /* Adjust sockets */
  r = uv_tcp_nodelay(&client->frontend.tcp, 1);
  if (r == 0 && config->frontend.keepalive > 0)
    r = uv_tcp_keepalive(&client->frontend.tcp, 1, config->frontend.keepalive);
  if (r != 0)
    goto failed_connect;

  /* Initialize SSL */

  /* First context is always default */
  client->ssl = SSL_new(config->contexts[0].ctx);
  if (client->ssl == NULL)
    goto failed_connect;

  if (!SSL_set_ex_data(client->ssl, kBudSSLClientIndex, client))
    goto failed_connect;

  SSL_set_cert_cb(client->ssl, bud_client_ssl_cert_cb, client);
  SSL_set_info_callback(client->ssl, bud_client_ssl_info_cb);

  enc_in = bud_bio_new(&client->frontend.input);
  if (enc_in == NULL)
    goto failed_connect;
  enc_out = bud_bio_new(&client->frontend.output);
  if (enc_out == NULL) {
    BIO_free_all(enc_in);
    goto failed_connect;
  }
  SSL_set_bio(client->ssl, enc_in, enc_out);

#ifdef SSL_MODE_RELEASE_BUFFERS
  mode = SSL_get_mode(client->ssl);
  SSL_set_mode(client->ssl, mode | SSL_MODE_RELEASE_BUFFERS);
#endif  /* SSL_MODE_RELEASE_BUFFERS */

  SSL_set_accept_state(client->ssl);

  bud_trace_frontend_accept(client);
  DBG_LN(&client->frontend, "new");
  return;

failed_connect:
  client->connect = kBudProgressDone;
  client->close = kBudProgressDone;
  uv_close((uv_handle_t*) &client->backend.tcp, bud_client_close_cb);

failed_accept:
  uv_close((uv_handle_t*) &client->frontend.tcp, bud_client_close_cb);

failed_tcp_in_init:
  uv_close((uv_handle_t*) &client->retry_timer, bud_client_close_cb);
  return;

failed_timer_init:
  free(client);
}
Exemplo n.º 28
0
int  UvTcpSocket::nodelay(bool enable)
{
	return uv_tcp_nodelay(m_uv_tcp, enable?1:0);
}
Exemplo n.º 29
0
 bool nodelay(bool enable) { return uv_tcp_nodelay(get<uv_tcp_t>(), enable ? 1 : 0) == 0; }
Exemplo n.º 30
0
Arquivo: tcp.hpp Projeto: skypjack/uvw
 /**
  * @brief Enables/Disables Nagle’s algorithm.
  * @param value True to enable it, false otherwise.
  * @return True in case of success, false otherwise.
  */
 bool noDelay(bool value = false) {
     return (0 == uv_tcp_nodelay(get(), value));
 }