Example #1
0
        public void SendCommand <T>(string message) where T : TemplateCommand
        {
            if (!isWorking)
            {
                allocToDrop(info, new Exception("Client dosen't connect"));
                return;
            }

            if (message == null)
            {
                message = "";
            }

            var buffer = commandController.GetBufferMessage <T>(message);

            if (buffer == null)
            {
                DebugAdapter.Log($"Havan't \"{typeof(T).Name}\" command");
                return;
            }

            var descripte = commandController.GetDescriptMessage(buffer);

            sendner.asynSendMessageFromClient(descripte, info, allocSended, allocToDrop);
        }
 private void allocDropClientSendMessage(ClientInfo client, Exception e)
 {
     DebugAdapter.Log("Drop client: " + e);
     DisconnectClient(client.Name);
     SendCommandFromAllClients <TemplateDisconnect>(client.Name);
     DebugAdapter.Log($"Client \"{client.Name}\" has been removed");
 }
        private void allocFindedClient(ClientInfo client)
        {
            if (Clients.ContainsKey(client.Name))
            {
                CloseClient(client);
                return;
            }

            Clients.Add(client.Name, client);
            DebugAdapter.Log($"Client \"{client.Name}\" has join to server");

            sendner.asynListeningClient(client, allocSendedMessageClientFromServer, allocDropClientSendMessage);
        }
        public void StopServer()
        {
            foreach (var clientInfo in Clients)
            {
                clientInfo.Value.ClientTCP.GetStream().Close();
                clientInfo.Value.ClientTCP.Close();
            }

            listner.Stop();
            sendner.Stop();
            isWorking = false;

            DebugAdapter.Log("Server is stoped");
        }
Example #5
0
        public void Connect(string ip, int port)
        {
            if (isWorking)
            {
                DebugAdapter.Log("You already connect");
                return;
            }

            info = new ClientInfo(info.Name, info.hashPassword);
            info.ClientTCP.Connect(ip, port);
            stream = info.ClientTCP.GetStream();
            sendner.Start();
            isWorking = true;
            SendCommand <TemplateConnect>(info.Name + " " + info.hashPassword);
            sendner.asynListeningClient(info, allocServerSended, allocToDrop);
        }
Example #6
0
 public void AbortClient()
 {
     try
     {
         info.ClientTCP.Close();
         stream.Close();
         sendner.Stop();
     }
     catch
     { }
     finally
     {
         isWorking = false;
         DebugAdapter.Log("Client disconnected");
     }
 }
        public void StartServer()
        {
            if (isWorking)
            {
                return;
            }

            isWorking = true;

            DebugAdapter.Log("Server is running");
            DebugAdapter.Log($"Server password: {ServerPassword}");


            listner.Start();
            sendner.Start();
            sendner.asynFindingClient(listner, ServerPassword, allocFindedClient, allocBreackServer);
        }
        private void allocSendedMessageClientFromServer(ClientInfo client, byte[] buffer)
        {
            var descript = commandController.GetDescriptWhithCommands(buffer, this, null);

            if (descript == null)
            {
                DebugAdapter.Log($"Server take brake message from {client.Name}");
            }
            else if (descript.Tempate is TemplateDisconnect)
            {
                DisconnectClient(client.Name);
            }
            else
            {
                DebugAdapter.Log($"Server take message from {client.Name}: {descript.Message}");
                SendCommandFromAllClients(descript);
            }
        }
 private void allocBreackServer(Exception e)
 {
     DebugAdapter.Log("Server is crashed: " + e.Message);
     StopServer();
 }
Example #10
0
        private void allocServerSended(ClientInfo info, byte[] buffer)
        {
            var message = commandController.GetDescriptWhithCommands(buffer, null, this);

            DebugAdapter.Log($"Take message: {message.Message}");
        }
Example #11
0
 private void allocSended(ClientInfo info, MessageDescript descript)
 {
     DebugAdapter.Log($"Sended: {descript.Message}");
 }
Example #12
0
 private void allocToDrop(ClientInfo info, Exception exception)
 {
     DebugAdapter.Log($"Client drop: {exception}");
 }