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(); }
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(); }
// 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);
protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent);
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(); }
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(); }
public void SendPacket(byte[] data) { UDPPacketBuffer buffer = new UDPPacketBuffer(); buffer.Data = data; buffer.DataLength = data.Length; buffer.RemoteEndPoint = ServerIpep; AsyncBeginSend(buffer); }
public void SendPacket(UDPPacketBuffer udpPacketBuffer) { AsyncBeginSend(udpPacketBuffer); }
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); } } }
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(); } }
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); } }