/// <summary> /// Invoked when a new peer disconnects. /// </summary> /// <param name="peer">Peers connection.</param> private void ProcessDisconnectedPeer(Connection peer) { GameClientPeer peerMetaData = peer.MetaData as GameClientPeer; if (peerMetaData == null) { return; } peerMetaData.Disconnected(); m_disconnectedPeers.Add(peer); }
/// <summary> /// Connects to superpeers that are required for the new zone. /// </summary> /// <param name="zone">Zone that player is moving to.</param> private void ConnectToNewSuperPeers(Zone zone) { foreach (ZoneSuperPeer peer in zone.SuperPeers) { bool connected = false; // Are we connected to the peer hosting this super peer? foreach (ClientToSuperPeerConnection c in m_superPeerConnections) { if (c.ClientID == peer.ClientID) { connected = true; break; } } if (connected == false) { Logger.Info("Connecting to peer hosting SuperPeer #{0} for zone #{1} on {2}:{3}.", LoggerVerboseLevel.High, peer.ID, peer.ZoneID, peer.ClientIPAddress, peer.ClientListenPort); // Connect to super peer! Connection connection = new Connection(); connection.ConnectOverTime(peer.ClientIPAddress, (ushort)peer.ClientListenPort); m_superPeerConnections.Add(new ClientToSuperPeerConnection(this, connection, peer.ClientID)); } } }
/// <summary> /// Processes a packet that has been recieved from the arbitrator. /// </summary> /// <param name="peer">The client or peer we recieved the packet from.</param> /// <param name="packet">Packet that we recieved.</param> private void ProcessArbitratorIncomingPacket(Connection peer, Packet packet) { // ----------------------------------------------------------------- // We've been sent an updated version of the zone grid. // ----------------------------------------------------------------- if (packet is ZoneGridPacket) { ZoneGridPacket specificPacket = packet as ZoneGridPacket; m_zoneGrid.FromPacket(specificPacket); // Invoke events for all super peers who have gained control of zones. foreach (ZoneSuperPeer p in m_zoneGrid.GainedSuperPeers) { Zone zone = m_zoneGrid.GetZoneByID(p.ZoneID); SuperPeerGainedControl(zone, p); } // Invoke events for all super peers who have lost control of zones. foreach (ZoneSuperPeer p in m_zoneGrid.LostSuperPeers) { Zone zone = m_zoneGrid.GetZoneByID(p.ZoneID); SuperPeerLostControl(zone, p); } Logger.Info("Recieved updated zone grid from arbitrator.", LoggerVerboseLevel.High); } // ----------------------------------------------------------------- // We've been sent our persistent state information. // ----------------------------------------------------------------- else if (packet is UserAccountStatePacket) { UserAccountStatePacket specificPacket = packet as UserAccountStatePacket; m_account = specificPacket.Account; m_clientID = specificPacket.ClientID; if (m_threadNameSet == false) { System.Threading.Thread.CurrentThread.Name = "Client #" + m_clientID; m_threadNameSet = true; } Logger.Info("Recieved updated account information from arbitrator.", LoggerVerboseLevel.High); } // ----------------------------------------------------------------- // Information sent to one of our super peers about a client? // ----------------------------------------------------------------- else if ((packet as SuperPeerClientPacket) != null) { SuperPeerClientPacket specificPacket = packet as SuperPeerClientPacket; bool found = false; foreach (Connection connection in m_listenConnection.Peers) { if (connection.MetaData != null) { GameClientPeer p = ((GameClientPeer)connection.MetaData); foreach (SuperPeerToClientConnection conn in p.SuperPeerConnections) // ERROR IS HERE: SuperPeerConnections is filled with inactive ones that are recieving the packets too :( { if (conn.SuperPeer.ID == specificPacket.SuperPeerID && conn.ClientID == specificPacket.ClientID) { conn.RecievedArbitratorPacket(specificPacket); found = true; } } } } if (found == false) { throw new InvalidOperationException("Failed to find super peer to direct arbitrator packet to."); } } // ----------------------------------------------------------------- // Information sent to one of our super peers? // ----------------------------------------------------------------- else if ((packet as SuperPeerPacket) != null) { SuperPeerPacket specificPacket = packet as SuperPeerPacket; SuperPeer superpeer = FindSuperPeerByID(specificPacket.SuperPeerID); if (superpeer != null) { superpeer.ArbitratorRecievedPacket(m_arbitratorConnection, specificPacket); } else { throw new InvalidOperationException("Failed to find super peer to direct arbitrator packet to."); } } }
/// <summary> /// Invoked when a new peer connects. /// </summary> /// <param name="peer">Peers connection.</param> private void ProcessConnectedPeer(Connection peer) { GameClientPeer peerMetaData = new GameClientPeer(this, peer); peerMetaData.Connected(); peer.MetaData = peerMetaData; }
/// <summary> /// Sets up the connection to an arbitrator to recieve information about the game world. /// </summary> /// <returns>Returns true if successful, else false.</returns> private async Task<bool> SetupArbitratorConnection() { bool result = false; if (m_arbitratorConnection == null) { Logger.Info("Attempting to connect to arbitrator on {0}:{1} ...", LoggerVerboseLevel.High, m_arbitratorHost, m_arbitratorPort); m_arbitratorConnection = new Connection(); result = await m_arbitratorConnection.ConnectAsync(m_arbitratorHost, m_arbitratorPort); } else { Logger.Error("Lost connection to arbitrator, attempting to setup connection again ...", LoggerVerboseLevel.Normal); result = await m_arbitratorConnection.ReconnectAsync(); } if (result == false) { Logger.Error("Failed to connect to arbitrator on {0}:{1}", LoggerVerboseLevel.Normal, m_arbitratorHost, m_arbitratorPort); } else { Logger.Info("Successfully connected to arbitrator.", LoggerVerboseLevel.High); } return result; }
/// <summary> /// Sets up the listen connection to accept incoming connections from peers. /// </summary> /// <returns>Returns true if successful, else false.</returns> private async Task<bool> SetupListenConnection() { bool result = false; if (m_listenConnection == null) { Logger.Info("Attempting to listen on {0}:{1} ...", LoggerVerboseLevel.High, LISTEN_HOST, LISTEN_PORT); m_listenConnection = new Connection(); result = await m_listenConnection.ListenAsync(LISTEN_PORT, LISTEN_HOST); } else { Logger.Error("Lost listening connection, attempting to setup connection again ...", LoggerVerboseLevel.Normal); result = await m_listenConnection.RelistenAsync(); } if (result == false) { Logger.Error("Failed to listen on {0}:{1}", LoggerVerboseLevel.Normal, LISTEN_HOST, LISTEN_PORT); } else { Logger.Info("Successfully began listening.", LoggerVerboseLevel.High); } return result; }
private int m_zoneID = 0; // Which zone we are currently in. #endregion Fields #region Constructors /// <summary> /// Constructs a new instance of this class. /// </summary> /// <param name="arbitrator">Arbitrator that this peer is connected to.</param> /// <param name="peer_connection">Connection through which this peer is connected.</param> public ArbitratorPeer(ArbitratorService arbitrator, Connection peer_connection) { m_arbitrator = arbitrator; m_connection = peer_connection; }
/// <summary> /// Initializes this arbitrator, making initial connections to database and getting ready /// to begin accepting connections. /// </summary> /// <returns>True if successful, otherwise false. If false is returned then the Run/Deinit methods are never called.</returns> public bool Initialize() { Logger.Info("Begining setup of game client ...", LoggerVerboseLevel.High); m_arbitratorConnection = null; // Setup connection. SetupArbitratorConnection().Wait(); if (m_arbitratorConnection == null || m_arbitratorConnection.Connected == false) { Logger.Error("Failed to setup game client, could not connect to arbitrator.", LoggerVerboseLevel.Normal); return false; } // Setup listen connection. SetupListenConnection().Wait(); if (m_listenConnection == null || m_listenConnection.Listening == false) { Logger.Error("Failed to setup game client, could not setup listen connection.", LoggerVerboseLevel.Normal); return false; } Logger.Info("Setup game client successfully.", LoggerVerboseLevel.High); return true; }
/// <summary> /// Constructs a new instance of this class. /// </summary> /// <param name="gameClient">Game client hosting this connection.</param> /// <param name="service">Superpeer a client is connected to..</param> /// <param name="connection">Connection used to communicate with the super peer.</param> public SuperPeerToClientConnection(GameClientService gameClient, SuperPeer superpeer, Connection connection) { m_superpeer = superpeer; m_connection = connection; m_gameClient = gameClient; }
/// <summary> /// Constructs a new instance of this class. /// </summary> /// <param name="client">Game client that this peer is connected to.</param> /// <param name="peer_connection">Connection through which this peer is connected.</param> public GameClientPeer(GameClientService client, Connection peer_connection) { m_gameClient = client; m_connection = peer_connection; }
/// <summary> /// Constructs a new instance of this class. /// </summary> /// <param name="service">Game client thats hosting this connection.</param> /// <param name="connection">Connection used to communicate with the super peer.</param> /// <param name="id">Client ID of superpeer on the other end of this connection.</param> public ClientToSuperPeerConnection(GameClientService service, Connection connection, int id) { m_service = service; m_connection = connection; m_clientID = id; }
/// <summary> /// Initializes this arbitrator, making initial connections to database and getting ready /// to begin accepting connections. /// </summary> /// <returns>True if successful, otherwise false. If false is returned then the Run/Deinit methods are never called.</returns> public bool Initialize() { Logger.Info("Begining setup of arbitrator server ...", LoggerVerboseLevel.High); m_listenConnection = null; m_databaseConnection = null; // Setup database connection. SetupDBConnection().Wait(); if (m_databaseConnection == null || m_databaseConnection.Connected == false) { Logger.Error("Failed to setup arbitrator, could not initialize database connection.", LoggerVerboseLevel.Normal); return false; } // Setup listening connection. SetupConnection().Wait(); if (m_listenConnection == null || m_listenConnection.Listening == false) { Logger.Error("Failed to setup arbitrator, could not initialize listen connection.", LoggerVerboseLevel.Normal); return false; } // Register arbitrator in database. RegisterArbitrator(); Logger.Info("Setup arbitrator server successfully.", LoggerVerboseLevel.High); return true; }
/// <summary> /// Invoked when a new peer disconnects. /// </summary> /// <param name="peer">Peers connection.</param> private void ProcessDisconnectedPeer(Connection peer) { ArbitratorPeer peerMetaData = peer.MetaData as ArbitratorPeer; if (peerMetaData == null) { return; } peerMetaData.Disconnected(); }
/// <summary> /// Invoked when a new peer connects. /// </summary> /// <param name="peer">Peers connection.</param> private void ProcessConnectedPeer(Connection peer) { ArbitratorPeer peerMetaData = new ArbitratorPeer(this, peer); peerMetaData.Connected(); peer.MetaData = peerMetaData; }
/// <summary> /// Invoked when an arbitrator sends a packet to this super peer. /// </summary> public void ArbitratorRecievedPacket(Connection connection, SuperPeerPacket packet) { }