public static Message BuildMessage(byte[] buffer) { var reader = new CustomDataReader(buffer); var messagePart = new MessagePart(false); if (messagePart.Build(reader)) { Message message; try { message = MessageReceiver.BuildMessage((ushort)messagePart.MessageId.Value, reader); return(message); } catch (Exception ex) { Logger.Error("Error while building Message :" + ex.Message); return(null); } finally { reader.Dispose(); } } else { return(null); } }
protected override void ReceiveEvent_Completed(object sender, SocketAsyncEventArgs e) { if (!IsConnected()) { return; } _reader.Add(e.Buffer, e.Offset, e.BytesTransferred); while (_reader.BytesAvailable > 0) { if (_currentMessage == null) { _currentMessage = new MessagePart(); } if (_currentMessage.Build(_reader)) { var messageDataReader = new CustomDataReader(_currentMessage.Data); var message = MessageReceiver.BuildMessage((uint)_currentMessage.MessageId, messageDataReader); if (message == null) { return; } _dispatcher.Dispatch(message); _currentMessage = null; } else { break; } } base.ReceiveEvent_Completed(sender, e); }
public static Message BuildMessage(byte[] buffer) { var reader = new CustomDataReader(buffer); var messagePart = new MessagePart(false); if (messagePart.Build(reader)) { Message message; try { message = MessageReceiver.BuildMessage((ushort)messagePart.MessageId.Value, reader); return message; } catch (Exception ex) { Logger.Error("Error while building Message :" + ex.Message); return null; } finally { reader.Dispose(); } } else return null; }
public override void OnDataArrival(byte[] buffer) { try { CustomDataReader reader = new CustomDataReader(buffer); bool isRequest = reader.ReadBoolean(); int num = reader.ReadInt(); var guid = new Guid(reader.ReadBytes(num)); TransitionMessage message = SSyncCore.BuildMessage(reader.ReadBytes(reader.BytesAvailable)) as TransitionMessage; message.Guid = guid; if (SSyncCore.ShowProtocolMessage) { logger.Color2("Receive " + message.ToString()); } if (isRequest) { MessagePool.HandleRequest(this, message); } else { MessagePool.HandleAnswer(this, message); } } catch (Exception ex) { logger.Error(ex.ToString()); } }
protected virtual bool BuildMessage(BufferSegment buffer) { if (this.m_currentMessage == null) { this.m_currentMessage = new MessagePart(false); } CustomDataReader fastBigEndianReader = new CustomDataReader(buffer); fastBigEndianReader.Seek((buffer.Offset + this.m_offset), SeekOrigin.Current); var maxPosition = (long)(buffer.Offset + this.m_offset + this.m_remainingLength); bool result; if (this.m_currentMessage.Build(fastBigEndianReader)) { long position = fastBigEndianReader.Position; if (this.m_currentMessage.ExceedBufferSize) { fastBigEndianReader = new CustomDataReader(this.m_currentMessage.Data); } else { maxPosition = (long)buffer.Offset + position + (long)this.m_currentMessage.Length.Value; } Message message; try { message = MessageReceiver.BuildMessage((ushort)this.m_currentMessage.MessageId.Value, fastBigEndianReader); } catch (Exception) { if (this.m_currentMessage.ReadData) { this.logger.Debug("Message = {0}", this.m_currentMessage.Data.ToString(" ")); } else { fastBigEndianReader.Seek((int)position, SeekOrigin.Begin); this.logger.Debug("Message = {0}", fastBigEndianReader.ReadBytes(this.m_currentMessage.Length.Value).ToString(" ")); } throw; } this.LastActivity = DateTime.Now; if (BaseClient.LogPackets) { Console.WriteLine("(RECV) {0} : {1}", this, message); } this.OnMessageReceived(message); this.m_remainingLength -= (int)fastBigEndianReader.Position - buffer.Offset - this.m_offset; this.m_offset = (int)fastBigEndianReader.Position - buffer.Offset; this.m_currentMessage = null; result = (this.m_remainingLength <= 0 || this.BuildMessage(buffer)); } else { this.m_offset = (int)fastBigEndianReader.Position; this.m_remainingLength = (int)(maxPosition - (long)this.m_offset); result = false; } return(result); }
public void Handle(byte[] message) { var header = BitConverter.ToUInt16(message.Take(2).Reverse().ToArray(), 0); var messageId = header >> 2; var lengthType = header & 3; var data = message.Skip(6); var networkMessage = NetworkMessageRepository.Instance.Get((uint)messageId); int messageLength = 0; if (networkMessage == null) { Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("[" + Nickname + "] << UnknownMessage(" + messageId + ") | Size: " + message.Length + " bytes"); Console.ResetColor(); return; } if (lengthType != 0) { var reader = new CustomDataReader(data.ToArray()); switch (lengthType) { case 1: messageLength = reader.ReadByte(); break; case 2: messageLength = reader.ReadUShort(); break; case 3: messageLength = reader.ReadByte() << 16; messageLength |= reader.ReadUShort(); break; } networkMessage.Deserialize(reader); } Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("[" + Nickname + "] << " + networkMessage.GetName() + " | " + networkMessage.GetSummary()); Console.ResetColor(); NetworkMessageHandlerService.Instance.Handle(this, (NetworkMessage)networkMessage); }
private void OnReceivData(int size) { if (_options.Buffer.Max() != 0) { _options.BufferEndPosition += size; if (_options.BufferEndPosition > _options.BufferLength) { throw new Exception("Too large amount of data."); } } else { _options.BufferEndPosition = size; } while (_options.BufferEndPosition > 0) { CustomDataReader customDataReader = new CustomDataReader(_options.Buffer); NetworkMessage networkMessage = _frameManager.BuildMessage(customDataReader); if (networkMessage == null) { _logger.LogWarning($"{this} Received Unknown Data"); } else { OnMessageReceived(networkMessage); } byte[] numArray = new byte[_options.BufferLength]; Array.Copy(_options.Buffer, customDataReader.Position, numArray, (long)0, (long)(int)numArray.Length - customDataReader.Position); _options.Buffer = numArray; _options.BufferEndPosition -= (int)customDataReader.Position; customDataReader.Dispose(); } }
public bool Build(CustomDataReader reader) { bool result; if (reader.BytesAvailable <= 0L) { result = false; } else { if (this.IsValid) { result = true; } else { if (!this.Header.HasValue && reader.BytesAvailable < 2L) { result = false; } else { if (reader.BytesAvailable >= 2L && !this.Header.HasValue) { this.Header = new int?((int)reader.ReadShort()); } bool formatedHeader; if (this.LengthBytesCount.HasValue) { long num = reader.BytesAvailable; int? num2 = this.LengthBytesCount; if (num >= (long)num2.GetValueOrDefault() && num2.HasValue) { formatedHeader = this.Length.HasValue; goto CheckHeader; } } formatedHeader = true; CheckHeader: if (!formatedHeader) { if (this.LengthBytesCount < 0 || this.LengthBytesCount > 3) { throw new Exception("Malformated Message Header, invalid bytes number to read message length (inferior to 0 or superior to 3)"); } this.Length = new int?(0); for (int i = this.LengthBytesCount.Value - 1; i >= 0; i--) { this.Length |= (int)reader.ReadByte() << i * 8; } } if (this.Length.HasValue && !this.m_dataMissing) { if (this.Length == 0) { if (this.ReadData) { this.Data = new byte[0]; } result = true; return(result); } long num = reader.BytesAvailable; int? num2 = this.Length; if (num >= (long)num2.GetValueOrDefault() && num2.HasValue) { if (this.ReadData) { this.Data = reader.ReadBytes(this.Length.Value); } else { this.m_availableBytes = reader.BytesAvailable; } result = true; return(result); } num2 = this.Length; num = reader.BytesAvailable; if ((long)num2.GetValueOrDefault() > num && num2.HasValue) { if (this.ReadData) { this.Data = reader.ReadBytes((int)reader.BytesAvailable); } else { this.m_availableBytes = reader.BytesAvailable; } this.m_dataMissing = true; result = false; return(result); } } else { if (this.Length.HasValue && this.m_dataMissing) { long num = (long)(this.ReadData ? this.Data.Length : 0) + reader.BytesAvailable; int? num2 = this.Length; if (num < (long)num2.GetValueOrDefault() && num2.HasValue) { if (this.ReadData) { int destinationIndex = this.m_data.Length; Array.Resize <byte>(ref this.m_data, (int)((long)this.Data.Length + reader.BytesAvailable)); byte[] array = reader.ReadBytes((int)reader.BytesAvailable); Array.Copy(array, 0, this.Data, destinationIndex, array.Length); } else { this.m_availableBytes = reader.BytesAvailable; } this.m_dataMissing = true; } num = (long)(this.ReadData ? this.Data.Length : 0) + reader.BytesAvailable; num2 = this.Length; if (num >= (long)num2.GetValueOrDefault() && num2.HasValue) { if (this.ReadData) { int num3 = this.Length.Value - this.Data.Length; Array.Resize <byte>(ref this.m_data, this.Data.Length + num3); byte[] array = reader.ReadBytes(num3); Array.Copy(array, 0, this.Data, this.Data.Length - num3, num3); } else { this.m_availableBytes = reader.BytesAvailable; } } } } result = this.IsValid; } } } return(result); }