Exemple #1
0
        public static Packet GetPacketFromJson(JObject json)
        {
            if (json == null) return null;


            Packet p = null;
            switch ((string)json.GetValue("CMD", StringComparison.CurrentCultureIgnoreCase))
            {

                //case ChatPacket.DefCmd:
                //    p = new ChatPacket(json);
                //    break;
                case DisconnectPacket.DefCmd:
                    p = new DisconnectPacket(json);
                    break;
                case LoginPacket.DefCmd:
                    p = new LoginPacket(json);
                    break;
                case LoginResponsePacket.DefCmd:
                    p = new LoginResponsePacket(json);
                    break;
                case SerialDataPushPacket.DefCmd:
                    p = new SerialDataPushPacket(json);
                    break;
                //case RegisterPacket.DefCmd:
                //    p = new RegisterPacket(json);
                //    break;
                //case RegisterResponsePacket.DefCmd:
                //    p = new RegisterResponsePacket(json);
                //    break;
                case UserChangedPacket.DefCmd:
                    p = new UserChangedPacket(json);
                    break;
                case PullResponsePacket.DefCmd:
                    p = new PullResponsePacket<User>(json);
                    break;
                default:
                    try
                    {
                        p = new ResponsePacket(json);
                    }
                    catch (ArgumentException)
                    {
                    }
                    break;
            }
            return p;
        }
 public async void Logout()
 {
     if (!Connected)
         return;
     DisconnectPacket p = new DisconnectPacket(this.Username);
     try
     {
         await Controller.SendAsync(p);
     }
     catch (InvalidOperationException e)
     {
         Console.WriteLine("An exception occured in the AppGlobal.LogoutFromServer function: " +
             e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("An unknown exception occured: " + e.Message);
     }
     Users.Clear();
     Connected = false;
 }
        //private ResponsePacket HandlePullRequestMessagesByUser(PullRequestPacket packet)
        //{
        //    var allMessages = _datastorage.GetMessages(packet.SearchKey);
        //    return new PullResponsePacket<ChatMessage>(Statuscode.Status.Ok,
        //        PullResponsePacket<ChatMessage>.DataType.ChatMessage,
        //        allMessages);
        //}

        //private ResponsePacket HandlePullRequestReceivedMessages(PullRequestPacket packet)
        //{
        //    var allMessages = _datastorage.GetMessagesSentTo(packet.SearchKey);
        //    return new PullResponsePacket<ChatMessage>(Statuscode.Status.Ok,
        //        PullResponsePacket<ChatMessage>.DataType.ChatMessage,
        //        allMessages);

        //}

        //        private void HandleRegisterPacket(JObject json)
        //        {
        //            Console.WriteLine("RegisterPacket Received");

        //            var packet = new RegisterPacket(json);
        //            var user = new User(packet.Nickname, packet.Username, packet.Passhash);
        //            Datastorage.Instance.AddUser(user);

        //            var returnPacket = new RegisterResponsePacket(Statuscode.Status.Ok);
        //            Send(returnPacket);

        //#if DEBUG
        //            Console.WriteLine(packet.ToString());
        //            Console.WriteLine(returnPacket.ToString());
        //#endif
        //        }

        private void HandleDisconnectPacket(JObject json)
        {
            Console.WriteLine("DisconnectPacket Received");

            var packet = new DisconnectPacket(json);

            var returnPacket = new ResponsePacket(Statuscode.Status.Unauthorized);
            if (Authentication.CheckLoggedIn(packet.Username))
            {
                Authentication.DeAuthenticate(packet.Username);
                returnPacket = new ResponsePacket(Statuscode.Status.Ok);
            }

            Send(returnPacket);

#if DEBUG
            Console.WriteLine(packet.ToString());
            Console.WriteLine(returnPacket.ToString());
#endif
        }