Exemple #1
0
/*
 * 创建一个Connection,连接到指定的地址,并加入到Socket的监听事件中。
 *
 * @param spec: 格式 [upd|tcp]:ip:port
 * @param streamer: 数据包的双向流,用packet创建,解包,组包。
 * @return  返回一个Connectoion对象指针
 */
Connection *Transport::connect(const char *spec, 
                               IPacketStreamer *streamer, 
                               bool autoReconn) {
    MutexGuard guard(&_stopMutex);
    if (_stop) { return NULL; }

    if (NULL == spec || NULL == streamer) {
        ANET_LOG(WARN, "Invalid parameters for connect(%,%p,%d)",
                 spec, streamer, autoReconn);
        return NULL;
    }
    char tmp[512];
    char *args[32];
    strncpy(tmp, spec, 512);
    tmp[511] = '\0';

    if (parseAddr(tmp, args, 32) != 3) {
        return NULL;
    }

    if (strcasecmp(args[0], "tcp") == 0) {
        char *host = args[1];
        int port = atoi(args[2]);

        // Socket
        Socket *socket = new Socket();
        assert(socket);
        if (!socket->setAddress(host, port)) {
            delete socket;
            ANET_LOG(ERROR, "setAddress error: %s:%d, %s", host, port, spec);
            return NULL;
        }

        // TCPComponent
        TCPComponent *component = new TCPComponent(this, socket);
        assert(component);
        if (!component->init()) {
            delete component;
            ANET_LOG(ERROR, "Failed to init TCPComponent(%s:%d).", host, port);
            return NULL;
        }
        component->createConnection(streamer, NULL);
        component->setAutoReconn(autoReconn);
        ANET_LOG(INFO, "socket(fd:%d) connected.", component->getSocket()->getSocketHandle());
        addToCheckingList(component);
        return component->getConnection();
    } else if (strcasecmp(args[0], "udp") == 0) {}

    return NULL;
}
/*
 * 创建一个Connection,连接到指定的地址,并加入到Socket的监听事件中。
 *
 * @param spec: 格式 [upd|tcp]:ip:port
 * @param streamer: 数据包的双向流,用packet创建,解包,组包。
 * @return  返回一个Connectoion对象指针
 */
Connection *Transport::connect(const char *spec, IPacketStreamer *streamer, bool autoReconn) {
    char tmp[1024];
    char *args[32];
    strncpy(tmp, spec, 1024);
    tmp[1023] = '\0';

    if (parseAddr(tmp, args, 32) != 3) {
        return NULL;
    }

    if (strcasecmp(args[0], "tcp") == 0) {
        char *host = args[1];
        int port = atoi(args[2]);

        // Socket
        Socket *socket = new Socket();

        if (!socket->setAddress(host, port)) {
            delete socket;
            TBSYS_LOG(ERROR, "设置setAddress错误: %s:%d, %s", host, port, spec);
            return NULL;
        }

        // TCPComponent
        TCPComponent *component = new TCPComponent(this, socket, streamer, NULL);
        // 设置是否自动重连
        component->setAutoReconn(autoReconn);
        if (!component->init()) {
            delete component;
            TBSYS_LOG(ERROR, "初始化失败TCPComponent: %s:%d", host, port);
            return NULL;
        }

        // 加入到iocomponents中,及注册可写到socketevent中
        addComponent(component, true, true);
        component->addRef();

        return component->getConnection();
    } else if (strcasecmp(args[0], "udp") == 0) {}

    return NULL;
}
Exemple #3
0
/**
* 当有数据可读时被Transport调用
*
* @return 是否成功
*/
bool TCPAcceptor::handleReadEvent() 
{
    Socket *socket;
    while ((socket = ((ServerSocket*)_socket)->accept()) != NULL) 
	{
        //TBSYS_LOG(INFO, "有新连接进来, fd: %d", socket->getSocketHandle());
        // TCPComponent, 在服务器端
        TCPComponent *component = new TCPComponent(_owner, socket, _streamer, _serverAdapter);

        if (!component->init(true)) {
            delete component;
            return true;
        }

        // 加入到iocomponents中,及注册可读到socketevent中
        _owner->addComponent(component, true, false);
    }

    return true;
}
Exemple #4
0
/**
* 当有数据可读时被Transport调用
*
* @return 是否成功
*/
bool TCPAcceptor::handleReadEvent() {
    lock();
    Socket *socket;
    while ((socket = ((ServerSocket*)_socket)->accept()) != NULL) {
        ANET_LOG(DEBUG, "New connection coming. fd=%d", socket->getSocketHandle());
        TCPComponent *component = new TCPComponent(_owner, socket);
        assert(component);

         if (!component->init(true)) {
             delete component;/**@TODO: may coredump?*/
             return true;
        }
        Connection *conn = component->createConnection(_streamer, _serverAdapter);
        conn->setQueueTimeout(_timeout);
         _owner->addToCheckingList(component);

        //transport's Read Write Thread and Timeout thread will have their
        //own reference of component, so we need to subRef() the initial one
        component->subRef();
    }
    unlock();
    return true;
}