void trydisconnect() { if(connpeer) abortconnect(); else if(curpeer || connectedlocally) { if(verbose) conoutft(CON_MESG, "\faattempting to disconnect..."); disconnect(0, !discmillis); } else conoutft(CON_MESG, "\frnot connected"); }
bool multiplayer(bool msg) { // check if we're playing alone int n = client::otherclients(); if(n && msg) conoutft(CON_MESG, "\froperation not available with other clients"); return n > 0; }
void connectserv(const char *name, int port, const char *password) { abortconnect(); if(!port) port = ENG_SERVER_PORT; ENetAddress address; address.port = port; setsvar("serveraddress", ""); setvar("serverconport", 0); if(name && *name) { addserver(name, port); conoutft(CON_MESG, "\faattempting to connect to %s:[%d]", name, port); if(!resolverwait(name, &address)) { conoutft(CON_MESG, "\frcould not resolve host %s", name); connectfail(); return; } setsvar("serveraddress", name); setvar("serverconport", port); } else { conoutft(CON_MESG, "\faattempting to connect to a local server"); address.host = ENET_HOST_BROADCAST; } if(!clienthost) clienthost = enet_host_create(NULL, 2, server::numchannels(), rate, rate); if(clienthost) { connpeer = enet_host_connect(clienthost, &address, server::numchannels(), 0); enet_host_flush(clienthost); connmillis = totalmillis; connattempts = 0; client::connectattempt(name ? name : "", port, password ? password : "", address); conoutft(CON_MESG, "\fgconnecting to %s:[%d]", name != NULL ? name : "local server", port); } else { conoutft(CON_MESG, "\frfailed creating client socket"); connectfail(); } }
void abortconnect(bool msg) { if(!connpeer) return; client::connectfail(); if(msg) conoutft(CON_MESG, "\faaborting connection attempt"); if(connpeer->state!=ENET_PEER_STATE_DISCONNECTED) enet_peer_reset(connpeer); connpeer = NULL; if(curpeer) return; enet_host_destroy(clienthost); clienthost = NULL; }
void gets2c() // get updates from the server { ENetEvent event; if(!clienthost) return; if(connpeer && totalmillis/3000 > connmillis/3000) { connmillis = totalmillis; ++connattempts; if(connattempts > 3) { conoutft(CON_MESG, "\frcould not connect to server"); connectfail(); return; } else conoutft(CON_MESG, "\faconnection attempt %d", connattempts); } while(clienthost && enet_host_service(clienthost, &event, 0)>0) switch(event.type) { case ENET_EVENT_TYPE_CONNECT: disconnect(1); curpeer = connpeer; connpeer = NULL; conoutft(CON_MESG, "\fgconnected to server"); throttle(); if(rate) setrate(rate); client::gameconnect(true); break; case ENET_EVENT_TYPE_RECEIVE: if(discmillis) conoutft(CON_MESG, "\faattempting to disconnect..."); else localservertoclient(event.channelID, event.packet); enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: if(event.data>=DISC_NUM) event.data = DISC_NONE; if(event.peer==connpeer) { conoutft(CON_MESG, "\frcould not connect to server"); connectfail(); } else { if(!discmillis || event.data) conoutft(CON_MESG, "\frserver network error, disconnecting (%s) ...", disc_reasons[event.data]); disconnect(); } return; default: break; } }
void disconnect(int onlyclean, int async) { bool cleanup = onlyclean!=0; if(curpeer || connectedlocally) { if(curpeer) { if(!discmillis) { enet_peer_disconnect(curpeer, DISC_NONE); if(clienthost) enet_host_flush(clienthost); discmillis = totalmillis; } if(curpeer->state!=ENET_PEER_STATE_DISCONNECTED) { if(async) return; enet_peer_reset(curpeer); } curpeer = NULL; } discmillis = 0; conoutft(CON_MESG, "\frdisconnected"); cleanup = true; } if(!connpeer && clienthost) { enet_host_destroy(clienthost); clienthost = NULL; } if(cleanup) { client::gamedisconnect(onlyclean); localdisconnect(); } if(!onlyclean) localconnect(false); }
void neterr(const char *s) { conoutft(CON_MESG, "\frillegal network message (%s)", s); disconnect(); }