Exemplo n.º 1
0
        void ServerModule_ClientReceiveCommand(FromClientCommand Command)
        {
            if (Command.Command == ClientActions.Disconnected || Command.Command == ClientActions.Shutdown)
            {
                TB_Messages.AppendText(Command.Client.ClientEndPoint + " Server Message: " + Command.Command + "\n");
                LB_Users.Items.Remove(Command.Client);

                Clients.Remove(Command.Client.ClientEndPoint);
            }
            else
            {
                if (Command.Command == ClientActions.Connected)
                {
                    TB_Messages.AppendText(Command.Client.ClientEndPoint + " Server Message: " + Command.Command + "\n");
                    LB_Users.Items.Add(Command.Client);

                    Manager.SockClient.Client ClientModule = new Manager.SockClient.Client();
                    ClientModule.ReceiveBufferSize     = 65535;
                    ClientModule.ServerReceiveCommand += new OnServerReceiveCommand(ClientModule_ServerReceiveCommand);
                    ClientModule.UserToken             = Command.Client.ClientEndPoint;

                    Clients.Add(Command.Client.ClientEndPoint, new object[] { Command.Client, ClientModule });
                    // Создаем новое подключение для клиента и заносим его в список клиентов
                }
                else
                {
                    if (Command.Command == ClientActions.Receive)
                    {
                        byte[] buf = new byte[Command.ReceiveBufferLength];
                        Array.Copy(Command.ReceiveBuffer, 0, buf, 0, Command.ReceiveBufferLength);

                        if (!((Manager.SockClient.Client)Clients[Command.Client.ClientEndPoint][1]).isConnected)
                        {
                            ((Manager.SockClient.Client)Clients[Command.Client.ClientEndPoint][1]).Connect(new DnsEndPoint("192.168.0.132", 1348), buf);
                        }

                        //TB_Messages.AppendText(Command.Client.ClientInfo + " Server Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");
                        TB_Messages.AppendText(Command.Client.ClientEndPoint + " Server Message: " + Command.Command + "\n");
                        // По приходу сообщения от клиента, находим его подключение в списке клиентов и отправляем его туда
                    }
                    else
                    {
                        //TB_Messages.AppendText(Command.Client.ClientInfo + " Server Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");
                    }
                }
            }
            TB_Messages.ScrollToEnd();
        }
Exemplo n.º 2
0
        void client_ServerReceiveCommand(FromServerCommand Command)
        {
            if (Command.Action == ServerActions.SendCompleted || Command.Action == ServerActions.Connected)
            {
                TB_MessageField.IsEnabled = true;
            }

            if (Command.ReceiveBuffer == null)
            {
                TB_Messages.AppendText(DateTime.Now.ToString("HH:mm:ss.f") + " Message: " + Command.Action + "\n");
            }
            else
            {
                TB_Messages.AppendText(DateTime.Now.ToString("HH:mm:ss.f") + " Message: " + Command.Action + ", Buffer: " + Encoding.UTF8.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");
            }
            TB_Messages.ScrollToEnd();
        }
Exemplo n.º 3
0
        void ClientModule_ServerReceiveCommand(FromServerCommand Command)
        {
            if (Command.Action == ServerActions.AlreadyConnected)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.Connected)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.ConnectedOverProxy)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.ConnectedToProxy)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.Connecting)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.ConnectingToProxy)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.ConnectionFailed)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.Disconnected)
            {
                try
                {
                    TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
                    ServerModule.Send(new ToServerCommand()
                    {
                        Action = ServerCommands.Disconnect, ToClient = new List <OnServerClientConnection>()
                        {
                            ((OnServerClientConnection)Clients[(EndPoint)Command.UserToken][0])
                        }
                    });
                }
                catch { }
            }
            else if (Command.Action == ServerActions.ProxyAuthFailed)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.Receive)
            {
                byte[] buf = new byte[Command.ReceiveBufferLength];
                Array.Copy(Command.ReceiveBuffer, 0, buf, 0, Command.ReceiveBufferLength);

                try
                {
                    ServerModule.Send(new ToServerCommand()
                    {
                        Action = ServerCommands.Send, ReceiveBuffer = buf, ToClient = new List <OnServerClientConnection>()
                        {
                            ((OnServerClientConnection)Clients[(EndPoint)Command.UserToken][0])
                        }
                    });
                }
                catch { }
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.SendCompleted)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
            else if (Command.Action == ServerActions.Shutdown)
            {
                TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n");
            }
        }
Exemplo n.º 4
0
        void ServerModule_ClientCommand(FromClientCommand Command)
        {
            if (Command.Command == ClientActions.Disconnected || Command.Command == ClientActions.Shutdown)
            {
                LB_Users.Items.Remove(Command.Client);
            }
            else if (Command.Command == ClientActions.Connected)
            {
                LB_Users.Items.Add(Command.Client);
            }

            if (Command.ReceiveBufferLength > 0)
            {
                if (Command.Command == ClientActions.Receive)
                {
                    if (Command.ReceiveBufferLength >= 4 && Crc8.ComputeChecksum(0, 3, Command.ReceiveBuffer) == Command.ReceiveBuffer[3])
                    {
                        if (Command.Client == null)
                        {
                            TB_Messages.AppendText(DateTime.Now.ToString() + ": " + "Message: " + Command.Command + ", Buffer length: " + Command.ReceiveBufferLength + "\n");
                        }
                        else
                        {
                            if ((LowCommands)Command.ReceiveBuffer[2] == LowCommands.Version)
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 4, Command.ReceiveBufferLength - 4 - 1) + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                            else if ((LowCommands)Command.ReceiveBuffer[2] == LowCommands.RunType)
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Value: " + BitConverter.ToInt32(Command.ReceiveBuffer, 4) + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                            else
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Buffer length: " + Command.ReceiveBufferLength + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                        }
                    }
                    else
                    {
                        ServerModule.Send(new ToServerCommand()
                        {
                            Action = ServerCommands.Disconnect, ToClient = new List <OnServerClientConnection>()
                            {
                                Command.Client
                            }
                        });
                    }
                }

                //TB_Messages.AppendText(Command.Client.ClientInfo + " Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");

                //TB_Messages.AppendText(Command.Client.ClientInfo + " Message: " + Command.Command + ", Buffer: " + BitConverter.ToString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");
            }
            else
            {
                if (Command.Client == null)
                {
                    TB_Messages.AppendText(DateTime.Now.ToString() + ": " + "Message: " + Command.Command + "\n");
                }
                else
                {
                    TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + "\n");
                }
            }

            if ((TB_Messages.ExtentHeight - TB_Messages.VerticalOffset - TB_Messages.ViewportHeight) < 50)
            {
                TB_Messages.ScrollToEnd();
            }
        }