Ejemplo n.º 1
0
        public Server()
        {
            Log.LogInfo("Setting up...", LogType.Application_Work);

            //create server parameter file if it doesn't exist
            if (!File.Exists(Globals.BASE_DIRECTORY + Globals.PARAMETERS_PATH))
            {
                JSONManagement.WriteToJsonFile <ServerParameters>(
                    Globals.BASE_DIRECTORY + Globals.PARAMETERS_PATH,
                    new ServerParameters(8407, "127.0.0.1"));
            }

            ServerParameter               = JSONManagement.ReadFromJsonFile <ServerParameters>(AppDomain.CurrentDomain.BaseDirectory + Globals.PARAMETERS_PATH);
            InternalTopicManager          = new TopicManager();
            InternalPrivateMessageManager = new PrivateMessageManager();
            ClientLinks = new List <ClientLink>();
            //Disconnect gracefully every link when server closes
            AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
        }
Ejemplo n.º 2
0
        public Client()
        {
            Log.LogInfo("Setting up Client...", LogType.Application_Work);
            KeepWorking = true;

            if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + Globals.PARAMETERS_PATH))
            {
                JSONManagement.WriteToJsonFile <ClientParameters>(
                    AppDomain.CurrentDomain.BaseDirectory + Globals.PARAMETERS_PATH,
                    new ClientParameters(8407, "127.0.0.1"));
            }

            ClientParameters clientParameters = JSONManagement.ReadFromJsonFile <ClientParameters>(AppDomain.CurrentDomain.BaseDirectory + Globals.PARAMETERS_PATH);

            this.Port     = clientParameters.port;
            this.Hostname = clientParameters.ipAddress;
            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken       ct  = cts.Token;
            Action action = () => Connect(ct);

            //start trying to connect
            Task task = new Task(action, ct);

            task.Start();

            //event subscribing for disconnecting when app closes
            AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;

            //wait 15 sec (in ms) before timeout
            if (task.Wait(15000))
            {
                Log.LogInfo("Connected to server", LogType.Connection_To_Server);
                Console.WriteLine("Connected to server " + Hostname + ":" + Port);
            }
            else
            {
                Log.LogInfo("Connection to server failed", LogType.Connection_To_Server);
                Console.WriteLine("Could not connect to server " + Hostname + ":" + Port);
            }
            cts.Cancel();
            cts.Dispose();
            task.Wait();
            task.Dispose();
            Log.LogInfo("Shutting down the connection process.", LogType.Application_Work);

            Console.WriteLine("you are now connected to the chat server");
            State = ClientState.Login;
            do
            {
                switch (State)
                {
                case ClientState.Login:
                    Login();
                    break;

                case ClientState.Disconnecting:
                    Disconnect();
                    break;

                case ClientState.Working:
                    Work();
                    break;

                case ClientState.Exiting:
                    break;

                default:
                    break;
                }
            } while (State != ClientState.Disconnecting);
            Console.ReadLine();
        }