void WriteLengthInPacket(byte[] bff, int index, int length) { // Append length to header switch (_HeaderLength) { case 1: { bff[index] = (byte)(length); break; } case 2: { byte[] bft = BitConverterHelper.GetBytesUInt16((ushort)(length)); Array.Copy(bft, 0, bff, index, _HeaderLength); break; } case 3: { byte[] bft = BitConverterHelper.GetBytesUInt24((uint)(length)); Array.Copy(bft, 0, bff, index, _HeaderLength); break; } case 4: { byte[] bft = BitConverterHelper.GetBytesInt32(length); Array.Copy(bft, 0, bff, index, _HeaderLength); break; } } }
public virtual int Send(IXPloitSocketMsg msg, Stream stream) { if (stream == null || msg == null) { return(0); } byte[] bff; if (_Crypt != null) { bff = msg.Serialize(_Codec, null); bff = _Crypt.Encrypt(bff, _HeaderPadding); } else { bff = msg.Serialize(_Codec, _HeaderPadding); } int length = bff.Length; if (length == 0) { return(0); } if (length >= _MaxLength) { // Dividir en partes mas pequeñas int lengthH = length - _HeaderLength; int maxPacketLength = _MaxLength - _HeaderLength; uint packets = (uint)(lengthH / maxPacketLength); if (lengthH % maxPacketLength != 0) { packets++; } byte[] pak = BitConverterHelper.GetBytesUInt24(packets); int write = 0; int index = _HeaderLength; int currentLength; byte[] data = new byte[_MaxLength]; // Guid-length lock (stream) { // Header and Parts stream.Write(_HeaderPadding, 0, _HeaderLength); stream.Write(pak, 0, PartsHeaderLength); for (int x = 0; x < packets; x++) { currentLength = Math.Min(length - index, maxPacketLength); WriteLengthInPacket(data, 0, currentLength); Array.Copy(bff, index, data, _HeaderLength, currentLength); index += currentLength; stream.Write(data, 0, currentLength + _HeaderLength); write += currentLength + _HeaderLength; } } return(write); } // Append length to header WriteLengthInPacket(bff, 0, length - _HeaderLength); //Log(bff, 0, length, true, cl.Parent.IsServer); stream.Write(bff, 0, length); return(length); }