コード例 #1
0
 public void UploadCharacterToOwner(CharacterMessage message)
 {
     foreach (var i in Clients)
     {
         if (i.Player.Id == message.CharacterOwner.Id)
         {
             i.Character = message.Character;
         }
     }
 }
コード例 #2
0
 public CharacterMessage GetCharacterByOwner(CharacterMessage message)
 {
     foreach (var i in Clients)
     {
         if (i.Player == message.CharacterOwner)
         {
             message.Character = i.Character;
         }
     }
     return(message);
 }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: ClientObject.cs プロジェクト: DeEsthete/BrpgCenter
 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();
     }
 }
コード例 #5
0
        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();
                    }
                }
            }));
        }