Пример #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;
}