protected override void PacketReceived(UdpPacketBuffer buffer) { EndPoint client = buffer.RemoteEndPoint; byte[] receivedData = buffer.Data.Take(buffer.DataLength).ToArray(); UdpPacket packet = UdpPacket.FromBytes(receivedData); switch (packet.PacketType) { case UdpPacketType.Login: Console.WriteLine("User Login -- " + packet.UserId); Login(packet, buffer.RemoteEndPoint); packet.PacketType = UdpPacketType.LoginSucceed; var bytes = packet.ToBytes(); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); Console.WriteLine("User Login Succeed-- " + packet.UserId); break; case UdpPacketType.Data: PerformBroadcasting(packet, receivedData); break; case UdpPacketType.Logoff: Console.WriteLine("User Logoff -- " + packet.UserId); Logoff(packet); packet.PacketType = UdpPacketType.LogoffSuccedd; bytes = packet.ToBytes(); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); Console.WriteLine("User Logoff Succeed -- " + packet.UserId); break; default: break; } }
private void AsyncEndSend(IAsyncResult iar) { // by now you should you get the idea - no further explanation necessary rwLock.AcquireReaderLock(-1); if (!shutdownFlag) { UdpPacketBuffer buffer = (UdpPacketBuffer)iar.AsyncState; try { int bytesSent = udpSocket.EndSend(iar); // note that call to the abstract PacketSent() method - we are passing the number // of bytes sent in a separate parameter, since we can't use buffer.DataLength which // is the number of bytes to send (or bytes received depending upon whether this // buffer was part of a send or a receive). //PacketSent(buffer, bytesSent); } catch (SocketException se) { System.Diagnostics.EventLog.WriteEntry(ServiceName, "A SocketException occurred in UDPServer.AsyncEndSend():\n\n" + se.Message, System.Diagnostics.EventLogEntryType.Error); } } Interlocked.Decrement(ref rwOperationCount); rwLock.ReleaseReaderLock(); }
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.BeginSend( buf.Data, buf.DataLength, 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 AsyncEndReceive(IAsyncResult iar) { // Asynchronous receive operations will complete here through the call // to AsyncBeginReceive // aquire a reader lock rwLock.AcquireReaderLock(-1); if (!shutdownFlag) { // start another receive - this keeps the server going! AsyncBeginReceive(); try { // get the length of data actually read from the socket, store it with the // buffer IPEndPoint remote = null; var data = udpSocket.EndReceive(iar, ref remote); var buffer = new UdpPacketBuffer(data, remote) { DataLength = data.Length }; // this operation is now complete, decrement the reference count Interlocked.Decrement(ref rwOperationCount); // we're done with the socket, release the reader lock rwLock.ReleaseReaderLock(); // call the abstract method PacketReceived(), passing the buffer that // has just been filled from the socket read. PacketReceived(buffer); } catch (SocketException se) { // something bad happened System.Diagnostics.EventLog.WriteEntry(ServiceName, "A SocketException occurred in UDPServer.AsyncEndReceive():\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(); } } else { // nothing bad happened, but we are done with the operation // decrement the reference count and release the reader lock Interlocked.Decrement(ref rwOperationCount); 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);