void NetClient::Disconnect() { m_bConnected = false; ResetNetwork(); NetSetClient(false); }
NetClient::~NetClient() { ResetNetwork(); NetSetClient(false); }
void NetServer::Disconnect() { ResetNetwork(); NetSetServer(false); }
bool NetClient::ConnectToServer(const char *pAddress,int port, NetDriver *pDriver) { m_bTimeSynced = false; m_bPrepareToRace = false; m_bBeginRace = false; m_timePhysics = -2.0; m_servertimedifference = 0.0; m_sendCarDataTime = 0.0; m_sendCtrlTime = 0.0; m_bPrepareToRace = false; m_bBeginRace = false; m_bConnected = false; m_pClient = NULL; m_pHost = NULL; #if (ENET_VERSION >= 0x010300) m_pClient = enet_host_create (NULL /* create a client host */, MAXNETWORKPLAYERS, 2, /*channel limit*/ 0/* downstream bandwidth */, 0/* upstream bandwidth */); #else m_pClient = enet_host_create (NULL /* create a client host */, MAXNETWORKPLAYERS, 0/* downstream bandwidth */, 0/* upstream bandwidth */); #endif if (m_pClient == NULL) { GfLogError ("An error occurred while trying to create an ENet client host.\n"); ResetNetwork(); return false; } ENetAddress caddress; caddress.host = ENET_HOST_ANY; /* Bind the server to port*/ caddress.port = SPEEDDREAMSPEERPORT; #if (ENET_VERSION >= 0x010300) m_pHost = enet_host_create (&caddress /* create a peer host */, MAXNETWORKPLAYERS, 2, /*channel limit*/ 0/* downstream bandwidth */, 0/* upstream bandwidth */); #else m_pHost = enet_host_create (&caddress /* create a peer host */, MAXNETWORKPLAYERS, 0/* downstream bandwidth */, 0/* upstream bandwidth */); #endif if(m_pHost==NULL) { //try the other ports for (int i=1;i<5;i++) { caddress.port++; #if (ENET_VERSION >= 0x010300) m_pHost = enet_host_create (&caddress,MAXNETWORKPLAYERS,2,0,0); #else m_pHost = enet_host_create (&caddress,MAXNETWORKPLAYERS,0,0); #endif if(m_pHost) break; } if (m_pHost == NULL) { GfLogError("Unable to setup client listener\n"); return false; } } ENetAddress address; ENetEvent event; enet_address_set_host (& address, pAddress); address.port = (enet_uint16)port; /* Initiate the connection, allocating the two channels 0 and 1. */ GfLogError ("Initiating network connection to host '%s:%d' ...\n", pAddress, port); #if (ENET_VERSION >= 0x010300) m_pServer = enet_host_connect (m_pClient, & address, 2, 0); #else m_pServer = enet_host_connect (m_pClient, & address, 2); #endif if (m_pServer == NULL) { GfLogInfo ("No available peers for initiating an ENet connection.\n"); ResetNetwork(); return false; } /* Wait up to 5 seconds for the connection attempt to succeed. */ if (enet_host_service (m_pClient, & event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { m_address.host = m_pClient->address.host; m_address.port = m_pClient->address.port; m_bConnected = true; GfLogInfo ("Network connection accepted.\n"); } else { m_bConnected = false; ResetNetwork(); GfLogError ("Network connection refused.\n"); return false; } m_eClientAccepted = PROCESSINGCLIENT; SendDriverInfoPacket(pDriver); //Wait for server to accept or reject GfLogInfo ("Sent local driver info to the network server : waiting ...\n"); while(m_eClientAccepted == PROCESSINGCLIENT) SDL_Delay(50); if (m_eClientAccepted == CLIENTREJECTED) { m_bConnected = false; ResetNetwork(); return false; } else GfLogInfo ("Driver info accepted by the network server.\n"); return m_bConnected; }
NetServer::~NetServer() { ResetNetwork(); NetSetServer(false); }