Beispiel #1
0
        /// <summary>
        /// Lắng nghe phản hồi từ phía client
        /// </summary>
        void Receive(object obj)
        {
            Socket client = obj as Socket;

            try
            {
                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024 * 20];
                    client.Receive(buffer);

                    object         data      = Deserialize(buffer);
                    ServerResponse container = data as ServerResponse;

                    switch (container.Type)
                    {
                    case ServerResponseType.SendStudent:

                        Student student = container.Data as Student;
                        AddMessage(client.RemoteEndPoint.ToString() + ": Thông tin sinh viên thao tác trên máy: " + student.FullNameAndId);

                        break;

                    case ServerResponseType.SendFile:

                        FileResponse file = container.Data as FileResponse;

                        string fileName = file.FileInfo.Name;
                        AddMessage(client.RemoteEndPoint.ToString() + ": Đã nhận bài làm, tập tin có tên: " + fileName);

                        using (var fileStream = File.Create(fileName))
                        {
                            fileStream.Write(file.FileContent, 0, file.FileContent.Length);
                        }

                        break;

                    case ServerResponseType.SendList:
                        break;

                    case ServerResponseType.SendString:

                        string computerName = (string)container.Data;

                        AddMessage(client.RemoteEndPoint.ToString() + ": Tên máy: " + computerName);

                        break;

                    case ServerResponseType.BeginExam:
                        break;

                    case ServerResponseType.FinishExam:
                        break;

                    case ServerResponseType.LockClient:
                        break;

                    default:
                        break;
                    }
                }
            }
            catch
            {
                AddMessage(client.RemoteEndPoint.ToString() + ": " + "Đã đóng kết nối");
                clientList.Remove(client);
                client.Close();
            }
        }
        public void HandleMessage(ServerResponse sender, Event e)
        {
            Event.EventTypeEnum enumType = e.EventType;
            Account             acc      = new Account("NULL", "NULL");

            switch (enumType)
            {
            case Event.EventTypeEnum.CreateAccount:
                //Check list of all accounts and make sure the username isn't taken. If not, send new account
                LoginEventData createAccData = JsonConvert.DeserializeObject <LoginEventData>(e.EventData.ToString());
                bool           canCreate     = true;
                if (accountList == null)
                {
                    accountList = new List <Account>();
                }
                for (int i = 0; i < accountList.Count && canCreate; i++)
                {
                    string s = accountList.ElementAt(i).Username;
                    if (s == createAccData.Username)
                    {
                        canCreate = false;
                    }
                }
                if (canCreate)
                {
                    acc          = new Account(createAccData.Username, createAccData.Password);
                    acc.IsOnline = true;
                    accountList.Add(acc);
                    updateActivityList("New user created: " + createAccData.Username);
                    updateUserList(accountList);

                    //Send success response
                    //sender.SendResponseToClient(new Event(Event.EventTypeEnum.ServerResponseSuccess, null));

                    //Send account data
                    sender.SendResponseToClient(new Event(Event.EventTypeEnum.ServerSendAccount, new AccountData(acc)));
                }
                else
                {
                    updateActivityList("User attempted to create an account with the same username as one that exists.");
                    sender.SendResponseToClient(new Event(Event.EventTypeEnum.ServerResponseError, new ServerErrorData("Account already exists. Try again.")));
                }
                break;

            case Event.EventTypeEnum.LoginAttempt:
                //Find if the username exits. If so, make sure the password is correct. Send account data
                bool           found     = false;
                LoginEventData loginData = JsonConvert.DeserializeObject <LoginEventData>(e.EventData.ToString());
                for (int i = 0; i < accountList.Count && !found; i++)
                {
                    string u = accountList.ElementAt(i).Username;
                    if (u == loginData.Username)
                    {
                        found = true;
                        acc   = accountList.ElementAt(i);
                    }
                }
                if (loginData.Password == acc.Password)
                {
                    //Successful login attempt
                    updateActivityList("User " + loginData.Username + " has logged in");
                    acc.IsOnline = true;
                    updateUserList(accountList);

                    sender.SendResponseToClient(new Event(Event.EventTypeEnum.ServerSendAccount, new AccountData(acc)));
                }
                else
                {
                    //Unsuccessful login attempt
                    updateActivityList("User " + loginData.Username + " made bad login attempt");
                    sender.SendResponseToClient(new Event(Event.EventTypeEnum.ServerResponseError, new ServerErrorData("We couldn't find that username/password combo. Try again.")));
                }
                break;

            case Event.EventTypeEnum.UserLogOff:
                LoginEventData logOffData = JsonConvert.DeserializeObject <LoginEventData>(e.EventData.ToString());
                foreach (Account a in accountList)
                {
                    if (a.Username == logOffData.Username)
                    {
                        updateActivityList(a.Username + " has logged off");
                        a.IsOnline = false;
                        updateUserList(accountList);
                        break;
                    }
                }
                break;

            case Event.EventTypeEnum.ClientStockRequest:
                StockRequestData reqData        = JsonConvert.DeserializeObject <StockRequestData>(e.EventData.ToString());
                string           reqSymbol      = reqData.StockSymbol.ToUpper();
                bool             inStockList    = false;
                Stock            reqStockToSend = new Stock("NULL");
                for (int i = 0; i < stockList.Count; i++)
                {
                    string tStock = stockList.ElementAt(i).TickerSymbol;
                    if (tStock == reqSymbol)
                    {
                        inStockList = true;
                        stockList.ElementAt(i).UpdateStock();
                        reqStockToSend = stockList.ElementAt(i);
                        break;
                    }
                }
                if (!inStockList)
                {
                    Stock tStock = new Stock(reqSymbol);
                    reqStockToSend = tStock;
                    stockList.Add(tStock);
                }
                StockData sd             = new StockData(reqStockToSend);
                Event     reqEventToSend = new Event(Event.EventTypeEnum.ServerSendStock, sd);
                sender.SendResponseToClient(reqEventToSend);
                updateStockList(stockList);
                break;

            case Event.EventTypeEnum.NULLEVENTENUM:
                updateActivityList("Null event received in ServerHandler.HandleMessage()");
                break;
            }
        }