Example #1
0
        public static void StartReadingRemote(
                TcpProxy proxy,
                TcpClient remoteClient,
                TcpClient localClient)
        {
            try
            {
                NetworkStream stream = null;
                try
                {
                    stream = remoteClient.GetStream();

                    ReadStreamState readState = new ReadStreamState()
                    {
                        Callback = _readStreamCallback,
                        ReadStreamOwner = remoteClient,
                        ClientToReport = localClient,
                        Buffer = new byte[remoteClient.ReceiveBufferSize],
                        Offset = 0,
                        ReadStream = stream,
                        Proxy = proxy
                    };

                    // Begin write. Resume reading will be done in the callback.
                    stream.BeginRead(
                            readState.Buffer,
                            readState.Offset,
                            readState.Buffer.Length,
                            readState.Callback,
                            readState);
                    //--
                }
                catch (IOException ioEx)
                {
                    if (remoteClient.Connected)
                    {
                        Console.WriteLine("IO EXCEPTION: StartReadingRemote head: {0}", ioEx.Message);
                        Console.ReadKey();
                    }
                    else
                    {
                        // Connect, after connected, try to send again.
                        ConnectState connState = new ConnectState()
                        {
                            Callback = _connectCallback,
                            Client = new TcpClient()
                        };

                        connState.ActionAfterConnect = delegate()
                        {
                            StartReadingRemote(proxy, connState.Client, localClient);
                        };

                        // FIXME: Depends if writing is being done on proxy client or proxy server ...
                        connState.Client.BeginConnect(
                                proxy._target.Address,
                                proxy._target.Port,
                                connState.Callback,
                                connState);
                        //---
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION: StartReadingRemote head: {0}", e.Message);
                    Console.ReadKey();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: StartReadingRemote tail: {0}", e.Message);
                Console.ReadKey();
            }
        }
Example #2
0
        private static void StartWritingRemote(
                TcpProxy proxy, 
                TcpClient clientWriter, 
                TcpClient clientToReport, 
                byte[] data)
        {
            try
            {
                NetworkStream clientStream = null;
                try
                {
                    // Can raise InvalidOperationException if tcpclient is disconnected
                    clientStream = clientWriter.GetStream();

                    // TODO: This might raise an exception, put in a try-catch
                    WriteStreamState writeState = new WriteStreamState()
                    {
                        Callback = _writeStreamCallback,
                        WriteStreamOwner = clientWriter,
                        WriteStream = clientStream,
                        Offset = 0,
                        Buffer = data,
                        ClientToReport = clientToReport,
                        Proxy = proxy
                    };

                    // Begin write. Resume reading will be done in the callback.
                    // Can raise IOException if tcpclient is disconnected
                    clientStream.BeginWrite(
                            writeState.Buffer,
                            writeState.Offset,
                            writeState.Buffer.Length,
                            writeState.Callback,
                            writeState);
                    //---
                }
                catch (Exception e)
                {
                    if (clientWriter.Connected)
                    {
                        Console.WriteLine("EXCEPTION: StartWritingRemote head: {0}", e.Message);
                        Console.ReadKey();
                    }
                    else
                    {
                        // Connect new TcpClient, drop the current one, after connected, try to send again.
                        ConnectState connState = new ConnectState()
                        {
                            Callback = _connectCallback,
                            Client = new TcpClient()
                        };

                        connState.ActionAfterConnect = delegate()
                        {
                            StartWritingRemote(proxy, connState.Client, clientToReport, data);
                        };

                        // FIXME: Depends if writing is being done on proxy client or proxy server ...
                        connState.Client.BeginConnect(
                                proxy._target.Address,
                                proxy._target.Port,
                                connState.Callback,
                                connState);
                        //---
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: StartWritingRemote tail: {0}", e.Message);
                Console.ReadKey();
            }
        }