Ejemplo n.º 1
0
 /// <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;
 }
Ejemplo n.º 2
0
 /// <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;
 }
Ejemplo n.º 3
0
 /// <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;
 }
Ejemplo n.º 4
0
 /// <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;
 }
Ejemplo n.º 5
0
 /// <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;
 }
Ejemplo n.º 6
0
 /// <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;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Callback function on receipt of a ping.
 /// </summary>
 /// <param name="packet">The packet received.</param>
 protected abstract void OnPing(NetworkPacket packet);
Ejemplo n.º 8
0
 /// <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;
 }
Ejemplo n.º 9
0
 /// <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;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Callback function on receipt of game data.
 /// </summary>
 /// <param name="packet">The packet received.</param>
 protected abstract void OnGameData(NetworkPacket packet);
Ejemplo n.º 11
0
 /// <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;
 }
Ejemplo n.º 12
0
 /// <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;
 }
Ejemplo n.º 13
0
 /// <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;
 }
Ejemplo n.º 14
0
 /// <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;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Callback function on receipt of a SYNC.
 /// </summary>
 /// <param name="packet">The packet received.</param>
 protected abstract void OnSync(NetworkPacket packet);
Ejemplo n.º 16
0
 /// <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;
 }
Ejemplo n.º 17
0
 /// <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;
 }