public static void Serialize <T>(Session session, MemoryStream destination, T packet) where T : Packet { byte[] idBytes = ConverterUtility.GetBytes(packet.GetPacketId()); destination.Write(idBytes, 0, idBytes.Length); packet.Serialize(destination); ProtoRegister.ReturnPacket(packet); }
public static byte[] Export2Bytes(string value, TableFiledType type) { List <byte> bytesList = new List <byte>(); byte[] targetBytes; switch (type) { case TableFiledType.FLOAT: targetBytes = ConverterUtility.GetBytes(float.Parse(value)); break; case TableFiledType.INT: targetBytes = ConverterUtility.GetBytes(int.Parse(value)); break; case TableFiledType.BOOL: targetBytes = ConverterUtility.GetBytes((int.Parse(value)) == 1); break; case TableFiledType.STRING: default: targetBytes = ConverterUtility.GetBytes(value); byte[] countBytes = ConverterUtility.GetBytes(targetBytes.Length); bytesList.AddRange(countBytes); break; } bytesList.AddRange(targetBytes); return(bytesList.ToArray()); }
public static void ExportByte(string path) { if (string.IsNullOrEmpty(path)) { return; } _infoDict.Clear(); _tableTypeDict.Clear(); _infoDict = TableReader.ReadCsvFile(path); List <byte> allBytes = new List <byte>(); if (_infoDict.ContainsKey(2)) { _fileName = Path.GetFileNameWithoutExtension(path); string filePath = _targetPath + _fileName + ".byte"; List <string> line = _infoDict[2]; for (int i = 0; i < line.Count; i++) { string target = line[i]; string[] temp = target.Split(":".ToArray()); string type = TableFiledType.STRING.ToString(); if (temp.Length < 2) { LogUtil.LogUtility.PrintWarning(string.Format("#配表未指定类型{0}行,{1}列#path:" + path, 2.ToString(), i.ToString())); return; } else { type = temp[1]; } _tableTypeDict[i] = TableReader.GetTableFiledType(type); } int col = line.Count; byte[] dataCount = ConverterUtility.GetBytes(_infoDict.Count - 2); allBytes.AddRange(dataCount); int index = 3; while (_infoDict.ContainsKey(index)) { List <string> info = _infoDict[index]; if (info.Count != col) { LogUtil.LogUtility.PrintWarning(string.Format("#配表未指定类型{0}行错误#path:" + path, index)); return; } for (int i = 0; i < info.Count; i++) { byte[] tempByte = Export2Bytes(info[i], _tableTypeDict[i]); allBytes.AddRange(tempByte); } index++; } FileUtility.Write2Bytes(filePath, allBytes.ToArray()); EditorUtility.DisplayDialog("提示", "byte 导出成功!", "确认"); } }
public static Packet Deserialize(Session session, MemoryStream source, out object customErrorData) { customErrorData = null; long begin = source.Position; byte[] buffer = new byte[4]; source.Read(buffer, 0, sizeof(int)); int id = ConverterUtility.GetInt32(buffer); Packet packet = ProtoRegister.GetPacket(id); packet.DeSerialize(source); return(packet); }
public override void LoadData(byte[] bytes) { _dataList.Clear(); int pos = 0; int dataCount = ConverterUtility.GetInt32(bytes, pos); pos += Marshal.SizeOf(pos);//int大小; for (int i = 0; i < dataCount; i++) { T data = Activator.CreateInstance <T>(); data.Decode(bytes, ref pos); _dataList.Add(data); } }
public void Send <T>(T packet) where T : Packet { if (_socket == null) { string errorMessage = "session not initialize."; if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.StateError, errorMessage); return; } throw new Exception(errorMessage); } if (packet == null) { string errorMessage = "Packet is invalid."; if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.StateError, errorMessage); return; } throw new Exception(errorMessage); } try { int length = 0; int packetLength = 0; byte[] packetBuffer = new byte[_defaultMaxPacketLength];//TODO:内存池; using (MemoryStream memoryStream = new MemoryStream(packetBuffer, true)) { memoryStream.Seek(_defaultPacketLength, SeekOrigin.Begin); SessionUtil.Serialize(this, memoryStream, packet); length = (int)memoryStream.Position; } packetLength = length - _defaultPacketLength; ConverterUtility.GetBytes(packetLength).CopyTo(packetBuffer, 0); Send(packetBuffer, 0, length); } catch (Exception exception) { _active = false; if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.SerializeError, exception.ToString()); return; } throw; } }
protected static void ReadFloat(ref byte[] byteArr, ref int byteOffset, out float outputValue) { outputValue = ConverterUtility.GetSingle(byteArr, byteOffset); byteOffset += ConverterUtility.FLOAT_LENGTH; }
protected static void ReadUInt64(ref byte[] byteArr, ref int byteOffset, out ulong outputValue) { outputValue = ConverterUtility.GetUInt64(byteArr, byteOffset); byteOffset += ConverterUtility.ULONG_LENGTH; }
protected static void ReadUInt32(ref byte[] byteArr, ref int byteOffset, out uint outputValue) { outputValue = ConverterUtility.GetUInt32(byteArr, byteOffset); byteOffset += ConverterUtility.UINT_LENGTH; }
protected static void ReadBoolean(ref byte[] byteArr, ref int byteOffset, out bool outputValue) { outputValue = ConverterUtility.GetBoolean(byteArr, byteOffset); byteOffset += ConverterUtility.BOOL_LENGTH; }
protected static void ReadShort(ref byte[] byteArr, ref int byteOffset, out short outputValue) { outputValue = ConverterUtility.GetInt16(byteArr, byteOffset); byteOffset += ConverterUtility.SHORT_LENGTH; }
private bool Process() { if (_receiver.ReceivedLength != _receiver.Length) { throw new Exception(string.Format("Receive length '{0}' is not equal to length '{1}'.", _receiver.ReceivedLength.ToString(), _receiver.Length.ToString())); } if (_receiver.Length < _defaultPacketLength) { throw new Exception(string.Format("Length '{0}' is smaller than length header.", _receiver.Length.ToString())); } if (_receiver.Length == _defaultPacketLength) { int packetLength = ConverterUtility.GetInt32(_receiver.GetBuffer()); if (packetLength <= 0) { string errorMessage = string.Format("Packet length '{0}' is invalid.", packetLength.ToString()); if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.HeaderError, errorMessage); return(false); } throw new Exception(errorMessage); } _receiver.Length += packetLength; if (_receiver.Length > _receiver.BufferSize) { string errorMessage = string.Format("Length '{0}' is larger than buffer size '{1}'.", _receiver.Length.ToString(), _receiver.BufferSize.ToString()); if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.OutOfRangeError, errorMessage);//未接收完成,继续; return(false); } throw new Exception(errorMessage); } return(true); } Packet packet = null; try { int packetLength = _receiver.Length - _defaultPacketLength; object customErrorData = null; using (MemoryStream memoryStream = new MemoryStream(_receiver.GetBuffer(), _defaultPacketLength, packetLength, false)) { lock (thisLock) { packet = SessionUtil.Deserialize(this, memoryStream, out customErrorData); } } _receiver.Reset(_defaultPacketLength); if (packet == null) { if (CustomErrorHandler != null) { CustomErrorHandler(this, customErrorData); } } else { if (ReceiveHandler != null) { ReceiveHandler(this, packet); } } } catch (Exception exception) { _active = false; if (ErrorHandler != null) { ErrorHandler(this, SessionErrorCode.DeserializeError, exception.ToString()); return(false); } throw; } return(true); }