Exemple #1
0
        public virtual int Send(IXPloitSocketMsg msg, Stream stream)
        {
            if (stream == null || msg == null)
            {
                return(0);
            }

            byte[] bff;
            switch (msg.Type)
            {
            case EXPloitSocketMsg.String:
            {
                XPloitMsgString send = (XPloitMsgString)msg;
                bff = _Codec.GetBytes(send.Data);
                break;
            }

            case EXPloitSocketMsg.ByteArray:
            {
                XPloitMsgByteArray send = (XPloitMsgByteArray)msg;
                bff = send.Data;
                break;
            }

            default: { bff = msg.Serialize(_Codec, null); break; }
            }

            int length = bff.Length;

            if (length == 0)
            {
                return(0);
            }

            stream.Write(bff, 0, length);
            return(length);
        }
Exemple #2
0
        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);
        }