// CreateConnection
status_t
ConnectionFactory::CreateConnection(const char* type, const char* parameters,
	Connection** _connection)
{
	if (!type)
		return B_BAD_VALUE;
	// create the connection
	Connection* connection = NULL;
	if (strcmp(type, "insecure") == 0)
		connection = new(nothrow) InsecureConnection;
	else if (strcmp(type, "port") == 0)
		connection = new(nothrow) PortConnection;
	else
		return B_BAD_VALUE;
	if (!connection)
		return B_NO_MEMORY;
	// init it
	status_t error = connection->Init(parameters);
	if (error != B_OK) {
		delete connection;
		return error;
	}
	*_connection = connection;
	return B_OK;
}
Beispiel #2
0
void EventModule::DoTcpAccept(int listen_fd, void* arg)
{
    Epoller* epoller_ = (Epoller*)arg;
    struct sockaddr_in sin;
    socklen_t len = sizeof(sin);

    ConfigModule* conf_module = FindModule<ConfigModule>(App::GetInstance());
    int32_t max_connection = conf_module->config().conn_pool_size();

    ConnMgrModule* conn_mgr_module = FindModule<ConnMgrModule>(App::GetInstance());
    int32_t cur_conn_map_size = conn_mgr_module->GetCurConnMapSize();

    if (cur_conn_map_size >= max_connection)
        return;

    int32_t conn_fd = accept(listen_fd, (struct sockaddr*)&sin, &len);
    if (conn_fd < 0) {
        if (errno != EAGAIN && errno != ECONNABORTED
                && errno != EPROTO && errno != EINTR) {
            PLOG(ERROR) << "accept";
        }
        return;
    }

    Epoller::SetNonBlock(conn_fd);
    Epoller::SetSocketOpt(conn_fd);

    Connection* conn = conn_mgr_module->CreateConn(conn_fd);
    conn->Init();
    if (conn == NULL) {
        LOG(ERROR) << "CreateConn error!";
        return;
    }

    LOG(INFO)
        << "accept ip[" << sin.sin_addr.s_addr
        << "] port[" << sin.sin_port
        << "] conn_fd[" << conn_fd
        << "]";

    int ret = epoller_->AddEvent(conn_fd, EVENT_READ, EventModule::DoTcpRead, (void*)epoller_);
    if (ret < 0)
        LOG(ERROR) << "epoller_->AddEvent error!";

    conn->set_conn_fd(conn_fd);
}