示例#1
0
        bool CheckRequestAndSend(FromClientCommand Command)
        {
            string stmpstring = Encoding.ASCII.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength);

            bool completeToSend = false;

            if (!clients.ContainsKey(Command.Client))
            {
                clients.Add(Command.Client, "");
            }

            if (stmpstring.IndexOf("\r\n\r\n") != -1)
            {
                clients[Command.Client] += stmpstring.Substring(0, stmpstring.IndexOf("\r\n\r\n") + 4);
                completeToSend           = true;
            }
            else
            {
                clients[Command.Client] += (String)stmpstring;
                if (clients[Command.Client].IndexOf("\r\n\r\n") != -1)
                {
                    clients[Command.Client] = clients[Command.Client].Substring(0, clients[Command.Client].IndexOf("\r\n\r\n") + 4);
                    completeToSend          = true;
                }
            }

            return(completeToSend);
        }
示例#2
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();
        }
示例#3
0
        void proxyServer_ClientReceiveCommand(FromClientCommand Command)
        {
            if (Command.Command == ClientActions.Receive)
            {
                if (CheckRequestAndSend(Command))
                {
                    HttpRequest request = null;
                    try
                    {
                        request = HttpParse.Parse(clients[Command.Client]);
                    }
                    catch { }

                    if (request != null)
                    {
                        /*// Текущий клиент остается висеть, до тех пор пока сам не отключится или его не отключат
                         * Manager.SockClient.Client client = new Manager.SockClient.Client();
                         * client.ServerReceiveCommand += new OnServerReceiveCommand(client_ServerReceiveCommand);
                         * client.SetConnectBuffer(Encoding.ASCII.GetBytes(clients[Command.Client]), 0, clients[Command.Client].Length);
                         * client.UserToken = Command.Client;
                         * if (request.Host.Split(new char[] { ':' }).Length == 2)
                         *      client.Connect(new System.Net.DnsEndPoint(request.Host.Split(new char[] { ':' })[0], int.Parse(request.Host.Split(new char[] { ':' })[1])));
                         * else
                         *      client.Connect(new System.Net.DnsEndPoint(request.Host, 80));
                         * */

                        Socket websocket = null;

                        try
                        {
                            //if (sMessage.Substring(0, 7) != "CONNECT" && iHostNameStart != -1 && iHostNameEnd != -1 && iHostNameEndFirstSlash != -1)
                            {
                                //This is a socket to the real webserver with the webpage
                                websocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                                if (request.Host.Split(new char[] { ':' }).Length == 2)
                                {
                                    websocket.Connect(new System.Net.DnsEndPoint(request.Host.Split(new char[] { ':' })[0], int.Parse(request.Host.Split(new char[] { ':' })[1])));
                                }
                                else
                                {
                                    websocket.Connect(new System.Net.DnsEndPoint(request.Host, 80));
                                }

                                websocket.Send(Encoding.ASCII.GetBytes(clients[Command.Client]), clients[Command.Client].Length, 0);
                                //Receive data from the webserver
                                int  ireceivedbyte = 0;
                                bool timeout       = false;
                                for (int i = 0; i < 200; i++)
                                {
                                    Thread.Sleep(10);
                                    if (websocket.Available != 0)
                                    {
                                        break;
                                    }

                                    if (i == 200)
                                    {
                                        timeout = true;
                                    }
                                }

                                if (timeout)
                                {
                                    websocket.Shutdown(SocketShutdown.Both);
                                    websocket.Close();

                                    //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } });

                                    //if (clientSocket != null && clientSocket.Connected)
                                    //	clientSocket.Close();
                                }

                                while (websocket.Available != 0)
                                {
                                    byte[] receivebuffer = new byte[websocket.Available];
                                    ireceivedbyte = websocket.Receive(receivebuffer, receivebuffer.Length, 0);
                                    //Console.WriteLine("Received from webserver socket {0} bytes", +ireceivedbyte);

                                    //clientSocket.Send(receivebuffer, ireceivedbyte, 0);
                                    proxyServer.Send(new ToServerCommand()
                                    {
                                        Action = ServerCommands.Send, ReceiveBuffer = receivebuffer, ToClient = new List <OnServerClientConnection> {
                                            (OnServerClientConnection)Command.Client
                                        }
                                    });
                                }

                                websocket.Shutdown(SocketShutdown.Both);
                                websocket.Close();
                                //clientSocket.Receive(readbyte, 4096, 0);
                            }
                            //else
                            //{
                            //	ireadbytes = 0;
                            //}
                        }
                        catch
                        {
                        }
                        finally
                        {
                            //ireadbytes = 0;
                            //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } });

                            //if (clientSocket != null && clientSocket.Connected)
                            //	clientSocket.Close();
                        }
                    }
                    else
                    {
                        //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } });
                        clients.Remove(Command.Client);
                    }
                }
            }

            if (Command.Command == ClientActions.Disconnected)
            {
                clients.Remove(Command.Client);
            }

            if (Command.Command == ClientActions.Shutdown)
            {
                clients.Remove(Command.Client);
            }
        }
示例#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();
            }
        }