Esempio n. 1
0
 void client_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Client receive message
     cl.Send(new XPloitMsgLogin()
     {
         Domain = "?", User = "******", Password = "******", InResponseTo = msg.Id
     });
 }
Esempio n. 2
0
 void client_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Client receive message
     cl.SendReply(new XPloitMsgLogin()
     {
         Domain = "?", User = "******", Password = "******"
     }, msg);
 }
Esempio 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;
        }
Esempio n. 4
0
 public void RaiseOnMessage(XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     if (msg == null)
     {
         return;
     }
     if (OnMessage != null)
     {
         OnMessage(this, cl, msg);
     }
 }
Esempio n. 5
0
        bool Read(XPloitSocketClient c)
        {
            IXPloitSocketMsg msg = c.Read();

            if (msg != null)
            {
                RaiseOnMessage(c, msg);
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        /// <summary>
        /// Envia el mensaje en respuesta a otro
        /// </summary>
        /// <param name="msg">Mensahe</param>
        /// <param name="inResponseTo">En respuesta a</param>
        /// <returns></returns>
        public int SendReply(IXPloitSocketMsg msg, IXPloitSocketMsg inResponseTo)
        {
            if (inResponseTo != null)
            {
                XPloitMsgHeaderId headerId = inResponseTo.Headers.GetHeader <XPloitMsgHeaderId>();
                if (headerId != null)
                {
                    msg.Headers.Add(XPloitMsgHeaderReply.CreateNew(headerId.Id));
                }
            }

            return(Send(msg));
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        /// <summary>
        /// Lee el mensaje o devuelve null si no hay
        /// </summary>
        public IXPloitSocketMsg Read()
        {
            if (_Stream == null)
            {
                return(null);
            }

            try
            {
                if (_Socket.Available <= 0)
                {
                    return(null);
                }

                IXPloitSocketMsg msg = _Protocol.Read(_Stream);
                _LastRead = DateTime.Now;

                if (msg == null)
                {
                    return(null);
                }
                _MsgReceived++;

                XPloitMsgHeaderReply inResponse = msg.Headers.GetHeader <XPloitMsgHeaderReply>();

                if (inResponse != null)
                {
                    if (!Guid.Equals(inResponse.InResponseTo, Guid.Empty))
                    {
                        _Actions.Add(inResponse.InResponseTo, msg);
                        return(null);
                    }
                }

                if (OnMessage != null)
                {
                    OnMessage(this, msg);
                }

                return(msg);
            }
            catch (Exception e)
            {
                Disconnect(EDissconnectReason.Error);
                return(null);
            }
        }
Esempio n. 9
0
 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());
         }
 }
Esempio 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);
        }
Esempio n. 11
0
        /// <summary>
        /// Lee el mensaje o devuelve null si no hay
        /// </summary>
        public IXPloitSocketMsg Read()
        {
            if (_Stream == null)
            {
                return(null);
            }

            try
            {
                if (_Socket.Available <= 0)
                {
                    return(null);
                }

                IXPloitSocketMsg msg = _Protocol.Read(_Stream);
                _LastRead = DateTime.Now;

                if (msg == null)
                {
                    return(null);
                }
                _MsgReceived++;

                if (!Guid.Equals(msg.InResponseTo, Guid.Empty))
                {
                    _Actions.Add(msg.InResponseTo, msg);
                    return(null);
                }

                if (OnMessage != null)
                {
                    OnMessage(this, msg);
                }

                return(msg);
            }
            catch
            {
                Disconnect(EDissconnectReason.Error);
                return(null);
            }
        }
Esempio n. 12
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)
        {
            XPloitMsgHeaderId headerId = XPloitMsgHeaderId.CreateNew();

            msg.Headers.Add(headerId);

            Guid wait = headerId.Id;

            if (Send(msg) <= 0)
            {
                return(null);
            }

            IXPloitSocketMsg msgRet;

            while (!_Actions.TryGetValue(wait, out msgRet))
            {
                //Read();
                Thread.Sleep(0);
            }
            return(msgRet);
        }
Esempio n. 13
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);
        }
Esempio n. 14
0
        public void TestSocketProtocol()
        {
            XPloitSocketProtocol proto = new XPloitSocketProtocol(Encoding.UTF8, XPloitSocketProtocol.EProtocolMode.UInt16);

            using (XPloitSocket server = new XPloitSocket(proto, 77)
            {
                TimeOut = TimeSpan.Zero
            })
                using (XPloitSocket client = new XPloitSocket(proto, "127.0.0.1,77")
                {
                    TimeOut = TimeSpan.Zero
                })
                {
                    server.OnMessage += s_OnMessage;
                    server.Start();

                    client.OnMessage += client_OnMessage;
                    client.Start();

                    Thread.Sleep(100);
                    IXPloitSocketMsg ret = server.Clients[0].SendAndWait(new XPloitMsgLogin()
                    {
                        Domain   = "2500bytes".PadLeft(2000, ' '),
                        User     = "******",
                        Password = "******"
                    });

                    if (ret != null)
                    {
                        isover = true;
                    }
                    while (!isover)
                    {
                        Thread.Sleep(1);
                    }
                }
        }
Esempio n. 15
0
 void _Socket_OnMessage(XPloitSocket sender, XPloitSocketClient client, IXPloitSocketMsg msg)
 {
 }
Esempio n. 16
0
 void s_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Server receive msg
     isover = true;
 }
Esempio n. 17
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);
        }
Esempio n. 18
0
        public virtual IXPloitSocketMsg Read(Stream stream)
        {
            // Comprobar que la cabera es menor que el paquere
            if (stream == null)
            {
                return(null);
            }

            int index = 0, msgLength;

            byte[] bxfData;

            lock (stream)
            {
                msgLength = ReadMessageLength(stream);

                // Control de tamaño máximo
                if (msgLength >= _MaxLength)
                {
                    if (msgLength == _MaxLength)
                    {
                        // Mensaje partido, parsear
                        byte[] parts = new byte[PartsHeaderLength];
                        ReadFull(stream, parts, 0, PartsHeaderLength);

                        uint iparts = BitConverterHelper.ToUInt24(parts, 0);

                        using (MemoryStream ms = new MemoryStream())
                        {
                            for (int x = 0; x < iparts; x++)
                            {
                                // Tamaño
                                msgLength = ReadMessageLength(stream);

                                bxfData = new byte[msgLength];
                                ReadFull(stream, bxfData, 0, msgLength);

                                ms.Write(bxfData, 0, msgLength);
                            }

                            if (_Crypt == null)
                            {
                                ms.Seek(0, SeekOrigin.Begin);
                                return(IXPloitSocketMsg.Deserialize(_Codec, ms));
                            }

                            bxfData   = ms.ToArray();
                            msgLength = bxfData.Length;
                        }
                    }
                    else
                    {
                        throw (new ProtocolException());
                    }
                }
                else
                {
                    bxfData = new byte[msgLength];
                    ReadFull(stream, bxfData, 0, msgLength);
                }
            }

            //Log(msg, index - 4, length + 4, false, cl.Parent.IsServer);
            if (_Crypt != null)
            {
                //desencriptamos el mensaje
                bxfData = _Crypt.Decrypt(bxfData, index, ref msgLength);
                if (bxfData == null)
                {
                    return(null);
                }

                index = 0;
            }

            return(IXPloitSocketMsg.Deserialize(_Codec, bxfData, index, msgLength));
        }