public void run()
        {
            running = true;

            NetHelper.SendNetCommand(client, new NetCommand(session));

            while (running)
            {
                NetCommand input = NetHelper.ReadNetCommand(client);

                switch (input.Type)
                {
                case NetCommand.CommandType.SESSION:

                    break;

                case NetCommand.CommandType.LOGIN:
                    if (!server.CheckPassword(input.DisplayName, input.Password))
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
                        loggedin = false;
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
                        loggedin = true;

                        if (input.IsDoctor)
                        {
                            server.ChangeClientToDoctor(client, this);
                            Console.WriteLine("Doctor connected");
                            running = false;
                        }
                        else
                        {
                            name     = input.DisplayName;
                            loggedin = true;
                            FileHandler.CreateSession(session, name);
                        }
                    }
                    break;

                case NetCommand.CommandType.DATA:
                    if (loggedin)
                    {
                        metingen.Add(input.Meting);
                        server.SendToDoctor(input);
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
                    }

                    break;

                case NetCommand.CommandType.CHAT:
                    if (loggedin)
                    {
                        chat.Add(new ChatMessage(name, input.ChatMessage, false));
                        server.SendToDoctor(input);
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
                    }
                    break;

                case NetCommand.CommandType.LOGOUT:
                    loggedin = false;
                    running  = false;
                    Console.WriteLine(name + " logged out");
                    FileHandler.WriteMetingen(session, metingen);
                    FileHandler.WriteChat(session, chat);
                    client.Close();
                    break;

                case NetCommand.CommandType.ERROR:
                    Console.WriteLine("An error occured, assuming client disconnected");
                    loggedin = false;
                    running  = false;
                    Console.WriteLine(name + " logged out due to an error");
                    FileHandler.WriteMetingen(session, metingen);
                    FileHandler.WriteChat(session, chat);
                    client.Close();
                    break;

                case NetCommand.CommandType.REQUEST:
                    switch (input.Request)
                    {
                    case NetCommand.RequestType.ALLSESSIONS:
                        List <Tuple <int, string, double> > sessions = FileHandler.GetAllSessions();
                        SendToClient(new NetCommand(NetCommand.LengthType.SESSIONS, sessions.Count,
                                                    input.Session));
                        foreach (Tuple <int, string, double> session in sessions)
                        {
                            SendToClient(new NetCommand(session.Item2, session.Item3, session.Item1));

                            Thread.Sleep(10);
                        }
                        break;

                    case NetCommand.RequestType.OLDDATA:
                        List <Meting> metingen = FileHandler.ReadMetingen(input.Session);
                        foreach (Meting meting in metingen)
                        {
                            Thread.Sleep(10);
                            SendToClient(new NetCommand(meting, input.Session));
                        }
                        break;

                    default:
                        throw new FormatException("Unknown command");
                    }
                    break;

                default:
                    if (loggedin)
                    {
                        throw new FormatException("Unknown command");
                    }
                    break;
                }
            }

            server.RemoveActiveSession(this);
        }
Пример #2
0
        public void run()
        {
            running = true;

            NetHelper.SendNetCommand(client, new NetCommand(session));

            while (running)
            {
                NetCommand input = NetHelper.ReadNetCommand(client);

                switch (input.Type)
                {
                case NetCommand.CommandType.SESSION:

                    break;

                case NetCommand.CommandType.LOGIN:
                    if (!server.CheckPassword(input.DisplayName, input.Password))
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
                        loggedin = false;
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
                        loggedin = true;

                        if (input.IsDoctor)
                        {
                            server.ChangeClientToDoctor(client, this);
                            Console.WriteLine("Doctor connected");
                            running = false;
                        }
                        else
                        {
                            name     = input.DisplayName;
                            loggedin = true;
                            FileHandler.CreateSession(session, name);
                        }
                    }
                    break;

                case NetCommand.CommandType.UITLEG:
                    if (loggedin)
                    {
                        server.SendToDoctor(input);
                    }
                    break;

                case NetCommand.CommandType.PERSONDATA:
                    if (loggedin)
                    {
                        personaldatareceived = true;
                        FileHandler.SavePersonalData(input);
                    }
                    break;

                case NetCommand.CommandType.TESTRESULT:
                    if (loggedin)
                    {
                        FileHandler.SaveTestResult(input);
                    }
                    break;

                case NetCommand.CommandType.DATA:
                    if (loggedin)
                    {
                        metingen.Add(input.Meting);
                        server.SendToDoctor(input);
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
                    }

                    break;

                case NetCommand.CommandType.CHAT:
                    if (loggedin)
                    {
                        chat.Add(new ChatMessage(name, input.ChatMessage, false));
                        server.SendToDoctor(input);
                    }
                    else
                    {
                        NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
                    }
                    break;

                case NetCommand.CommandType.LOGOUT:
                    loggedin = false;
                    running  = false;
                    Console.WriteLine(name + " logged out");
                    FileHandler.WriteMetingen(session, metingen);
                    FileHandler.WriteChat(session, chat);
                    client.Close();
                    server.SendToDoctor(new NetCommand(session, "De client heeft uitgelogd."));
                    server.RemoveBacklogDisconnectedClient(session);
                    break;

                case NetCommand.CommandType.ERROR:
                    Console.WriteLine("An error occured, assuming client disconnected");
                    loggedin = false;
                    running  = false;
                    Console.WriteLine(name + " logged out due to an error");
                    FileHandler.WriteMetingen(session, metingen);
                    FileHandler.WriteChat(session, chat);
                    client.Close();
                    break;

                default:
                    if (loggedin)
                    {
                        throw new FormatException("Unknown command");
                    }
                    break;
                }
            }

            server.RemoveActiveSession(this);
        }