Пример #1
0
        public static void Run()
        {
            Thread cmdListener = new Thread(ServerMainThread.CmdListener);

            cmdListener.Start();

            Socket newClient = null;
            string msgAll    = String.Empty;
            string msg       = String.Empty;
            string cmd       = String.Empty;

            while (!closed)
            {
                try
                {
                    newClient = m_Socket.Accept();
                } catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }



                try {
                    msgAll = SocketIO.ReceiveAll(newClient);
                    msg    = msgAll.Substring(CmdPrefix.commandPrefixSize);
                    cmd    = msgAll.Substring(0, CmdPrefix.commandPrefixSize);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }



                if (cmd == CmdPrefix.authCommandPrefix)
                {
                    UserDescriptor tmpUser = SocketIO.Diserialize <UserDescriptor>(msg);

                    Console.WriteLine("[INFO/AUTH] Accepting connection with " + tmpUser.Name);

                    if (DataBaseControl.AuthUser(tmpUser))
                    {
                        Console.WriteLine("[INFO/AUTH] Success!");

                        SocketIO.SendAll(newClient, CmdPrefix.authSuccessCommandPrefix + "Access allowed!");
                        ClientThread client = new ClientThread(newClient, tmpUser, HostSettings.defaultChannelId);
                        DataBaseControl.AddClientToChannel(HostSettings.defaultChannelId, tmpUser, newClient);
                        DataBaseControl.SendLast_N_MessagesToThisUser(HostSettings.defaultChannelId, newClient, 10);
                        Thread thr = new Thread(client.Run);
                        thr.Start();
                    }
                    else
                    {
                        Console.WriteLine("[INFO/AUTH] Fail!");
                        SocketIO.SendAll(newClient, CmdPrefix.authErrorCommandPrefix + "Access denied!");
                    }
                }

                if (cmd == CmdPrefix.registerCommandPrefix)
                {
                    UserDescriptor tmpUser = SocketIO.Diserialize <UserDescriptor>(msg);

                    Console.WriteLine("[INFO/REG] Accepting connection with " + tmpUser.Name);

                    if (DataBaseControl.RegisterNewUser(tmpUser))
                    {
                        Console.WriteLine("[INFO/REG] Success!");

                        SocketIO.SendAll(newClient, CmdPrefix.registerSuccessCommandPrefix + "Register success!");
                        ClientThread client = new ClientThread(newClient, tmpUser, HostSettings.defaultChannelId);
                        DataBaseControl.AddClientToChannel(HostSettings.defaultChannelId, tmpUser, newClient);
                        DataBaseControl.SendLast_N_MessagesToThisUser(HostSettings.defaultChannelId, newClient, 10);
                        Thread thr = new Thread(client.Run);
                        thr.Start();
                    }
                    else
                    {
                        Console.WriteLine("[INFO/REG] Fail!");
                        SocketIO.SendAll(newClient, CmdPrefix.registerErrorCommandPrefix + "This user already exists! Change name!");
                    }
                }
            }
        }