Advertisement
RaWRCoder

Server Send Packet

Jul 21st, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1.     public SocketError SendPacket(uint clid, DataPacket p, DProcessRequestResultsServer handler = null)
  2.         {
  3.             if (clid < Clients.Count || Clients[(int)clid] == null )
  4.             {
  5.                 Log.Post(LogMessageType.Error, "Cannot send packet to client[{0}] - client id is invalid!", clid);
  6.                 return SocketError.SocketError;
  7.             }
  8.             if (Protocol == null)
  9.             {
  10.                 Log.Post(LogMessageType.CriticalError, "Cannot send packet to client[{0}] - no protocol specified!", clid);
  11.                 return SocketError.SocketError;
  12.             }
  13.             if (p.DataLength > Protocol.MaxPacketLength)
  14.             {
  15.                 Log.Post(LogMessageType.CriticalError,
  16.                     "Cannot send packet to client[{0}] - packet is too big ({1} vs {2} bytes allowed by protocol)",
  17.                     clid, p.DataLength, Protocol.MaxPacketLength);
  18.                 return SocketError.SocketError;
  19.             }
  20.  
  21.             var client = Clients[(int) clid];
  22.             if (!client.ShouldBeActive || !client.IsActive || client.Socket == null)
  23.             {
  24.                 Log.Post(LogMessageType.Error,
  25.                     "Failed to send packet to client[{0}] - client is now disconnecting",
  26.                     clid);
  27.                 client.ShouldBeActive = false;
  28.                 return SocketError.SocketError;
  29.             }
  30.  
  31.             if (!RegisterAnswerHandler(p.Key, handler))
  32.                 return SocketError.SocketError;
  33.  
  34.             SocketError err;
  35.             client.Socket.Send(p.GetBytes(), 0, DataPacket.HeaderLength, SocketFlags.None, out err);
  36.             if (err != SocketError.Success)
  37.             {
  38.                 Log.Post(LogMessageType.Error,
  39.                     "Failed to send packet to client[{0}] - SocketError.{1}",
  40.                     clid, err);
  41.             }
  42.             return err;
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement