private void ChangeStream(Stream stream) { m_stream.Dispose(); m_reader.Dispose(); m_stream = stream; m_reader = new BigEndianReader(m_stream); }
private void Initialize() { var indexesPointer = _reader.ReadInt();//index offset _reader.Seek(indexesPointer, SeekOrigin.Begin); long indexesLength = _reader.ReadInt(); int i = 0; while (i < indexesLength)//texts { var key = _reader.ReadInt(); var dialectCriticalText = _reader.ReadBoolean(); var pointer = _reader.ReadInt(); _indexes.Add(key, pointer); if (dialectCriticalText) { i += 4; _unDiacriticalIndexes.Add(key, Tuple.Create(true, _reader.ReadInt())); } else { _unDiacriticalIndexes.Add(key, Tuple.Create(false, pointer)); } i += 9; } indexesLength = _reader.ReadInt(); while (indexesLength > 0)//ui texts { var position = _reader.Position; _textIndexes.Add(_reader.ReadUTF(), _reader.ReadInt()); indexesLength = (indexesLength - (_reader.Position - position)); } indexesLength = _reader.ReadInt(); i = 0; while (indexesLength > 0)//texts sorts { var position = _reader.Position; _textSortIndexes.Add(_reader.ReadInt(), i++); indexesLength = (indexesLength - (_reader.Position - position)); } foreach (var key in _indexes.Keys) { GetText(key); } foreach (var textKey in _textIndexes.Keys) { GetText(textKey); } _reader.Dispose(); }
public override void InitBuild(MemoryStream stream) { MessageBuffer informations = Informations as MessageBuffer; if (stream.Length > 0) { Reader.Add(stream.ToArray(), 0, (int)stream.Length); } if (informations.Build(Reader, ClientSide)) { if (BotofuProtocolManager.Protocol[ProtocolKeyEnum.Messages, x => x.protocolID == informations.MessageId] is NetworkElement message) { IDataReader reader = new BigEndianReader(informations.Data); ProtocolJsonContent content = FromByte(null, message, reader); reader.Dispose(); OnMessageParsed?.Invoke(message, content); } Informations = null; Informations = new MessageBuffer(); stream.Close(); stream = new MemoryStream(Reader.ReadBytes((int)Reader.BytesAvailable)); Reader.Dispose(); Reader = null; Reader = new BigEndianReader(); InitBuild(stream); } }
/// <summary> /// Build a messagePart and call the ConstructMessage(); method. /// </summary> /// <param name="buffer">data received</param> /// <returns>Message of your protocol, builted</returns> public static Message BuildMessage(byte[] buffer) { var reader = new BigEndianReader(buffer); var messagePart = new MessagePart(false); if (messagePart.Build(reader)) { Message message; try { message = ConstructMessage((ushort)messagePart.MessageId.Value, reader); return(message); } catch (Exception ex) { Console.WriteLine("Error while building Message :" + ex.Message); return(null); } finally { reader.Dispose(); } } else { return(null); } }
private void ClientDataReceived(object sender, SimpleClient.DataReceivedEventArgs e) { try { var messageDataReader = new BigEndianReader(e.Data.Data); NetworkMessage message = MessageReceiver.BuildMessage((uint)e.Data.MessageId.Value, messageDataReader); SaveMsg(message, ConnectionDestination.Client); if (ClientMessageReceived != null) { OnClientMessageReceived(new MessageReceivedEventArgs(message)); } if (message == null) { Send(e.Data, ConnectionDestination.Server); return; } // here if (!message.Cancel) { Send(e.Data, ConnectionDestination.Server); messageDataReader.Dispose(); message = null; } } catch (Exception ex) { if (Client != null && WindowManager.MainWindow.Logger != null) { WindowManager.MainWindow.Logger.Error("[Network] ClientDataReceived Function -> " + ex.Message); } else { WindowManager.MainWindow.Logger.Error("[Network] ClientDataReceived Function -> " + ex.Message); } } }
public void Dispose() { if (m_isDisposed) { return; } m_isDisposed = true; if (m_reader != null) { m_reader.Dispose(); } if (m_links != null) { foreach (D2pFile link in m_links) { link.Dispose(); } } }
// // reading methods // public void Read(byte[] data, int position) { System.IO.MemoryStream ms = null; BigEndianReader ber = null; try { ms = new System.IO.MemoryStream(data); if (position != 0) { data = TTC.TTCollectionFile.ExtractTTFfromTTC(ms, position); ms.Dispose(); ms = new System.IO.MemoryStream(data); } ber = new BigEndianReader(ms); this.Read(ber); this.FileData = data; } catch (Exception ex) { throw new System.IO.IOException("Could not load the font file from the stream. " + ex.Message); } finally { if (null != ber) { ber.Dispose(); } if (null != ms) { ms.Dispose(); } } }
public static bool DeserializeConfig() { if (IsFileExists() == false) { return(false); } lock (CheckLock) { try { BigEndianReader reader = new BigEndianReader(File.ReadAllBytes(Path.Combine(System.Windows.Forms.Application.StartupPath, "config.ap"))); int count = reader.ReadInt(); ConfigsList = new List <ConfigEntry>(); for (int i = 0; i < count; i++) { ConfigEntry entry = new ConfigEntry(); entry.Deserialize(reader); ConfigsList.Add(entry); } Loaded = true; reader.Close(); reader.Dispose(); return(true); } catch (Exception ex) { WindowManager.MainWindow.Logger.Error("Read file error -> " + ex.Message); return(false); } } }
/// <summary> /// thx to Hitman for this implementation ;) /// </summary> /// <param name="stream"></param> private async void OnReceive(MemoryStream stream) { if (_reader is null) { _reader = new BigEndianReader(); } if (stream.Length > 0) { _reader.Add(stream.ToArray(), 0, (int)stream.Length); } byte[] full_data = _reader.Data; while (_position < full_data.Length && full_data.Length - _position >= 2) { long start_pos = _position; _current_header = (ushort)((full_data[_position] * 256) + full_data[_position + 1]); _position += sizeof(ushort); _instance_id = (uint)((full_data[_position] * 256 * 256 * 256) + (full_data[_position + 1] * 256 * 256) + (full_data[_position + 2] * 256) + full_data[_position + 3]); _position += sizeof(uint); if (full_data.Length - _position < _static_header) { break; } switch (_static_header) { case 0: _length = 0; break; case 1: _length = full_data[_position]; break; case 2: _length = (ushort)((full_data[_position] * 256) + full_data[_position + 1]); break; case 3: _length = (full_data[_position] * 256 * 256) + (full_data[_position + 1] * 256) + full_data[_position + 2]; break; } _position += _static_header; long _current_data_len = full_data.Length - _position; if (_current_data_len >= _length) { NetworkElement _element = BotofuProtocolManager.Protocol[ProtocolKeyEnum.Messages, x => x.protocolID == _message_id]; _data = new byte[_current_data_len]; byte[] packet_data = new byte[(int)(_position - start_pos) + _length.Value]; Array.Copy(full_data, start_pos, packet_data, 0, packet_data.Length); Array.Copy(full_data, _position, _data, 0, _length.Value); if (_element != null) { logger.Info($"[{_tag}] {_element.BasicString}"); _data_buffer_reader = new MessageDataBufferReader(_element); using (BigEndianReader big_data_reader = new BigEndianReader(_data)) { await _handler.Handle(this, _element, _data_buffer_reader.Parse(big_data_reader)); } } _position += _length.Value; if (_current_data_len == _length) { _clear(); _reader.Dispose(); _reader = new BigEndianReader(); _position = 0; break; } _clear(); } else { _position = start_pos; break; } } }
public void Close() { stream.Close(); reader.Dispose(); }
public void Dispose() { m_reader.Dispose(); }
/// <summary> /// thx to Hitman for this implementation made by ToOnS /// </summary> /// <param name="stream"></param> protected virtual void OnReceive(MemoryStream stream) { if (_reader is null) { _reader = new BigEndianReader(); } if (stream.Length > 0) { _reader.Add(stream.ToArray(), 0, (int)stream.Length); } byte[] full_data = _reader.Data; while (_position < full_data.Length && full_data.Length - _position >= 2) { long start_pos = _position; _current_header = (ushort)((full_data[_position] * 256) + full_data[_position + 1]); _position += sizeof(ushort); if (_tag == ProxyTagEnum.Client) { _instance_id = (uint)((full_data[_position] * 256 * 256 * 256) + (full_data[_position + 1] * 256 * 256) + (full_data[_position + 2] * 256) + full_data[_position + 3]); _position += sizeof(uint); } if (full_data.Length - _position < _static_header) { break; } switch (_static_header) { case 0: _length = 0; break; case 1: _length = full_data[_position]; break; case 2: _length = (ushort)((full_data[_position] * 256) + full_data[_position + 1]); break; case 3: _length = (full_data[_position] * 256 * 256) + (full_data[_position + 1] * 256) + full_data[_position + 2]; break; } _position += _static_header; long _current_data_len = full_data.Length - _position; if (_current_data_len >= _length) { NetworkElement _element = BotofuProtocolManager.Protocol[ProtocolKeyEnum.Messages, x => x.protocolID == _message_id]; _data = new byte[_current_data_len]; if (_tag == ProxyTagEnum.Client) { // rmv element from not game socket if (_instance_id > _proxy.GLOBAL_INSTANCE_ID * 2) { _element = null; } else { _proxy.LAST_CLIENT_INSTANCE_ID = _instance_id.Value; _proxy.MESSAGE_RECEIVED_FROM_LAST = 0; } } else { if (_message_id == StaticValues.RAW_DATA_MSG_RCV_ID) // rdm { _element = BotofuProtocolManager.Protocol[ProtocolKeyEnum.Messages, x => x.name == "RawDataMessage"]; } _proxy.MESSAGE_RECEIVED_FROM_LAST++; } byte[] packet_data = new byte[(int)(_position - start_pos) + _length.Value]; Array.Copy(full_data, start_pos, packet_data, 0, packet_data.Length); Array.Copy(full_data, _position, _data, 0, _length.Value); if (_element != null) { logger.Info($"[{_tag}] {_element.BasicString} n°{_proxy.GLOBAL_INSTANCE_ID}"); _data_buffer_reader = new MessageDataBufferReader(_element); using (BigEndianReader big_data_reader = new BigEndianReader(_data)) { if (_handler.Handle(this, _element, _data_buffer_reader.Parse(big_data_reader)).Result) { _client_sender.Handle(_remote, packet_data); } } } else { _client_sender.Handle(_remote, packet_data); } _position += _length.Value; if (_current_data_len == _length) { _clear(); _reader.Dispose(); _reader = new BigEndianReader(); _position = 0; break; } _clear(); } else { _position = start_pos; break; } } }
private void ServerDataReceived(object sender, SimpleClient.DataReceivedEventArgs e) { try { var messageDataReader = new BigEndianReader(e.Data.Data); NetworkMessage message = MessageReceiver.BuildMessage((uint)e.Data.MessageId.Value, messageDataReader); SaveMsg(message, ConnectionDestination.Server); if (ServerMessageReceived != null) { OnServerMessageReceived(new MessageReceivedEventArgs(message)); } if (message == null) { Send(e.Data, ConnectionDestination.Client); return; } // here if (message.MessageId == 6469 || message.MessageId == 42) { SelectedServerDataMessage msg = (SelectedServerDataMessage)message; TicketsManager.RegisterTicket(Client.AccountName, Client.Network.Instance, msg); ((ClientNetwork)Client.Network).Ticket = true; Client.Network.Send(new SelectedServerDataMessage(msg.serverId, "127.0.0.1", new uint[] { 443 }, msg.canCreateNewCharacter, msg.ticket), ConnectionDestination.Client); message.Cancel = true; Client.UnloadClient(); } if (message.MessageId == 22) { IdentificationSuccessMessage msg = (IdentificationSuccessMessage)message; Client.AccountName = msg.login; } if (message.MessageId == 153) { CharacterSelectedSuccessMessage msg = (CharacterSelectedSuccessMessage)message; Client.Dock.Invoke((MethodInvoker) delegate { Client.Dock.Text = msg.infos.name + " (" + Client.AccountName + ")"; }); } if (!message.Cancel) { Send(e.Data, ConnectionDestination.Client); messageDataReader.Dispose(); message = null; } } catch (Exception ex) { if (Client != null && WindowManager.MainWindow.Logger != null) { WindowManager.MainWindow.Logger.Error("[Network] ServerDataReceived Function -> " + ex.Message); } } }