Example #1
0
        public static XPacket Serialize(byte type, byte subtype, object obj, bool strict = false)
        {
            var fields = GetFields(obj.GetType());

            if (strict)
            {
                var usedUp = new List <byte>();

                foreach (var field in fields)
                {
                    if (usedUp.Contains(field.Item2))
                    {
                        throw new Exception("One field used two times.");
                    }

                    usedUp.Add(field.Item2);
                }
            }

            var packet = XPacket.Create(type, subtype);

            foreach (var field in fields)
            {
                packet.SetValue(field.Item2, field.Item1.GetValue(obj));
            }

            return(packet);
        }
Example #2
0
 public static void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes)
 {
     if (m_Server != null)
     {
         m_Server.SendRawData(ctsMarker, msgNote, protoBytes);
     }
 }
Example #3
0
    public override void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes)
    {
        msgNote.msgSize = (ushort)(ctsMarker.tcpSocket.MsgPrefixLength + (protoBytes != null ? protoBytes.Length : 0));

        ctsMarker.tcpSocket.SendData(msgNote, protoBytes);

        Debug.LogError("================================================= Send data to client");
    }
Example #4
0
        private static void ProcessHandshake(XPacket packet)
        {
            var handshake = XPacketConverter.Deserialize <XPacketHandshake>(packet);

            if (_handshakeMagic - handshake.MagicHandshakeNumber == 15)
            {
                Console.WriteLine("Handshake successful!");
            }
        }
Example #5
0
        private static void OnPacketRecieve(byte[] packet)
        {
            var parsed = XPacket.Parse(packet);

            if (parsed != null)
            {
                ProcessIncomingPacket(parsed);
            }
        }
Example #6
0
 public void HandleReceiveData(TcpSocket <XPacket> tcpSocket, XPacket msgNote, MemoryStream msgStream)
 {
     if (msgNote.MsgID == (ushort)eMsgID.C2S_AttributeStream)
     {
         Launcher.instance.connectionMgr.ProcessAttributeStream(new CTSMarker(tcpSocket, null), msgStream.ToArray());
     }
     else
     {
         // Common message
     }
 }
Example #7
0
        private void ProcessHandshake(XPacket packet)
        {
            Console.WriteLine("Recieved handshake packet.");

            var handshake = XPacketConverter.Deserialize <XPacketHandshake>(packet);

            handshake.MagicHandshakeNumber -= 15;

            Console.WriteLine("Answering..");

            QueuePacketSend(XPacketConverter.Serialize(XPacketType.Handshake, handshake).ToPacket());
        }
Example #8
0
    void HandleReceiveData(TcpSocket <XPacket> srcSocket, XPacket msgNote, MemoryStream msgStream)
    {
        if (msgNote.MsgID == (ushort)eMsgID.S2C_RadianceStream)
        {
            Debug.LogError("======================================= Receive radiance from sever: " + msgNote.MsgID + ", " + msgNote.msgSize);

            Launcher.instance.stats.ShowRadianceTexture(msgNote.ScreenWidth, msgNote.ScreenHeight, msgStream.ToArray());
        }
        else
        {
            // Common message
        }
    }
Example #9
0
        private static void Main()
        {
            Console.Title           = "";
            Console.ForegroundColor = ConsoleColor.White;

            var packet = XPacket.Create(0, 0);

            packet.SetValue(0, 12345);

            var encr = packet.Encrypt().ToPacket();
            var decr = XPacket.Parse(encr);

            Console.WriteLine(decr.GetValue <int>(0));

            Console.ReadLine();
        }
Example #10
0
        private static void ProcessIncomingPacket(XPacket packet)
        {
            var type = XPacketTypeManager.GetTypeFromPacket(packet);

            switch (type)
            {
            case XPacketType.Handshake:
                ProcessHandshake(packet);
                break;

            case XPacketType.Unknown:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #11
0
        public static T Deserialize <T>(XPacket packet, bool strict = false)
        {
            var fields   = GetFields(typeof(T));
            var instance = Activator.CreateInstance <T>();

            if (fields.Count == 0)
            {
                return(instance);
            }

            foreach (var tuple in fields)
            {
                var field         = tuple.Item1;
                var packetFieldId = tuple.Item2;

                if (!packet.HashField(packetFieldId))
                {
                    if (strict)
                    {
                        throw new Exception($"Couldn't get field[{packetFieldId}] for {field.Name}");
                    }

                    continue;
                }

                var value = typeof(XPacket)
                            .GetMethod("GetValue")?
                            .MakeGenericMethod(field.FieldType)
                            .Invoke(packet, new object[] { packetFieldId });

                if (value == null)
                {
                    if (strict)
                    {
                        throw new Exception($"Couldn't get value for field[{packetFieldId}] for {field.Name}");
                    }

                    continue;
                }

                field.SetValue(instance, value);
            }

            return(instance);
        }
Example #12
0
    public override void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes)
    {
        if (m_LcrsService != null)
        {
            //Debug.Log("==================================================== Send data with: " + protoBytes.Length);

            if (m_SessionDataBufferMap.ContainsKey(ctsMarker.sessionId) == false)
            {
                m_SessionDataBufferMap.Add(ctsMarker.sessionId, new AccumDataBuffer(m_LcrsService, ctsMarker.sessionId));
            }

            m_SessionDataBufferMap[ctsMarker.sessionId].PushData(protoBytes);

            //m_LcrsService.Sessions.SendTo(ctsMarker.sessionId, protoBytes);

            m_LcrsService.Sessions.SendToAsync(ctsMarker.sessionId, protoBytes, SendAsyncCompleted);

            // Debug: save the file
            //string strSaveFile = Application.dataPath + "/rt_" + System.DateTime.Now.Minute + "_" + System.DateTime.Now.Second + "_web.png";
            //FileStream fs = File.Open(strSaveFile, FileMode.Create);
            //fs.Write(protoBytes, 0, protoBytes.Length);
            //fs.Close();
        }
    }
Example #13
0
        private void ProcessIncomingPackets()
        {
            while (true)                  // Слушаем пакеты, пока клиент не отключится.
            {
                var buff = new byte[256]; // Максимальный размер пакета - 256 байт.
                Client.Receive(buff);

                buff = buff.TakeWhile((b, i) =>
                {
                    if (b != 0xFF)
                    {
                        return(true);
                    }
                    return(buff[i + 1] != 0);
                }).Concat(new byte[] { 0xFF, 0 }).ToArray();

                var parsed = XPacket.Parse(buff);

                if (parsed != null)
                {
                    ProcessIncomingPacket(parsed);
                }
            }
        }
Example #14
0
    public void SendRawData(XPacket msgNote, byte[] protoBytes)
    {
        msgNote.msgSize = (ushort)(mClientSocket.MsgPrefixLength + (protoBytes != null ? protoBytes.Length : 0));

        mClientSocket.SendData(msgNote, protoBytes);
    }
Example #15
0
 public virtual void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes)
 {
 }