public void UploadCharacterToOwner(CharacterMessage message) { foreach (var i in Clients) { if (i.Player.Id == message.CharacterOwner.Id) { i.Character = message.Character; } } }
public CharacterMessage GetCharacterByOwner(CharacterMessage message) { foreach (var i in Clients) { if (i.Player == message.CharacterOwner) { message.Character = i.Character; } } return(message); }
public void SendCharacterChanged(Character character) { CharacterMessage message = new CharacterMessage { Type = CharacterMessageType.SendCharacter, Character = character, CharacterOwner = character.Owner }; string serialized = JsonConvert.SerializeObject(message); byte[] data = Encoding.Unicode.GetBytes(serialized); Stream.Write(data, 0, data.Length); }
private void CharactersProcess() { try { while (true) { CharacterMessage message = GetCharacterMessage(); if (message.Type == CharacterMessageType.SendCharacter) { if (message.Character != null) { Server.UploadCharacterToOwner(message); } } else if (message.Type == CharacterMessageType.AcceptCharacters) { message.Characters = Server.GetAllCharacters(); message.Type = CharacterMessageType.SendCharacters; string serialized = JsonConvert.SerializeObject(message); byte[] data = Encoding.Unicode.GetBytes(serialized); Stream.Write(data, 0, data.Length); } else if (message.Type == CharacterMessageType.AcceptCharacter) { if (message.CharacterOwner != null) { message = Server.GetCharacterByOwner(message); message.Type = CharacterMessageType.SendCharacter; string serialized = JsonConvert.SerializeObject(message); byte[] data = Encoding.Unicode.GetBytes(serialized); Stream.Write(data, 0, data.Length); } } } } catch (Exception e) { MessageBox.Show(e.ToString()); } finally { Server.RemoveConnection(this.Id); Close(); } }
private Task ReceiveWork() { return(Task.Run(() => { while (IsConnected) { try { Thread.Sleep(SLEEP_TIME_COMMON); CharacterMessage characterMessage = new CharacterMessage { Type = CharacterMessageType.AcceptCharacters }; string serializedForSend = JsonConvert.SerializeObject(characterMessage); byte[] dataForSend = Encoding.Unicode.GetBytes(serializedForSend); Stream.Write(dataForSend, 0, dataForSend.Length); byte[] data = new byte[DATA_LENGTH]; StringBuilder builder = new StringBuilder(); int bytes = 0; do { bytes = Stream.Read(data, 0, data.Length); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } while (Stream.DataAvailable); string serialized = builder.ToString(); CharacterMessage message = JsonConvert.DeserializeObject <CharacterMessage>(serialized); if (message.Type == CharacterMessageType.SendCharacters && message.Characters != null) { CharactersInRoom = message.Characters; } } catch (Exception) { Disconnect(); } } })); }