/// <summary> /// Callback function on receipt of game data. /// </summary> /// <param name="packet">The packet received.</param> protected override void OnGameData(NetworkPacket packet) { gameDataUpdater(new GameData(packet.DataArray)); return; }
/// <summary> /// Callback function on receipt of a ping. /// </summary> /// <param name="packet">The packet received.</param> protected override void OnPing(NetworkPacket packet) { lock (pingStopwatch) { pingStopwatch.Stop(); lastPing = pingStopwatch.ElapsedMilliseconds; } return; }
/// <summary> /// Queue a packet to be sent over the network. /// </summary> /// <param name="packet">The packet to be sent.</param> public void SendPacket(NetworkPacket packet) { lock (sendBuffer) { sendBuffer.Enqueue(packet); } sendSemaphore.Release(); return; }
/// <summary> /// Callback function on disconnect events. /// </summary> /// <param name="packet">A packet received that has caused us to disconnect.</param> protected void OnDisconnect(NetworkPacket packet) { Debug.WriteLine(String.Format("Client has disconnected")); Shutdown(); return; }
/// <summary> /// Perform a transion between states. /// </summary> /// <param name="transEvent">An event that is occuring when a packet arrives.</param> /// <param name="packet">The packet received over the network.</param> public void DoTransition(TransitionEvent transEvent, NetworkPacket packet) { Tuple<NetworkState, TransitionAction> transition = null; //try { transition = TransitionTable[new Tuple<NetworkState, TransitionEvent>(CurrentState, transEvent)]; transition.Item2(packet); CurrentState = transition.Item1; } //catch (Exception error) //{ // Debug.WriteLine("You managed to break the NetStateMachine. Congratulations, asshole: {0}", new Object[] {error.Message}); // Debug.WriteLine("Violating Transition: {0} - {1}",new Object[] {CurrentState,transEvent}); // throw error; //} return; }
/// <summary> /// Main thread loop for receiving packets. /// </summary> private void RunReceiveThread() { IPEndPoint serverAddress = new IPEndPoint(IPAddress.Any, 0); while (continueRunning) { try { using (MemoryStream incomingPacketStream = new MemoryStream(this.Receive(ref serverAddress))) { NetworkPacket receivePacket = new NetworkPacket((NetworkPacket.PacketType)incomingPacketStream.ReadByte(), serverAddress); incomingPacketStream.Read(receivePacket.DataArray, 0, (int)incomingPacketStream.Length - 1); lock (receiveBuffer) { receiveBuffer.Enqueue(receivePacket); } receiveSemaphore.Release(); } } catch { } // This should only be enountered if something is horribly wrong or it the worker is closing down } return; }
/// <summary> /// Callback function on receipt of a ping. /// </summary> /// <param name="packet">The packet received.</param> protected abstract void OnPing(NetworkPacket packet);
/// <summary> /// Update this connection ID on receipt of a sync packet. /// </summary> /// <param name="packet">The packet that was received.</param> private void OnSync(NetworkPacket packet) { lock (this) { this.MissedSyncs = 0; } return; }
/// <summary> /// After receiving a handshake, respond to a client with another handshake. /// </summary> /// <param name="packet"></param> private void OnHandshake(NetworkPacket packet) { if (!connections.ContainsKey(packet.Destination)) { Debug.WriteLine("Server - New connection from: " + packet.Destination); connections[packet.Destination] = new ConnectionID(packet.Destination); Debug.WriteLine("Server - Added Connection: " + connections[packet.Destination].ID); HandshakePacket hs = new HandshakePacket(packet.Destination); networkWorker.SendPacket(hs); PacketsSent++; } return; }
/// <summary> /// Callback function on receipt of game data. /// </summary> /// <param name="packet">The packet received.</param> protected abstract void OnGameData(NetworkPacket packet);
/// <summary> /// Callback function on receipt of a SYNC packet. /// </summary> /// <param name="packet">The packet received.</param> protected override void OnSync(NetworkPacket packet) { connections[packet.Destination].ChangeState(NetworkStateMachine.TransitionEvent.SERVERSYNC); return; }
/// <summary> /// Callback function on receipt of a ping. /// </summary> /// <param name="packet">The packet received.</param> protected override void OnPing(NetworkPacket packet) { PingPacket ps = new PingPacket(packet.Destination); networkWorker.SendPacket(ps); //ACK the ping PacketsSent++; return; }
/// <summary> /// Handles reception of game data updates from the client. /// </summary> /// <param name="packet">A packet that contains game data information.</param> protected override void OnGameData(NetworkPacket packet) { GameData gameData = new GameData(packet.DataArray); gameData.ConnectionInfo = connections[packet.Destination]; gameDataUpdater(gameData); return; }
/// <summary> /// Callback function on receipt of a SYNC packet. /// </summary> /// <param name="packet">The packet received.</param> protected override void OnSync(NetworkPacket packet) { SendSync(); return; }
/// <summary> /// Callback function on receipt of a SYNC. /// </summary> /// <param name="packet">The packet received.</param> protected abstract void OnSync(NetworkPacket packet);
/// <summary> /// Callback function upon connecting to a server. /// </summary> /// <param name="packet">The network packet retrieved during this transition.</param> private void OnConnect(NetworkPacket packet) { clientConnectedSemaphore.Release(); StartPinging(); return; }
/// <summary> /// Keep track of the number of SYNCS that have been missed to eventually transition into a TIMEOUT state. /// </summary> /// <param name="packet">The packet that was received.</param> private void OnMissingSync(NetworkPacket packet) { lock (this) { MissedSyncs += 1; Debug.WriteLine(String.Format("Missed {0} SYNCS", MissedSyncs)); } return; }