public void SendPacket(SendBasePacket packet) { lock (this) { packet.Write(); byte[] pck = packet.ToByteArray(); byte PacketId = pck[0]; pck = CryptEngine.Crypt(pck, NetworkKey); List <Byte> FullPacket = new List <Byte>(); FullPacket.AddRange(BitConverter.GetBytes((short)(pck.Length + 2))); //+2 Packet Length FullPacket.AddRange(pck); try { _socket.Send(FullPacket.ToArray()); } catch { } settings.SendedPackets++; } }
public bool SendPacket(SendBasePacket packet) { lock (this) { try { packet.Write(); byte[] pck = CryptEngine.Crypt(packet.ToByteArray()); List <Byte> FullPacket = new List <Byte>(); FullPacket.AddRange(BitConverter.GetBytes((short)(pck.Length + 2))); //+2 Packet Length FullPacket.AddRange(pck); //Packet _client.Send(FullPacket.ToArray()); } catch { return(false); } return(true); } }
public void SendPacket(SendBasePacket packet) { lock (this) { packet.Write(); byte[] pck = CryptEngine.Crypt(packet.ToByteArray()); if (packet.Length > 60000) { return; } List <Byte> FullPacket = new List <Byte>(); FullPacket.AddRange(BitConverter.GetBytes((short)(pck.Length + 2))); //+2 Packet Length FullPacket.AddRange(pck); //Packet try { _client.Send(FullPacket.ToArray()); } catch {} } }
private void Client_BeginReceive(IAsyncResult ar) { try { if (_client.EndReceive(ar) > 0) { _buffer = new byte[BitConverter.ToInt16(_buffer, 0) - 2]; if (_buffer.Length > 10000) //ignore packets that are bigger then 10kb { return; } if (_buffer.Length > 0) { _client.Receive(_buffer, 0, _buffer.Length, SocketFlags.Partial); _buffer = CryptEngine.Crypt(_buffer); //Process the packet. Type pck = ClientPacketProcessor.ProcessPacket(_buffer); if (pck != null) { ReceiveBasePacket rbp = (ReceiveBasePacket)Activator.CreateInstance(pck, this, _buffer); rbp.Run(); } Read(); return; } } } catch { if (!_client.Connected) { TryToConnect(); } } }
private void ReadCallbackStatic(IAsyncResult ar) { try { if (_socket.EndReceive(ar) > 1) { if (_FloodProtector.HandleFlood(_socket.RemoteEndPoint) == false) { Disconnect(); return; } //Get length from the buffer, convert to Int16 and read the rest of the packet. _buffer = new byte[BitConverter.ToInt16(_buffer, 0) - 2]; //-2 for the length, we already read that :P if (_buffer.Length > 10000) //ignore packets that are bigger then 10kb { return; } if (_buffer.Length > 0) { _socket.Receive(_buffer, _buffer.Length, SocketFlags.Partial); _buffer = CryptEngine.Crypt(_buffer, NetworkKey); Type pck = ClientPacketProcessor.ProcessPacket(_buffer, _socket.RemoteEndPoint.ToString()); if (pck != null) { ReceiveBasePacket rbp = (ReceiveBasePacket)Activator.CreateInstance(pck, this, _buffer); rbp.Run(); settings.ReceivedPackets++; } else { //unknown packet received Disconnect(); return; } Read(); return; } else { //Disconnect when no length is given... maybe flood? just disconnect :P Disconnect(); return; } } else { Disconnect(); return; } } catch { if (!_socket.Connected) { Disconnect(); } return; } }
private void ReadCallbackStatic(IAsyncResult ar) { try { if (_socket.EndReceive(ar) > 1) { if (_FloodProtector.HandleFlood(_socket.RemoteEndPoint) == false) { //Disconnect(); //return; } //Get length from the buffer, convert to Int16 and read the rest of the packet. if (_buffer[0] == 0 && _buffer[1] == 0) { Read(); return; } try { _buffer = new byte[BitConverter.ToInt16(_buffer, 0) - 2]; } catch { //Sometimes here is a error because the first 2 bytes are '0' //I really don't know how to fix that... so lets ignore it :P } if (_buffer.Length > 0) { settings.ReceivedPackets++; _socket.Receive(_buffer, _buffer.Length, SocketFlags.Partial); _buffer = CryptEngine.Crypt(_buffer, NetworkKey); Type pck = FileClientPacketProcessor.ProcessPacket(_buffer, _socket.RemoteEndPoint.ToString()); if (pck != null) { ReceiveBasePacket rbp = (ReceiveBasePacket)Activator.CreateInstance(pck, this, _buffer); rbp.Run(); } else { //unknown packet received //Disconnect(); Read(); return; } Read(); return; } else { //Disconnect when no length is given... maybe flood? just disconnect :P //Disconnect(); Read(); return; } } else { //Disconnect(); Read(); return; } } catch { //if(!_socket.Connected) // Disconnect(); Read(); return; } }