public LoginServerProtocol() : base()
        {
            responses = new Dictionary <Request, Response>();
            opcodes   = new Dictionary <string, int>();
            user      = new Account();

            #region OPCODE
            opcodes["LOGIN"]          = 0;
            opcodes["REGISTER"]       = 1;
            opcodes["CHANGE_PWD"]     = 2;
            opcodes["GETUSER"]        = 3;
            opcodes["UPDATEUSERDATA"] = 4;
            opcodes["FILEADD"]        = 5;
            opcodes["FILEALL"]        = 6;
            opcodes["FILEDELETE"]     = 7;
            opcodes["FILEUPDATE"]     = 8;
            opcodes["FILEOPEN"]       = 9;
            opcodes["GETLOGS"]        = 10;

            #endregion

            #region LOGIN_AND_REGISTRATION
            responses[new Request(opcodes["LOGIN"], "LOGIN", null, null)] = new Response(0,
                                                                                         (userName, pass) =>
            {
                loginAttempServer();
                if (CheckUser(userName))
                {
                    userName = userName.ToLower().Trim(new char[] { '\r', '\n', '\0' });
                    if (!UsersDatabase.UserIsLogged(userName))
                    {
                        if (UsersDatabase.CheckPassword(userName, pass))
                        {
                            user = UsersDatabase.GetUserWithDatabase(userName);
                            UsersDatabase.UpdateLoginStatus(user);
                            user.Status = Account.StatusCode.logged;
                            loginUserServer(user.Id, user.Login);
                        }
                        else
                        {
                            user.Status = Account.StatusCode.inv_pass;
                        }
                    }
                    else
                    {
                        user.Status = Account.StatusCode.user_is_logged;
                    }
                }
            },
                                                                                         null,
                                                                                         (args) =>
            {
                return(GetLogStatus());
            });

            responses[new Request(opcodes["REGISTER"], "REGISTER", null, null)] = new Response(0,
                                                                                               (userName, pass) =>
            {
                registrationAttempServer();
                if (!CheckUser(userName) && user.Status == Account.StatusCode.user_does_not_exist)
                {
                    if (pass.Length == 64)
                    {
                        UsersDatabase.AddUser(userName, pass);
                        user.Status = Account.StatusCode.successful_registration;
                    }
                    else
                    {
                        user.Status = Account.StatusCode.inv_pass;
                    }
                }

                if (user.Status != Account.StatusCode.successful_registration)
                {
                    if (userName.Length > 0)
                    {
                        user.Status = Account.StatusCode.user_exists;
                    }
                    else
                    {
                        user.Status = Account.StatusCode.inv_user;
                    }
                }
            },
                                                                                               null,
                                                                                               (args) =>
            {
                return(GetLogStatus());
            });

            responses[new Request(opcodes["CHANGE_PWD"], "CHANGE_PWD", null, null)] = new Response(0,
                                                                                                   (oldPass, newPass) =>
            {
                if (user.IsLogged)
                {
                    if (UsersDatabase.CheckPassword(user.Login, oldPass))
                    {
                        if (UsersDatabase.ChangePassword(user.Login, oldPass, newPass))
                        {
                            user.Status = Account.StatusCode.change_pwd;
                        }
                        else
                        {
                            user.Status = Account.StatusCode.change_pwd_error;
                        }
                    }
                    else
                    {
                        user.Status = Account.StatusCode.inv_pass;
                    }
                }
                else
                {
                    user.Status = Account.StatusCode.must_be_logged;
                }
            },
                                                                                                   null,
                                                                                                   (args) =>
            {
                return(GetLogStatus());;
            });

            responses[new Request(opcodes["GETUSER"], "GETUSER")] = new Response(0,
                                                                                 () =>
            {
                if (user.IsLogged)
                {
                    user.Status = Account.StatusCode.get_all_user_data;
                }
                else
                {
                    user.Status = Account.StatusCode.must_be_logged;
                }
            },
                                                                                 (args) =>
            {
                return(GetLogStatus());
            });
            responses[new Request(opcodes["UPDATEUSERDATA"], "UPDATEUSERDATA", null)] = new Response(0,
                                                                                                     (string data) =>
            {
                var tokens = SplitUserData(data);
                if (user.IsLogged)
                {
                    if (ValidUserData(tokens))
                    {
                        UsersDatabase.UpdateUserData((int)user.Id, tokens);
                    }
                    else
                    {
                        user.Status = Account.StatusCode.user_inv_data;
                    }
                }
                else
                {
                    user.Status = Account.StatusCode.must_be_logged;
                }
            },
                                                                                                     (args) =>
            {
                return(GetLogStatus());
            });

            #endregion

            #region FILE
            responses[new Request(opcodes["FILEADD"], "FILEADD", null, null)] = new Response(0,
                                                                                             (fileName, text) =>
            {
                if (!CheckFile(fileName) && user.FileStatus == Account.FileCode.file_does_not_exist)
                {
                    userAddFileServer(user.Id, user.Login, fileName);
                    FileDatabase.AddFile(fileName, text, (int)user.Id);
                    user.FileStatus = Account.FileCode.file_added;
                }
            },
                                                                                             null,
                                                                                             (args) =>
            {
                return(GetFileStatus());
            });


            responses[new Request(opcodes["FILEALL"], "FILEALL")] = new Response(0,
                                                                                 () =>
            {
                if (user.IsLogged)
                {
                    user.FileStatus = Account.FileCode.get_all;
                }
                else
                {
                    user.FileStatus = Account.FileCode.must_be_logged;
                }
            },
                                                                                 (args) =>
            {
                return(GetFileStatus());
            });

            responses[new Request(opcodes["FILEDELETE"], "FILEDELETE", null)] = new Response(0,
                                                                                             (fileName) =>
            {
                if (CheckFile(fileName))
                {
                    FileDatabase.DeleteFile(fileName, (int)user.Id);
                    if (!FileDatabase.FileExists(fileName, (int)user.Id))
                    {
                        userDelFileServer(user.Id, user.Login, fileName);
                        user.FileStatus = Account.FileCode.file_deleted;
                    }
                    else
                    {
                        user.FileStatus = Account.FileCode.file_deleted_error;
                    }
                }
            },
                                                                                             (args) =>
            {
                return(GetFileStatus());
            });


            responses[new Request(opcodes["FILEUPDATE"], "FILEUPDATE", null, null)] = new Response(0,
                                                                                                   (fileName, text) =>
            {
                if (CheckFile(fileName))
                {
                    userUpdateFileServer(user.Id, user.Login, fileName);
                    FileDatabase.UpdateFile(fileName, (int)user.Id, text);
                    user.FileStatus = Account.FileCode.file_update;
                }
            },
                                                                                                   null,
                                                                                                   (args) =>
            {
                return(GetFileStatus());
            });

            responses[new Request(opcodes["FILEOPEN"], "FILEOPEN", null)] = new Response(0,
                                                                                         (fileName) =>
            {
                if (CheckFile(fileName))
                {
                    userOpenFileServer(user.Id, user.Login, fileName);
                    user.FileStatus = Account.FileCode.file_open;
                }
            },
                                                                                         (fileName) =>
            {
                if (user.FileStatus == Account.FileCode.file_open)
                {
                    return(FileDatabase.openFile(fileName, (int)user.Id));
                }
                return(GetFileStatus());
            });

            responses[new Request(opcodes["GETLOGS"], "GETLOGS")] = new Response(0,
                                                                                 () =>
            {
                if (user.IsLogged)
                {
                    user.FileStatus = Account.FileCode.get_logs;
                }
                else
                {
                    user.FileStatus = Account.FileCode.must_be_logged;
                }
            },
                                                                                 (args) =>
            {
                return(GetFileStatus());
            });
            #endregion
        }