Ejemplo n.º 1
0
        private void DataIn(IAsyncResult ar)
        {
            if (Source == null || Destination == null)
            {
                return;
            }
            int Readed = 0;

            Connection From = (Connection)ar.AsyncState;
            Connection To   = From.Other;

            try
            {
                Readed = From.NS.EndRead(ar);
            }
            catch
            {
                ForwarderEvent?.Invoke(this, From == Source ? ForwarderEventType.ClientGone : ForwarderEventType.ServerGone);
            }
            if (Readed > 0)
            {
                From.BytesReceived += (ulong)Readed;
                DataTransmitted?.Invoke(this, Readed);

                /*Only enable if the connections behave weirdly. Causes massive performance issues.
                 #if DEBUG
                 * lock ("console")
                 * {
                 *  Console.ForegroundColor = From == Source ? ConsoleColor.Green : ConsoleColor.Yellow;
                 *  Console.Error.WriteLine(b2s(From.Data, Readed));
                 *  Console.ResetColor();
                 * }
                 #endif
                 * //*/
                try
                {
                    To.NS.Write(From.Data, 0, Readed);
                    From.Handle = From.NS.BeginRead(From.Data, 0, BUFFER, DataIn, From);
                }
                catch
                {
                    ForwarderEvent?.Invoke(this, To == Source ? ForwarderEventType.ClientGone : ForwarderEventType.ServerGone);
                }
            }
            else
            {
                ForwarderEvent?.Invoke(this, From == Source ? ForwarderEventType.ClientGone : ForwarderEventType.ServerGone);
            }
        }
Ejemplo n.º 2
0
        public void Start(int Timeout)
        {
            if (Destination == null)
            {
                if (Timeout < 0)
                {
                    throw new ArgumentOutOfRangeException("Timeout must be 0 or bigger");
                }
                Destination       = new Connection();
                Destination.Other = Source;
                Source.Other      = Destination;
                Destination.Data  = new byte[BUFFER];

                Source.Mode      = Connection.ConnectionMode.Client;
                Destination.Mode = Connection.ConnectionMode.Server;

                new Thread(delegate()
                {
                    Socket Temp = new Socket(SocketType.Stream, ProtocolType.Tcp);
                    try
                    {
                        Temp.Connect(Dest);
                        Destination.NS = new NetworkStream(Temp, true);
                        ForwarderEvent?.Invoke(this, ForwarderEventType.Established);
                    }
                    catch
                    {
                        Source.NS.Close();
                        Source.NS.Dispose();
                        Source = null;
                        ForwarderEvent?.Invoke(this, ForwarderEventType.ServerUnavailable);
                    }
                    if (Destination != null)
                    {
                        if (Timeout > 0)
                        {
                            Source.NS.ReadTimeout      = Timeout;
                            Destination.NS.ReadTimeout = Timeout;
                        }
                        Source.Handle      = Source.NS.BeginRead(Source.Data, 0, BUFFER, DataIn, Source);
                        Destination.Handle = Destination.NS.BeginRead(Destination.Data, 0, BUFFER, DataIn, Destination);
                    }
                }).Start();
            }
            else
            {
                throw new InvalidOperationException("Forwarder already running");
            }
        }
Ejemplo n.º 3
0
        public void Stop()
        {
            if (Destination == null)
            {
                throw new InvalidOperationException("Forwarder has not yet been started");
            }
            var Temp1 = Source;
            var Temp2 = Destination;

            Source      = null;
            Destination = null;
            if (Temp1 != null && Temp1.NS != null)
            {
                Temp1.NS.Close();
                Temp1.NS.Dispose();
            }
            if (Temp2 != null && Temp2.NS != null)
            {
                Temp2.NS.Close();
                Temp2.NS.Dispose();
            }
            ForwarderEvent?.Invoke(this, ForwarderEventType.UserClosed);
        }