示例#1
0
        public TcpSession(TcpServer Server, StreamedAsyncSocket Socket, IPEndPoint RemoteEndPoint, Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory, Action <Action> QueueUserWorkItem)
        {
            this.Server         = Server;
            this.Socket         = Socket;
            this.RemoteEndPoint = RemoteEndPoint;
            ssm              = new SessionStateMachine <StreamedVirtualTransportServerHandleResult, Unit>(ex => ex is SocketException, OnCriticalError, OnShutdownRead, OnShutdownWrite, OnWrite, OnExecute, OnStartRawRead, OnExit, QueueUserWorkItem);
            Socket.TimedOut += ssm.NotifyFailure;

            Context                = Server.ServerContext.CreateSessionContext();
            Context.Quit          += ssm.NotifyExit;
            Context.Authenticated += () =>
            {
                Socket.TimeoutSeconds = Server.SessionIdleTimeout;
                Server.NotifySessionAuthenticated(this);
            };

            var rpst = new Rc4PacketServerTransformer();
            var Pair = VirtualTransportServerFactory(Context, rpst);

            si  = Pair.Key;
            vts = Pair.Value;
            Context.SecureConnectionRequired += c =>
            {
                rpst.SetSecureContext(c);
            };
            vts.ServerEvent           += () => ssm.NotifyWrite(new Unit());
            vts.InputByteLengthReport += (CommandName, ByteLength) => Server.ServerContext.RaiseSessionLog(new SessionLogEntry {
                Token = Context.SessionTokenString, RemoteEndPoint = RemoteEndPoint, Time = DateTime.UtcNow, Type = "InBytes", Name = CommandName, Message = ByteLength.ToInvariantString()
            });
            vts.OutputByteLengthReport += (CommandName, ByteLength) => Server.ServerContext.RaiseSessionLog(new SessionLogEntry {
                Token = Context.SessionTokenString, RemoteEndPoint = RemoteEndPoint, Time = DateTime.UtcNow, Type = "OutBytes", Name = CommandName, Message = ByteLength.ToInvariantString()
            });
        }
示例#2
0
        public TcpClient(IPEndPoint RemoteEndPoint, IStreamedVirtualTransportClient VirtualTransportClient, Action <Action> QueueUserWorkItem)
        {
            this.RemoteEndPoint = RemoteEndPoint;
            Socket = new StreamedAsyncSocket(new Socket(RemoteEndPoint.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp), null, QueueUserWorkItem);
            //this.QueueUserWorkItem = QueueUserWorkItem;
            this.VirtualTransportClient          = VirtualTransportClient;
            VirtualTransportClient.ClientMethod += OnError =>
            {
                using (var h = new AutoResetEvent(false))
                {
                    var ByteArrays  = VirtualTransportClient.TakeWriteBuffer();
                    var TotalLength = ByteArrays.Sum(b => b.Length);
                    if ((WriteBuffer == null) || (TotalLength > WriteBuffer.Length))
                    {
                        WriteBuffer = new Byte[GetMinNotLessPowerOfTwo(TotalLength)];
                    }
                    var Offset = 0;
                    foreach (var b in ByteArrays)
                    {
                        Array.Copy(b, 0, WriteBuffer, Offset, b.Length);
                        Offset += b.Length;
                    }

                    Socket.SendAsync(WriteBuffer, 0, TotalLength, () => { }, OnError);
                }
            };
        }
示例#3
0
 private void CloseSocket()
 {
     if (sock != null)
     {
         try
         {
             sock.Shutdown(SocketShutdown.Both);
         }
         catch
         {
         }
         try
         {
             sock.Close();
         }
         catch
         {
         }
         try
         {
             sock.Dispose();
         }
         catch
         {
         }
         sock = null;
     }
 }
示例#4
0
        private void Button_Connect_Click(object sender, RoutedEventArgs e)
        {
            var Success = false;

            try
            {
                if (sock != null)
                {
                    throw new InvalidOperationException();
                }

                Button_Connect.IsEnabled    = false;
                Button_Listen.IsEnabled     = false;
                Button_Disconnect.IsEnabled = true;
                TextBox_IP.IsEnabled        = false;
                TextBox_Port.IsEnabled      = false;

                EndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Parse(TextBox_IP.Text), int.Parse(TextBox_Port.Text));
                sock = new StreamedAsyncSocket(new Socket(RemoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp));
                Action Completed = () => this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    Button_Send.IsEnabled = true;
                    StartReceive();
                }));
                Action <SocketError> Faulted = se => this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    if (se == SocketError.OperationAborted)
                    {
                        return;
                    }
                    MessageBox.Show(this, (new SocketException((int)se)).Message, "Error");
                    ForceDisconnect();
                }));
                sock.ConnectAsync(RemoteEndPoint, Completed, Faulted);

                Success = true;
            }
            finally
            {
                if (!Success)
                {
                    ForceDisconnect();
                }
            }
        }
示例#5
0
        private void Button_Listen_Click(object sender, RoutedEventArgs e)
        {
            var Success = false;

            try
            {
                if (sock != null)
                {
                    throw new InvalidOperationException();
                }

                Button_Connect.IsEnabled    = false;
                Button_Listen.IsEnabled     = false;
                Button_Disconnect.IsEnabled = true;
                TextBox_IP.IsEnabled        = false;
                TextBox_Port.IsEnabled      = false;

                AddressFamily AddressFamily = AddressFamily.InterNetwork;
                try
                {
                    AddressFamily = IPAddress.Parse(TextBox_IP.Text).AddressFamily;
                }
                catch
                {
                }
                sock = new StreamedAsyncSocket(new Socket(AddressFamily, SocketType.Stream, ProtocolType.Tcp));
                if (AddressFamily == AddressFamily.InterNetwork)
                {
                    sock.Bind(new IPEndPoint(IPAddress.Any, int.Parse(TextBox_Port.Text)));
                }
                else if (AddressFamily == AddressFamily.InterNetworkV6)
                {
                    sock.Bind(new IPEndPoint(IPAddress.IPv6Any, int.Parse(TextBox_Port.Text)));
                }
                else
                {
                    throw new InvalidOperationException();
                }
                sock.Listen(1);
                Action <StreamedAsyncSocket> Completed = s => this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    CloseSocket();
                    sock = s;
                    Button_Send.IsEnabled = true;
                    StartReceive();
                }));
                Action <SocketError> Faulted = se => this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    if (se == SocketError.OperationAborted)
                    {
                        return;
                    }
                    MessageBox.Show(this, (new SocketException((int)se)).Message, "Error");
                    ForceDisconnect();
                }));
                sock.AcceptAsync(Completed, Faulted);

                Success = true;
            }
            finally
            {
                if (!Success)
                {
                    ForceDisconnect();
                }
            }
        }