private void AcceptServer(IAsyncResult ar) { bool restarted = false; try { Socket tempServer = _serversListener.EndAccept(ar); _serversListener.BeginAccept(new AsyncCallback(AcceptServer), null); restarted = true; if (servers.Count < Constants.MAX_SERVERS) { ServerDetails newServer = new ServerDetails(); servers.Add(newServer); newServer.socket = tempServer; newServer.buffer = new byte[newServer.socket.ReceiveBufferSize]; newServer.socket.BeginReceive(newServer.buffer, 0, newServer.buffer.Length, SocketFlags.None, new AsyncCallback(OnServerReceive), newServer); } else RejectConnection("Rejecting Server -> too many servers registered...", tempServer); } catch (Exception ex) { if (!restarted) _serversListener.BeginAccept(new AsyncCallback(AcceptServer), null); appendLog("Exception in: Server Listener - AcceptServer() ->" + ex.Message); //MessageBox.Show(ex.Message, "Server Listener - AcceptServer()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void RemoveServer(ServerDetails server) { if (server.socket != null && SocketConnected(server.socket)) { server.socket.Shutdown(SocketShutdown.Both); server.socket.Close(); } servers.Remove(server); DisconnectClientsForThisServer(server); }
public void DisconnectClientsForThisServer(ServerDetails server) { foreach (Candidate c in clients) { if (c != null) { if (c.selectedServerIndex == server.serverIndex) { RemoveClient(c); } } } }
private static void RegisterServer(PacketContent pc, ServerDetails serverDetails) { appendLog(".::Received server connection::. "); serverDetails.serverIndex = pc.ReadLong(); serverDetails.name = pc.ReadString(); serverDetails.ip = pc.ReadString(); serverDetails.port = pc.ReadLong(); serverDetails.location = pc.ReadString(); serverDetails.donorOnly = pc.ReadFlag(); serverDetails.registered = true; appendLog(".::Name: " + serverDetails.name); appendLog(".::IP: " + serverDetails.ip); appendLog(".::Port: " + serverDetails.port); }
public static void SendServer_SaveConfirmation(ServerDetails server) { PacketsProcessor sendBuffer = new PacketsProcessor((int)GameSendPackets.SaveConfirmation); sendBuffer.FinishPacking(); SendPacket(sendBuffer, server.socket); }
public static bool SendServer_RegisterPlayer(ServerDetails server, string loginName, string loginToken, ref Player player, ref Bank playerBank) { PacketsProcessor sendBuffer = new PacketsProcessor((int)GameSendPackets.RegisterNewPlayer); sendBuffer.WriteString(loginToken); sendBuffer.WriteString(loginName); sendBuffer.BufferPlayer(ref player); sendBuffer.BufferPlayerBank(ref playerBank); sendBuffer.FinishPacking(); //Testing packet //byte[] temp = sendBuffer.GetSendBytes(); //AccountsManager.SavePacket("davemax", ref temp); return SendPacket(sendBuffer, server.socket); }
public static void Received(AuthenticationServer form, PacketContent pc, ServerDetails serverDetails) { _form = form; GameRecvPackets msgType = (GameRecvPackets)pc.GetMsgType(); switch (msgType) { case GameRecvPackets.RegisterServer: RegisterServer(pc, serverDetails); break; case GameRecvPackets.UpdatePlayer: PreSavePlayer(pc); break; case GameRecvPackets.PlayerAcknowledged: CheckAdmission(pc); break; case GameRecvPackets.BeginPlayerUpdate: updatingPlayers = true; break; case GameRecvPackets.FinishPlayerUpdate: if (updatedPlayerList.Length == 0) return; updatedPlayerList = updatedPlayerList.Remove(updatedPlayerList.Length - 2, 2); appendLog("Saved accounts & banks for:\n" + updatedPlayerList); updatingPlayers = false; updatedPlayerList = ""; SendServer_SaveConfirmation(serverDetails); break; default: break; } }