Close services while closing listener.

Now it will close all existing TCP relay connections
while reloading the listener.

Signed-off-by: noisyfox <timemanager.rick@gmail.com>
This commit is contained in:
noisyfox
2016-08-26 21:08:04 +10:00
parent 65d72fdfed
commit ed54b249db
6 changed files with 32 additions and 10 deletions
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
@@ -10,9 +11,18 @@ namespace Shadowsocks.Controller
{
public class Listener
{
public interface Service
public interface IService
{
bool Handle(byte[] firstPacket, int length, Socket socket, object state);
void Stop();
}
public abstract class Service : IService
{
public abstract bool Handle(byte[] firstPacket, int length, Socket socket, object state);
public virtual void Stop() { }
}
public class UDPState
@@ -25,9 +35,9 @@ namespace Shadowsocks.Controller
bool _shareOverLAN;
Socket _tcpSocket;
Socket _udpSocket;
IList<Service> _services;
List<IService> _services;
public Listener(IList<Service> services)
public Listener(List<IService> services)
{
this._services = services;
}
@@ -97,6 +107,8 @@ namespace Shadowsocks.Controller
_udpSocket.Close();
_udpSocket = null;
}
_services.ForEach(s=>s.Stop());
}
public void RecvFromCallback(IAsyncResult ar)
@@ -105,7 +117,7 @@ namespace Shadowsocks.Controller
try
{
int bytesRead = _udpSocket.EndReceiveFrom(ar, ref state.remoteEndPoint);
foreach (Service service in _services)
foreach (IService service in _services)
{
if (service.Handle(state.buffer, bytesRead, _udpSocket, state))
{
@@ -187,7 +199,7 @@ namespace Shadowsocks.Controller
try
{
int bytesRead = conn.EndReceive(ar);
foreach (Service service in _services)
foreach (IService service in _services)
{
if (service.Handle(buf, bytesRead, conn, null))
{
@@ -35,7 +35,7 @@ namespace Shadowsocks.Controller
this._config = config;
}
public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
public override bool Handle(byte[] firstPacket, int length, Socket socket, object state)
{
if (socket.ProtocolType != ProtocolType.Tcp)
{
@@ -14,7 +14,7 @@ namespace Shadowsocks.Controller
this._targetPort = targetPort;
}
public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
public override bool Handle(byte[] firstPacket, int length, Socket socket, object state)
{
if (socket.ProtocolType != ProtocolType.Tcp)
{
@@ -29,7 +29,7 @@ namespace Shadowsocks.Controller
_lastSweepTime = DateTime.Now;
}
public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
public override bool Handle(byte[] firstPacket, int length, Socket socket, object state)
{
if (socket.ProtocolType != ProtocolType.Tcp
|| (length < 2 || firstPacket[0] != 5))
@@ -62,6 +62,16 @@ namespace Shadowsocks.Controller
return true;
}
public override void Stop()
{
List<TCPHandler> handlersToClose = new List<TCPHandler>();
lock (Handlers)
{
handlersToClose.AddRange(Handlers);
}
handlersToClose.ForEach(h=>h.Close());
}
public void UpdateInboundCounter(Server server, long n)
{
_controller.UpdateInboundCounter(server, n);
@@ -24,7 +24,7 @@ namespace Shadowsocks.Controller
this._cache = new LRUCache<IPEndPoint, UDPHandler>(512); // todo: choose a smart number
}
public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
public override bool Handle(byte[] firstPacket, int length, Socket socket, object state)
{
if (socket.ProtocolType != ProtocolType.Udp)
{
@@ -429,7 +429,7 @@ namespace Shadowsocks.Controller
TCPRelay tcpRelay = new TCPRelay(this, _config);
UDPRelay udpRelay = new UDPRelay(this);
List<Listener.Service> services = new List<Listener.Service>();
List<Listener.IService> services = new List<Listener.IService>();
services.Add(tcpRelay);
services.Add(udpRelay);
services.Add(_pacServer);