예제 #1
0
파일: network.cpp 프로젝트: voidah/tak
NetworkPeer Network::Connect(const std::string& host, unsigned short port, NetworkCallback* callback)
{
    AutoLock<Mutex> lock(m_mutex);

    if(!m_host)
        return NetworkPeer(0);

    if(m_type != CLIENT)
    {
        std::cout << "Connect operation valid only if type is client" << std::endl;
        return NetworkPeer(0);
    }

    ENetAddress address;
    enet_address_set_host(&address, host.c_str());
    address.port = port;

    ENetPeer* peer = enet_host_connect((ENetHost*)m_host, &address, NETWORK_CHANNEL_COUNT, 0);    

    if(!peer)
    {
        std::cout << "Failed to connect to " << host << ":" << port << std::endl;
        return NetworkPeer(0);
    }

    m_networkCallback = callback;

    ENetEvent event;
    if(enet_host_service((ENetHost*)m_host, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
        return NetworkPeer(peer);

    enet_peer_reset(peer);
    std::cout << "Failed to connect to " << host << ":" << port << std::endl;
    return NetworkPeer(0);
}
예제 #2
0
void DomainHandler::clearConnectionInfo() {
    _uuid = QUuid();

    _icePeer = NetworkPeer();

    if (requiresICE()) {
        // if we connected to this domain with ICE, re-set the socket so we reconnect through the ice-server
        _sockAddr.clear();
    }

    setIsConnected(false);
}
예제 #3
0
파일: network.cpp 프로젝트: voidah/tak
bool Network::Tick()
{
    AutoLock<Mutex> lock(m_mutex);

    if(!m_host)
        return false;

    if(!m_networkCallback)
        return false;

    ENetEvent event;

    while(enet_host_service((ENetHost*)m_host, &event, 0) > 0)
    {
        switch(event.type)
        {
            case ENET_EVENT_TYPE_CONNECT:
                m_networkCallback->OnNetworkConnect(NetworkPeer(event.peer), event.peer->data);
                break;

            case ENET_EVENT_TYPE_RECEIVE:
                m_networkCallback->OnNetworkReceive(NetworkPeer(event.peer), (const char*)event.packet->data, event.packet->dataLength, event.peer->data);
                enet_packet_destroy(event.packet);
                break;

            case ENET_EVENT_TYPE_DISCONNECT:
                if(m_type == CLIENT)
                    m_host = 0;
                m_networkCallback->OnNetworkDisconnect(NetworkPeer(event.peer), event.peer->data);
                event.peer->data = NULL;

            default:
                break;
        }

        return true; // event processed
    }

    return false;
}