Пример #1
0
 protected override void OnAccept(IAsyncResult ar)
 {
     try
     {
         RemoteSocket = AcceptSocket.EndAccept(ar);
         IPAddress remoteAddress = ((IPEndPoint)RemoteSocket.RemoteEndPoint).Address;
         if (remoteAddress.Equals(BindEndPoint.Address))
         {
             SendRespoce(0x5a);
             return;
         }
         SendRespoce(0x5b);
     }
     catch
     {
         SendRespoce(0x5b);
     }
     finally
     {
         if (AcceptSocket != null)
         {
             AcceptSocket.Close();
         }
         AcceptSocket = null;
     }
 }
Пример #2
0
        public void Disconnect()
        {
            //ServerForm server = new ServerForm();

            if (AcceptSocket != null)
            {
                AcceptSocket.Close();
                AcceptSocket.Dispose();
            }
            if (ConnectSocket != null)
            {
                ConnectSocket.Close();
                ConnectSocket.Dispose();
            }

            /*
             * ServerForm server = new ServerForm();
             *
             * if (server.AcceptSocket != null)
             * {
             *  server.AcceptSocket.Close();
             *  server.AcceptSocket.Dispose();
             * }
             * if (server.ConnectSocket != null)
             * {
             *  server.ConnectSocket.Close();
             *  server.ConnectSocket.Dispose();
             * }
             */
        }
Пример #3
0
        protected override void OnCompleted(SocketAsyncEventArgs e)
        {
            var completionSource = Interlocked.Exchange(ref _completionSource, null);

            try
            {
                if (completionSource == null)
                {
                    return;
                }
                else if (SocketError != SocketError.Success)
                {
                    completionSource.TrySetException(new SocketException((int)SocketError));
                }
                else if (_cancelled)
                {
                    AcceptSocket.Dispose();
                }
                else // Accept
                {
                    completionSource.TrySetResult(new NativeSocket(AcceptSocket));
                }
            }
            finally
            {
                CheckIn();
                base.OnCompleted(e);
            }
        }
    public AcceptSocket GetAcceptSocket(int id)
    {
        AcceptSocket socket = null;

        this._clients.TryGetValue(id, out socket);
        return(socket);
    }
Пример #5
0
 protected void Dispose(bool Success)
 {
     if (AcceptSocket != null)
     {
         AcceptSocket.Close();
     }
     Signaler(Success, RemoteConnection);
 }
Пример #6
0
 ///<summary>Closes the listening socket if present, and signals the parent object that SOCKS negotiation is complete.</summary>
 ///<param name="Success">Indicates whether the SOCKS negotiation was successful or not.</param>
 protected async Task Dispose(bool Success)
 {
     if (AcceptSocket != null)
     {
         AcceptSocket.Dispose();
     }
     Signaler(Success, RemoteConnection);
 }
Пример #7
0
 private void Dispose(bool disposing)
 {
     AcceptSocket?.Close();
     if (disposing)
     {
         GC.SuppressFinalize(this);
     }
 }
    private void OnAccepted(AcceptSocket client)
    {
        lock (_clients_lock) {
            _clients.Add(this._id++, client);
        }
        ServerSocketAcceptedEventArgs e = new ServerSocketAcceptedEventArgs(this._clients.Count, client);

        this.OnAccepted(e);
    }
Пример #9
0
        public EndPoint Bind(IPEndPoint endpoint, ChannelWriter <ConnectionContext> connectionSource)
        {
            var context = AcceptSocket.Bind(endpoint, connectionSource, _memoryPool, _options, _scheduler);

            _acceptSocketsPerEndPoint[(IPEndPoint)context.EndPoint] = context;
            _scheduler.ScheduleAsyncAddAndAcceptPoll(context.Socket, context);

            return(context.EndPoint);
        }
Пример #10
0
 public DataReadInfo(DataReadInfoType type, AcceptSocket client, NetworkStream ns, int bufferSize, int size)
 {
     this.Type           = type;
     this.AcceptSocket   = client;
     this.NetworkStream  = ns;
     this.Buffer         = new byte[bufferSize];
     this.Size           = size;
     this.Over           = size;
     this.ResponseStream = new MemoryStream();
 }
Пример #11
0
 ///<summary>Called when there's an incoming connection in the AcceptSocket queue.</summary>
 ///<param name="ar">The result of the asynchronous operation.</param>
 protected override void OnAccept(IAsyncResult ar)
 {
     try {
         RemoteConnection = AcceptSocket.EndAccept(ar);
         AcceptSocket.Close();
         AcceptSocket = null;
         Dispose(0);
     } catch {
         Dispose(1);
     }
 }
    public void Start()
    {
        if (this._running == false)
        {
            this._running = true;
            try {
                this._tcpListener = new TcpListener(IPAddress.Any, this._port);
                this._tcpListener.Start();
            } catch (Exception ex) {
                this._running = false;
                this.OnError(ex);
                return;
            }
            this._tcpListenerThread = new Thread(delegate() {
                while (this._running)
                {
                    try {
                        TcpClient tcpClient = this._tcpListener.AcceptTcpClient();
                        new Thread(delegate() {
                            try {
                                AcceptSocket acceptSocket = new AcceptSocket(this, tcpClient, this._id);
                                this.OnAccepted(acceptSocket);
                            } catch (Exception ex) {
                                this.OnError(ex);
                            }
                        }).Start();
                    } catch (Exception ex) {
                        this.OnError(ex);
                    }
                }

                int[] keys = new int[this._clients.Count];
                try {
                    this._clients.Keys.CopyTo(keys, 0);
                } catch {
                    lock (this._clients_lock) {
                        keys = new int[this._clients.Count];
                        this._clients.Keys.CopyTo(keys, 0);
                    }
                }
                foreach (int key in keys)
                {
                    AcceptSocket client = null;
                    if (this._clients.TryGetValue(key, out client))
                    {
                        client.Close();
                    }
                }
                this._clients.Clear();
                this._stopWait.Set();
            });
            this._tcpListenerThread.Start();
        }
    }
Пример #13
0
 ///<summary>Called when the AcceptSocket should start accepting incoming connections.</summary>
 ///<param name="ar">The result of the asynchronous operation.</param>
 protected async Task OnStartAccept()
 {
     try
     {
         await AcceptSocket.AcceptAsync(OnAccept);
     }
     catch (Exception ex)
     {
         Console.WriteLine("[WARN] " + ex.Message + "\r\n" + ex.StackTrace);
         await OnAcceptError(ex);
     }
 }
Пример #14
0
 ///<summary>Called when there's an incoming connection in the AcceptSocket queue.</summary>
 ///<param name="ar">The result of the asynchronous operation.</param>
 protected override async Task OnAccept(SocketConnection socket)
 {
     AcceptSocket.Dispose();
     AcceptSocket = null;
     if (RemoteBindIP.Equals(((IPEndPoint)RemoteConnection.RemoteEndPoint).Address))
     {
         await Dispose(ReplyCode.RequestGranted);
     }
     else
     {
         await Dispose(ReplyCode.RequestRejected1);
     }
 }
Пример #15
0
 protected void OnStartAccept(IAsyncResult ar)
 {
     try {
         if (Connection.EndSend(ar) <= 0)
         {
             Dispose(false);
         }
         else
         {
             AcceptSocket.BeginAccept(new AsyncCallback(this.OnAccept), AcceptSocket);
         }
     } catch {
         Dispose(false);
     }
 }
Пример #16
0
 ///<summary>Called when there's an incoming connection in the AcceptSocket queue.</summary>
 ///<param name="ar">The result of the asynchronous operation.</param>
 protected override void OnAccept(IAsyncResult ar)
 {
     try {
         RemoteConnection = AcceptSocket.EndAccept(ar);
         AcceptSocket.Close();
         AcceptSocket = null;
         if (RemoteBindIP.Equals(((IPEndPoint)RemoteConnection.RemoteEndPoint).Address))
         {
             Dispose(90);
         }
         else
         {
             Dispose(91);
         }
     } catch {
         Dispose(91);
     }
 }
Пример #17
0
    public void Stop()
    {
        if (this._tcpListener != null)
        {
            this._tcpListener.Stop();
        }
        if (this._running == true)
        {
            this._beginAcceptTcpClient.AsyncWaitHandle.Close();

            this._running = false;

            int[] keys = new int[this._clients.Count];
            try {
                this._clients.Keys.CopyTo(keys, 0);
            } catch {
                lock (this._clients_lock) {
                    keys = new int[this._clients.Count];
                    this._clients.Keys.CopyTo(keys, 0);
                }
            }
            foreach (int key in keys)
            {
                AcceptSocket client = null;
                if (this._clients.TryGetValue(key, out client))
                {
                    client.Close();
                }
            }
            if (this._receiveWQ != null)
            {
                this._receiveWQ.Dispose();
            }
            if (this._receiveSyncWQ != null)
            {
                this._receiveSyncWQ.Dispose();
            }
            if (this._writeWQ != null)
            {
                this._writeWQ.Dispose();
            }
            this._clients.Clear();
        }
    }
Пример #18
0
    private void HandleTcpClientAccepted(IAsyncResult ar)
    {
        if (this._running)
        {
            try {
                TcpClient tcpClient = this._tcpListener.EndAcceptTcpClient(ar);

                try {
                    AcceptSocket acceptSocket = new AcceptSocket(this, tcpClient, this._id);
                    this.OnAccepted(acceptSocket);
                } catch (Exception ex) {
                    this.OnError(ex);
                }

                this._beginAcceptTcpClient = this._tcpListener.BeginAcceptTcpClient(HandleTcpClientAccepted, null);
            } catch (Exception ex) {
                this.OnError(ex);
            }
        }
    }
 public void Write(SocketMessager messager)
 {
     int[] keys = new int[this._clients.Count];
     try {
         this._clients.Keys.CopyTo(keys, 0);
     } catch {
         lock (this._clients_lock) {
             keys = new int[this._clients.Count];
             this._clients.Keys.CopyTo(keys, 0);
         }
     }
     foreach (int key in keys)
     {
         AcceptSocket client = null;
         if (this._clients.TryGetValue(key, out client))
         {
             client.Write(messager);
         }
     }
 }
Пример #20
0
 ///<summary>Called when there's an incoming connection in the AcceptSocket queue.</summary>
 ///<param name="ar">The result of the asynchronous operation.</param>
 protected override async Task OnAccept(SocketConnection ar)
 {
     AcceptSocket.Dispose();
     AcceptSocket = null;
     await Dispose(ReplyCode.Ok);
 }
Пример #21
0
 public CodeBuild(ClientInfo client, AcceptSocket socket)
 {
     _client = client;
     _socket = socket;
 }
 internal void AccessDenied(AcceptSocket client)
 {
     client.Write(SocketMessager.SYS_ACCESS_DENIED, delegate(object sender2, ServerSocketReceiveEventArgs e2) {
     }, TimeSpan.FromSeconds(3));
     client.Close();
 }
 public ServerSocketReceiveEventArgs(int receives, SocketMessager messager, AcceptSocket acceptSocket)
 {
     this._receives     = receives;
     this._messager     = messager;
     this._acceptSocket = acceptSocket;
 }
 public ServerSocketErrorEventArgs(int errors, Exception exception, AcceptSocket acceptSocket)
 {
     this._errors       = errors;
     this._exception    = exception;
     this._acceptSocket = acceptSocket;
 }
 public ServerSocketAcceptedEventArgs(int accepts, AcceptSocket acceptSocket)
 {
     this._accepts      = accepts;
     this._acceptSocket = acceptSocket;
 }
    internal void OnClosed(AcceptSocket client)
    {
        ServerSocketClosedEventArgs e = new ServerSocketClosedEventArgs(this._clients.Count, client.Id);

        this.OnClosed(e);
    }
 internal void CloseClient(AcceptSocket client)
 {
     this._clients.Remove(client.Id);
 }