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); }
public static void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes) { if (m_Server != null) { m_Server.SendRawData(ctsMarker, msgNote, protoBytes); } }
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"); }
private static void ProcessHandshake(XPacket packet) { var handshake = XPacketConverter.Deserialize <XPacketHandshake>(packet); if (_handshakeMagic - handshake.MagicHandshakeNumber == 15) { Console.WriteLine("Handshake successful!"); } }
private static void OnPacketRecieve(byte[] packet) { var parsed = XPacket.Parse(packet); if (parsed != null) { ProcessIncomingPacket(parsed); } }
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 } }
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()); }
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 } }
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(); }
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(); } }
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); }
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(); } }
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); } } }
public void SendRawData(XPacket msgNote, byte[] protoBytes) { msgNote.msgSize = (ushort)(mClientSocket.MsgPrefixLength + (protoBytes != null ? protoBytes.Length : 0)); mClientSocket.SendData(msgNote, protoBytes); }
public virtual void SendRawData(CTSMarker ctsMarker, XPacket msgNote, byte[] protoBytes) { }