public byte[] Encode(object message, ref short serial) { var factory = _factories.FirstOrDefault(f => f.ContainsType(message.GetType())); byte[] result = null; if (factory == null) { throw new InvalidProgramException($"Invalid message type {message.GetType()}"); } ushort opCode = factory.GetOpCode(message.GetType()); var att = message .GetType() .GetCustomAttributes(false) .Where(x => x.GetType() == typeof(WZContractAttribute)) .FirstOrDefault() as WZContractAttribute; if (att == null) { throw new InvalidOperationException("Invalid message format"); } using (var h = new MemoryStream()) using (var b = new MemoryStream()) { Serializer.Serialize(b, message); var body = b.ToArray(); var length = body.Length; var sizeFix = (((opCode & 0xFF00) == 0xFF00) ? 1 : 0); if (att.LongMessage) { var header = new WZWPacket { Type = (byte)0xC2, Size = (ushort)(length + 5 - sizeFix), Operation = opCode }; if (att.Serialized) { var temp = new byte[header.Size - 2]; temp[0] = (byte)serial; Array.Copy(body, 0, temp, 1, body.Length); body = SimpleModulus.Encoder(temp); header.Type += 2; serial++; } Serializer.Serialize(h, header); } else { var header = new WZBPacket { Type = (byte)0xC1, Size = (byte)(length + 4 - sizeFix), Operation = opCode }; if (att.Serialized) { var temp = new byte[header.Size - 1]; temp[0] = (byte)serial; Array.Copy(body, 0, temp, 1, body.Length); body = SimpleModulus.Encoder(temp); header.Type += 2; serial++; } Serializer.Serialize(h, header); } var head = h.ToArray(); var headLen = head.Length - sizeFix; result = new byte[headLen + body.Length]; Array.Copy(head, result, headLen); Array.Copy(body, 0, result, headLen, body.Length); using (var spe = new StreamPacketEngine(false)) { spe.AddData(result); result = spe.ExtractData(); } } return(result); }
public byte[] Encode(object message, ref short serial) { var factory = _factories.First(f => f.ContainsType(message.GetType())); byte[] result = null; ushort opCode = factory.GetOpCode(message.GetType()); WZContractAttribute att = null; foreach (var a in message.GetType().GetCustomAttributes(false)) { if (a.GetType() == typeof(WZContractAttribute)) { att = a as WZContractAttribute; } } if (att == null) { throw new InvalidOperationException("Invalid message format"); } //Console.WriteLine("[S->C] {0} {1} {2}", message.GetType().Name, att.Serialized, serial); using (var h = new MemoryStream()) using (var b = new MemoryStream()) { try { Serializer.Serialize(b, message); }catch (Exception e) { Console.WriteLine(e.Message); } var body = b.ToArray(); var length = body.Length; var sizeFix = (((opCode & 0xFF00) == 0xFF00) ? 1 : 0); var sizeFix2 = 0; if (att.LongMessage) { var header = new WZWPacket { Type = (byte)0xC2, Size = (ushort)(length + 5 - sizeFix), Operation = opCode }; Serializer.Serialize(h, header); sizeFix2 = 2; } else { var header = new WZBPacket { Type = (byte)0xC1, Size = (byte)(length + 4 - sizeFix), Operation = opCode }; Serializer.Serialize(h, header); sizeFix2 = 1; } var head = h.ToArray(); var headLen = head.Length - sizeFix; result = new byte[headLen + body.Length]; Array.Copy(head, result, headLen); Array.Copy(body, 0, result, headLen, body.Length); if (att.Serialized) { result[0] += 2; var temp = new byte[result.Length - sizeFix2]; Array.Copy(result, sizeFix2, temp, 0, temp.Length); temp[0] = (byte)serial; byte[] enc;// = SimpleModulus.Encoder(temp); using (var ms = new MemoryStream()) { SimpleModulus.Encrypt(ms, new MemoryStream(temp)); enc = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(enc, 0, (int)ms.Length); } var resultTemp = new byte[sizeFix2 + enc.Length + 1]; Array.Copy(result, resultTemp, sizeFix2 + 1); if (resultTemp[0] == 0xC3) { resultTemp[1] = (byte)(resultTemp.Length); } else { resultTemp[1] = (byte)(resultTemp.Length >> 8); resultTemp[2] = (byte)(resultTemp.Length & 0xff); } Array.Copy(enc, 0, resultTemp, sizeFix2 + 1, enc.Length); serial++; return(resultTemp); } } return(result); }