Esempio n. 1
0
        public void AsyncBeginSend(UDPPacketBuffer buf)
        {
            // by now you should you get the idea - no further explanation necessary

            rwLock.AcquireReaderLock(-1);

            if (!shutdownFlag)
            {
                try
                {
                    Interlocked.Increment(ref rwOperationCount);
                    udpSocket.BeginSendTo(
                        buf.Data,
                        0,
                        buf.DataLength,
                        SocketFlags.None,
                        buf.RemoteEndPoint,
                        new AsyncCallback(AsyncEndSend),
                        buf);
                }
                catch (SocketException se)
                {
                    System.Diagnostics.EventLog.WriteEntry(ServiceName,
                        "A SocketException occurred in UDPServer.AsyncBeginSend():\n\n" + se.Message,
                        System.Diagnostics.EventLogEntryType.Error);
                }
            }

            rwLock.ReleaseReaderLock();
        }
Esempio n. 2
0
        private void AsyncBeginReceive()
        {
            // this method actually kicks off the async read on the socket.
            // we aquire a reader lock here to ensure that no other thread
            // is trying to set shutdownFlag and close the socket.
            rwLock.AcquireReaderLock(-1);

            if (!shutdownFlag)
            {
                // increment the count of pending operations
                Interlocked.Increment(ref rwOperationCount);

                // allocate a packet buffer
                UDPPacketBuffer buf = new UDPPacketBuffer();

                try
                {
                    // kick off an async read
                    udpSocket.BeginReceiveFrom(
                        buf.Data,
                        0,
                        UDPPacketBuffer.BUFFER_SIZE,
                        SocketFlags.None,
                        ref buf.RemoteEndPoint,
                        new AsyncCallback(AsyncEndReceive),
                        buf);
                }
                catch (SocketException se)
                {
                    // something bad happened
                    System.Diagnostics.EventLog.WriteEntry(ServiceName,
                        "A SocketException occurred in UDPServer.AsyncBeginReceive():\n\n" + se.Message,
                        System.Diagnostics.EventLogEntryType.Error);

                    // an error occurred, therefore the operation is void.  Decrement the reference count.
                    Interlocked.Decrement(ref rwOperationCount);
                }
            }

            // we're done with the socket for now, release the reader lock.
            rwLock.ReleaseReaderLock();
        }
Esempio n. 3
0
 // these abstract methods must be implemented in a derived class to actually do
 // something with the packets that are sent and received.
 protected abstract void PacketReceived(UDPPacketBuffer buffer);
Esempio n. 4
0
 protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent);
Esempio n. 5
0
        private void AsyncBeginReceive()
        {
            // this method actually kicks off the async read on the socket.
            // we aquire a reader lock here to ensure that no other thread
            // is trying to set shutdownFlag and close the socket.
            rwLock.AcquireReaderLock(-1);

            if (!shutdownFlag)
            {
                // increment the count of pending operations
                Interlocked.Increment(ref rwOperationCount);

                    // allocate a packet buffer
                    UDPPacketBuffer buf = new UDPPacketBuffer();

                    try
                    {
                        // kick off an async read
                        udpSocket.BeginReceiveFrom(
                            buf.Data,
                            0,
                            UDPPacketBuffer.BUFFER_SIZE,
                            SocketFlags.None,
                            ref buf.RemoteEndPoint,
                            new AsyncCallback(AsyncEndReceive),
                            buf);
                    }
                    catch (SocketException se)
                    {
                        Diagnostic.NetworkingDiagnostics.Logging.Error("AsyncBeginReceive", se);                        // an error occurred, therefore the operation is void.  Decrement the reference count.
                        Interlocked.Decrement(ref rwOperationCount);

                    }
                    catch (Exception ex)
                    {
                        Diagnostic.NetworkingDiagnostics.Logging.Fatal("AsyncBeginReceive", ex);                        // an error occurred, therefore the operation is void.  Decrement the reference count.
                        Interlocked.Decrement(ref rwOperationCount);

                    }
                }
            // we're done with the socket for now, release the reader lock.
            rwLock.ReleaseReaderLock();
        }
Esempio n. 6
0
        private void AsyncBeginSend(UDPPacketBuffer buf)
        {
            rwLock.AcquireReaderLock(-1);
            if (!shutdownFlag)
            {
                    try
                    {
                        Interlocked.Increment(ref rwOperationCount);

                        udpSocket.BeginSendTo(
                            buf.Data,
                            0,
                            buf.DataLength,
                            SocketFlags.None,
                            buf.RemoteEndPoint,
                            new AsyncCallback(AsyncEndSend),
                            buf);
                    }
                    catch (SocketException se)
                    {
                        Diagnostic.NetworkingDiagnostics.Logging.Error("AsyncBeginSend", se);
                    }
                    catch (Exception ex)
                    {
                        Diagnostic.NetworkingDiagnostics.Logging.Fatal("AsyncBeginSend", ex);
                    }
                }
            rwLock.ReleaseReaderLock();
        }
Esempio n. 7
0
        public void SendPacket(byte[] data)
        {
            UDPPacketBuffer buffer = new UDPPacketBuffer();
            buffer.Data = data;
            buffer.DataLength = data.Length;
            buffer.RemoteEndPoint = ServerIpep;

            AsyncBeginSend(buffer);
        }
Esempio n. 8
0
 public void SendPacket(UDPPacketBuffer udpPacketBuffer)
 {
     AsyncBeginSend(udpPacketBuffer);
 }
Esempio n. 9
0
        private void timerCallbackMethod(object target)
        {
            bool gameHasStarted = false;
            lock (gameInfoPacketLock)
            {
                try
                {
                    gameHasStarted = gameStarted;
                    if (!gameStarted)
                    {
                        if (gameInfoPacketSendCounter == 0)
                        {
                            // game hasnt started yet so sent game info more often
                            UDPPacketBuffer buff = new UDPPacketBuffer();
                            gameInfoPacket.TimeStamp = DateTime.Now;
                            gameInfoPacket.PacketId.Value = packetIdCounter.Next();

                            buff.Data = gameInfoPacket.ToMinimalByte();
                            buff.DataLength = buff.Data.Length;

                            // broadcasting GameInfo to everyone
                            lanBroadcast.SendAsync(buff.Data);
                            //Console.WriteLine("Sending broadcast");
                            for (int i = 0; i < cliendAdressList.Count; i++)
                            {
                                //Console.WriteLine("Sending gameInfo to : {0}", cliendAdressList[i]);
                                buff.RemoteEndPoint = cliendAdressList[i];
                                base.AsyncBeginSend(buff);
                            }
                            if (clientForServer != null && !clientForServer.GameIsRunningAsDedicatedServer)
                                clientForServer.GetMessageFromServer((GameInfoPacket) gameInfoPacket.Clone());
                            // we send every 10 ticks = every second

                            System.Threading.Interlocked.Add(ref gameInfoPacketSendCounter, 10);
                            //gameInfoPacketSendCounter = 10;
                        }
                        else
                            System.Threading.Interlocked.Decrement(ref gameInfoPacketSendCounter);
                            //gameInfoPacketSendCounter--;
                    }
                    else
                    {
                        // game has started so we dont have to send game info so often
                        if (gameInfoPacketSendCounter == 0)
                        {
                            UDPPacketBuffer buff = new UDPPacketBuffer();
                            gameInfoPacket.TimeStamp = DateTime.Now;
                            gameInfoPacket.PacketId.Value = packetIdCounter.Next();

                            buff.Data = gameInfoPacket.ToMinimalByte();
                            buff.DataLength = gameInfoPacket.ByteCount;

                            for (int i = 0; i < cliendAdressList.Count; i++)
                            {
                                //Console.WriteLine("Sending gameInfo to : {0}", cliendAdressList[i]);
                                buff.RemoteEndPoint = cliendAdressList[i];
                                base.AsyncBeginSend(buff);
                            }
                            if (clientForServer != null && !clientForServer.GameIsRunningAsDedicatedServer)
                            clientForServer.GetMessageFromServer((GameInfoPacket)gameInfoPacket.Clone());
                            if (cliendAdressList.Count > 0)
                            {
                                if (lastPackages.ContainsKey(last10.Counter))
                                    lastPackages[last10.Counter] = gameInfoPacket;
                                else
                                    lastPackages.Add(last10.Counter, gameInfoPacket);
                                last10.Increase();
                            }
                            // we need to send GameInfoPaket every 30 ticks - 3 seconds
                            //gameInfoPacketSendCounter = 30;
                            System.Threading.Interlocked.Add(ref gameInfoPacketSendCounter, 30);
                        }
                        else
                           // gameInfoPacketSendCounter--;
                            System.Threading.Interlocked.Decrement(ref gameInfoPacketSendCounter);
                    }
                }
                catch (Exception ex)
                { Diagnostic.NetworkingDiagnostics.Logging.Fatal("Server Timer Error !!! ", ex); }
            }

            if(gameHasStarted)
            lock (serverPacketLock)
            {
                try
                {
                UDPPacketBuffer buff = new UDPPacketBuffer();
                serverPacket.TimeStamp = DateTime.Now;
                serverPacket.PacketId.Value = packetIdCounter.Next();
                buff.Data = serverPacket.ToMinimalByte();
                buff.DataLength = serverPacket.ToMinimalByte().Length;

                for (int i = 0; i < cliendAdressList.Count; i++)
                {
                    //Console.WriteLine("Sending serverPacket to : {0}", cliendAdressList[i]);
                    buff.RemoteEndPoint = cliendAdressList[i];
                    base.AsyncBeginSend(buff);
                }
                if (clientForServer != null && !clientForServer.GameIsRunningAsDedicatedServer)
                clientForServer.GetMessageFromServer((ServerPacket)serverPacket.Clone());

                    //todo ????
                    //WTF ?????
                    //clear client events ??
                for (int k = 0; k < serverPacket.PlayerInfoList.Count; k++)
                {
                    serverPacket.PlayerInfoList[k].PlayerJumping = false;
                    serverPacket.PlayerInfoList[k].PlayerShooting = false;
                }

                if (cliendAdressList.Count > 0)
                {
                    if (lastPackages.ContainsKey(last10.Counter))
                        lastPackages[last10.Counter] = serverPacket;
                    else
                        lastPackages.Add(last10.Counter, serverPacket);
                    last10.Increase();
                }
                }
                catch (Exception ex)
                { Diagnostic.NetworkingDiagnostics.Logging.Fatal("Server Timer Error !!! ", ex); }
            }
        }
Esempio n. 10
0
 private void clientForServer_EndGameEvent(object sender, EventArgs e)
 {
     lock (gameInfoPacketLock)
     {
         try
         {
             UDPPacketBuffer buff = new UDPPacketBuffer();
             buff.Data = gameInfoPacket.ToMinimalByte();
             buff.DataLength = gameInfoPacket.ByteCount;
             gameInfoPacket.RoundNumber = 0;
             for (int i = 0; i < cliendAdressList.Count; i++)
             {
                 buff.RemoteEndPoint = cliendAdressList[i];
                 base.AsyncBeginSend(buff);
             }
         }
         catch (Exception ex)
         {
             Diagnostic.NetworkingDiagnostics.Logging.Error("EndGameEvent", ex);
         }
     }
     System.Threading.Thread.Sleep(200);
     timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
     cliendAdressList.Clear();
     lock (clientPackagesDictionaryLock)
     {
         clientPackagesDictionary = new Dictionary<ushort, UDPClientServerCommons.Usefull.Last10Packages>();
     }
     lock (gameInfoPacketLock)
     {
         gameInfoPacket = new GameInfoPacket();
     }
     last10 = new Last10();
     lock (serverPacketLock)
     {
         serverPacket = new ServerPacket();
     }
 }
Esempio n. 11
0
 private void lanBroadCast_PacketWasReceived(UDPPacketBuffer udpPacketBuffer)
 {
     try
     {
         IPacket packet = PacketTypeChecker.GetPacket(udpPacketBuffer.Data);
         if (packet != null && packet.PacketType == PacketTypeEnumeration.GameInfoPacket)
         {
             GameInfoPacket gameInfoPacket = (GameInfoPacket)packet;
             lock (gameInfoPacketsLock)
             {
                 if (gameInfoPackets.ContainsKey(gameInfoPacket.GameId))
                     gameInfoPackets[gameInfoPacket.GameId].AddPacket(gameInfoPacket);
                 else
                     gameInfoPackets.Add(gameInfoPacket.GameId, new UDPClientServerCommons.Usefull.Last10Packages());
             }
             if (PacketReceivedEvent != null)
                 PacketReceivedEvent(gameInfoPacket);
         }
     }
     catch (Exception ex)
     {
         Diagnostic.NetworkingDiagnostics.Logging.Fatal("lanBroadCast_PacketWasReceived", ex);
     }
 }