Esempio n. 1
0
        public static void Listen()
        {
            TcpListener cache = new TcpListener(IPAddress.Parse("127.0.0.1"), port);

            cache.Start();
            Syslog.Log("Bouncer is listening on port " + port);
            Ping = DateTime.Now;
            while (true)
            {
                client = cache.AcceptTcpClient();
                NetworkStream temp = client.GetStream();
                local_writer = new StreamWriter(temp);
                local_reader = new StreamReader(temp, Encoding.UTF8);
                Syslog.Log("New client has connected to bouncer");
                try
                {
                    while (!local_reader.EndOfStream)
                    {
                        string data = local_reader.ReadLine();
                        if (string.IsNullOrEmpty(data))
                        {
                            continue;
                        }
                        if (data[0] != 'C' || !data.StartsWith("CONTROL: "))
                        {
                            Buffer.Out(data);
                        }
                        else
                        {
                            string code      = data.Replace("\r", "").Substring("CONTROLxx".Length);
                            string parameter = "";
                            if (code.Contains(" "))
                            {
                                int sidx = code.IndexOf(" ");
                                parameter = code.Substring(sidx + 1);
                                code      = code.Substring(0, sidx);
                            }
                            switch (code)
                            {
                            case "STATUS":
                                if (IsConnectedOnRemote)
                                {
                                    Buffer.In("CONTROL: TRUE", true);
                                }
                                else
                                {
                                    Buffer.In("CONTROL: FALSE", true);
                                }
                                break;

                            case "CONNECT":
                            case "CREATE":
                                Syslog.Log("Connecting to remote server: " + parameter);
                                StartIRC(parameter);
                                break;

                            case "DISCONNECT":
                                Disconnect();
                                SendDisconnectOnRemote();
                                break;
                            }
                        }
                        Thread.Sleep(20);
                    }
                    Syslog.Log("Client has disconnect on EOF");
                }
                catch (IOException)
                {
                    Syslog.Log("Client has disconnected on IOEX term");
                }
                Thread.Sleep(20);
            }
        }
Esempio n. 2
0
        public static void Connect()
        {
            listener = new Thread(Listen);
            listener.Start();
            irc = new Thread(Init);
            irc.Start();
            int ping = 0;

            while (true)
            {
                try
                {
                    if (client != null && client.Connected && Buffer.IncomingData.Count > 0)
                    {
                        BufferItem lastitem;
                        lock (Buffer.IncomingData)
                        {
                            lastitem = Buffer.IncomingData[0];
                            foreach (BufferItem Item in Buffer.IncomingData)
                            {
                                if (Item.Important)
                                {
                                    lastitem = Item;
                                    break;
                                }
                            }
                            Buffer.IncomingData.Remove(lastitem);
                        }
                        local_writer.WriteLine(lastitem.Text);
                        local_writer.Flush();
                    }

                    if (IsConnectedOnRemote)
                    {
                        if (Buffer.OutgoingData.Count > 0)
                        {
                            BufferItem lastitem;
                            lock (Buffer.OutgoingData)
                            {
                                lastitem = Buffer.OutgoingData[0];
                                Buffer.OutgoingData.Remove(lastitem);
                            }
                            remote_writer.WriteLine(lastitem.Text);
                            remote_writer.Flush();
                        }
                        ping++;
                        if (ping > 2000)
                        {
                            if ((DateTime.Now - Ping).Minutes > 2)
                            {
                                // no response from server within 2 minutes
                                SendDisconnectOnRemote();
                                Syslog.Log("Remote didn't respond for long time, closing connection");
                                Disconnect();
                                ping = 0;
                                continue;
                            }
                            ping = 0;
                            remote_writer.WriteLine("PING :" + DateTime.Now.ToBinary());
                            remote_writer.Flush();
                        }
                    }
                }
                catch (Exception fail)
                {
                    Console.Write(fail.ToString());
                }
                Thread.Sleep(10);
            }
        }