Пример #1
0
        protected virtual void ProcessAccepted(Socket socket)
        {
            socket.Blocking = false;
            var connection = new ProxyConnection(socket, Context, Connections);

            Connections.Register(connection);
        }
Пример #2
0
 public void Close(ProxyConnection connection)
 {
     if (ConnectionIndecies.TryRemove(connection, out var id))
     {
         IndexConnections.TryRemove(id, out connection);
         OnConnectionRemoved?.Invoke(this, connection);
         DisposeConnection(connection);
     }
 }
Пример #3
0
 public ProxyConnection(Socket socket, ProxyConnection reverseConnection)
 {
     Socket            = socket;
     Context           = reverseConnection.Context;
     ReverseConnection = reverseConnection;
     IsClient          = false;
     DataHandler       = new ProxyHandler(this);
     AssignedTabled    = reverseConnection.AssignedTabled;
 }
Пример #4
0
 public void RemoveConnection(ProxyConnection connection)
 {
     lock (Connections)
     {
         Connections.Remove(connection);
         if (Connections.Count == 0)
         {
             Stop();
         }
     }
 }
Пример #5
0
 public void AddConnection(ProxyConnection connection)
 {
     lock (Connections)
     {
         Connections.Add(connection);
         if (Connections.Count == 1)
         {
             Start();
         }
     }
 }
Пример #6
0
        public ProxyConnection(Socket socket, ProxyConnection reverseConnection)
        {
            ActivityTime = AcceptedTime = DateTime.Now;

            Socket            = socket;
            Context           = reverseConnection.Context;
            ReverseConnection = reverseConnection;
            IsClient          = false;
            DataHandler       = new ProxyHandler(this);
            AssignedTabled    = reverseConnection.AssignedTabled;
        }
Пример #7
0
        public long Register(ProxyConnection connection)
        {
            var id = Interlocked.Increment(ref NextConnectionIndex);

            if (!ConnectionIndecies.TryAdd(connection, id) || !IndexConnections.TryAdd(id, connection))
            {
                throw new Exception("Connection register failed");
            }
            OnConnectionRegistered?.Invoke(this, connection);
            return(id);
        }
Пример #8
0
 public void AddConnection(ProxyConnection connection)
 {
     lock (Connections)
     {
         Connections.Add(connection);
         if (Connections.Count == 1)
         {
             Start();
         }
     }
     connection.Socket.ReceiveTimeout = ReceiveTimeout;
 }
Пример #9
0
 private void DisposeConnection(ProxyConnection connection)
 {
     try
     {
         connection.Socket.Close();
         connection.Socket.Dispose();
     }
     catch
     {
     }
     if (connection is IDisposable disposable)
     {
         using (disposable) { }
     }
 }
Пример #10
0
 private void ConnectionTable_OnConnectionRegistered(ConnectionTable connectionTable, ProxyConnection connection)
 {
     lock (ConnectionLock)
     {
         if (IncompleteReceivers.Count == 0)
         {
             IncompleteReceivers.Push(new SocketSelectReceiverThread(SelectTimeout, ReceiveBufferSize));
         }
         SocketSelectReceiverThread receiver = IncompleteReceivers.Peek();
         if (receiver.Count + 1 >= ConnectionsPerThread)
         {
             IncompleteReceivers.Pop();
         }
         ConnectionReceiver[connection] = receiver;
         receiver.AddConnection(connection);
     }
 }
Пример #11
0
 private void ConnectionTable_OnConnectionRemoved(ConnectionTable connectionTable, ProxyConnection connection)
 {
     lock (ConnectionLock)
     {
         if (ConnectionReceiver.TryGetValue(connection, out var receiver))
         {
             if (receiver.Count == ConnectionsPerThread)
             {
                 IncompleteReceivers.Push(receiver);
             }
             receiver.RemoveConnection(connection);
             ConnectionReceiver.Remove(connection);
         }
     }
 }
Пример #12
0
 public long GetId(ProxyConnection connection)
 {
     return(ConnectionIndecies.GetValueOrDefault(connection, -1));
 }