Exemplo n.º 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;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Genera un TCP SocketManager del array de bytes de los datos solicitados
        /// </summary>
        /// <param name="codec">Codec para la obtención</param>
        /// <param name="stream">Stream</param>
        /// <returns>Devuelve la clase TCPSocketMsgManager</returns>
        public static IXPloitSocketMsg Deserialize(Encoding codec, Stream stream)
        {
            List <IXploitMsgHeader> headers = new List <IXploitMsgHeader>();
            byte nheaders = (byte)stream.ReadByte();
            byte type;
            SerializableJSONReference tp;

            if (nheaders > 0)
            {
                // Read headers

                byte[] bl = new byte[3];
                for (int x = 0; x < nheaders; x++)
                {
                    // Read header Length
                    stream.Read(bl, 0, 3);
                    type = bl[2];

                    byte[] data = new byte[BitConverterHelper.ToUInt16(bl, 0) - 1];
                    stream.Read(data, 0, data.Length);

                    if (!_CacheHeaders.TryGetValue(type, out tp))
                    {
                        //no está en cache
                        EXPloitSocketMsgHeader e = (EXPloitSocketMsgHeader)type;
                        tp = e.GetAttribute <SerializableJSONReference>();
                        lock (_CacheHeaders)
                        {
                            _CacheHeaders.Add(type, tp);
                        }
                    }

                    headers.Add(tp.Deserialize <IXploitMsgHeader>(tp.Type, codec, data, 0, data.Length));
                }
            }

            type = (byte)stream.ReadByte();

            if (!_Cache.TryGetValue(type, out tp))
            {
                //no está en cache
                EXPloitSocketMsg e = (EXPloitSocketMsg)type;
                tp = e.GetAttribute <SerializableJSONReference>();
                lock (_Cache)
                {
                    _Cache.Add(type, tp);
                }
            }

            IXPloitSocketMsg ret = tp.Deserialize <IXPloitSocketMsg>(tp.Type, codec, stream);

            if (nheaders > 0)
            {
                ret.Headers.AddRange(headers);
            }
            return(ret);
        }
Exemplo n.º 3
0
        void s_OnMessage2(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
        {
            // Server receive msg
            XPloitMsgString msgS = (XPloitMsgString)msg;

            //XPloitTelnetProtocol.Send(cl, XPloitTelnetProtocol.GetColorMessage());
            //XPloitTelnetProtocol.Send(cl, new byte[] { 255, 247 });
            XPloitTelnetProtocol.Send(cl, "Received: " + msgS.Data + Environment.NewLine);

            //isover = true;
        }
        public virtual byte[] Serialize(Encoding codec, EXPloitSocketMsg type, IXPloitSocketMsg msg, byte[] header)
        {
            string smsg = JsonHelper.Serialize(msg);
            byte[] data = codec.GetBytes(smsg);
            int l = data.Length;

            int header_length = header == null ? 0 : header.Length;
            byte[] all = new byte[l + header_length + 1];
            Array.Copy(data, 0, all, header_length + 1, l);
            all[header_length] = (byte)type;
            return all;
        }
 public override byte[] Serialize(Encoding codec, EXPloitSocketMsg type, IXPloitSocketMsg msg, byte[] header)
 {
     using (MemoryStream ms = new MemoryStream())
     using (BsonWriter writer = new BsonWriter(ms))
     {
         //grabar la cabecera del mensaje
         if (header != null) ms.Write(header, 0, header.Length);
         //grabar el tipo
         ms.WriteByte((byte)type);
         //grabar el mensaje serializado
         Serializer.Serialize(writer, msg);
         return ms.ToArray();
     }
 }
Exemplo n.º 6
0
 void s_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Server receive msg
     isover = true;
 }
Exemplo n.º 7
0
 void client_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Client receive message
     cl.Send(new XPloitMsgLogin() { Domain = "?", User = "******", Password = "******", InResponseTo = msg.Id });
 }
Exemplo n.º 8
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;
        }
Exemplo n.º 9
0
        void _Socket_OnMessage(XPloitSocket sender, XPloitSocketClient client, IXPloitSocketMsg msg)
        {

        }
Exemplo n.º 10
0
        /// <summary>
        /// Envia el mensaje y devuelve una respuesta, Este método nunca hay que llamarlo desde el hilo del Socket
        /// </summary>
        /// <param name="msg">Mensaje</param>
        public IXPloitSocketMsg SendAndWait(IXPloitSocketMsg msg)
        {
            Guid wait = msg.Id;
            if (Send(msg) <= 0) return null;

            IXPloitSocketMsg msgRet;
            while (!_Actions.TryGetValue(wait, out msgRet))
            {
                //Read();
                Thread.Sleep(0);
            }
            return msgRet;
        }
Exemplo n.º 11
0
 public void RaiseOnMessage(XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     if (msg == null) return;
     if (OnMessage != null) OnMessage(this, cl, msg);
 }