Révision | 92 (tree) |
---|---|
l'heure | 2018-04-07 17:49:11 |
Auteur | bananajinn |
reloadコマンドを受けた時に ObjectDisposedException で強制終了してしまうのを回避
@@ -95,32 +95,36 @@ | ||
95 | 95 | |
96 | 96 | void Accept(IAsyncResult ar) |
97 | 97 | { |
98 | - var tcpClient = tcpListener.EndAcceptTcpClient(ar); | |
99 | - Task.Factory.StartNew( | |
100 | - () => { | |
101 | - try { | |
102 | - var clientAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString(); | |
103 | - log.InfoFormat("Connect from {0}.", clientAddress); | |
104 | - tcpClient.NoDelay = true; | |
105 | - tcpClient.ReceiveTimeout = 30000; | |
106 | - var stream = tcpClient.GetStream(); | |
107 | - while(tcpClient.Connected){ | |
108 | - var commandLine = StreamUtil.ReadLine(stream); | |
109 | - if(string.IsNullOrEmpty(commandLine)){ | |
110 | - break; | |
98 | + try { | |
99 | + var tcpClient = tcpListener.EndAcceptTcpClient(ar); | |
100 | + Task.Factory.StartNew( | |
101 | + () => { | |
102 | + try { | |
103 | + var clientAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString(); | |
104 | + log.InfoFormat("Connect from {0}.", clientAddress); | |
105 | + tcpClient.NoDelay = true; | |
106 | + tcpClient.ReceiveTimeout = 30000; | |
107 | + var stream = tcpClient.GetStream(); | |
108 | + while(tcpClient.Connected){ | |
109 | + var commandLine = StreamUtil.ReadLine(stream); | |
110 | + if(string.IsNullOrEmpty(commandLine)){ | |
111 | + break; | |
112 | + } | |
113 | + ExecuteCommand(commandLine, stream, clientAddress); | |
111 | 114 | } |
112 | - ExecuteCommand(commandLine, stream, clientAddress); | |
113 | 115 | } |
114 | - } | |
115 | - catch(Exception ex){ | |
116 | - log.Error("error", ex); | |
117 | - var data = Encoding.UTF8.GetBytes(string.Concat("[ERROR] ", ex.Message)); | |
118 | - tcpClient.GetStream().Write(data, 0, data.Length); | |
119 | - } | |
120 | - finally { | |
121 | - tcpClient.Close(); | |
122 | - } | |
123 | - }); | |
116 | + catch(Exception ex){ | |
117 | + log.Error("error", ex); | |
118 | + var data = Encoding.UTF8.GetBytes(string.Concat("[ERROR] ", ex.Message)); | |
119 | + tcpClient.GetStream().Write(data, 0, data.Length); | |
120 | + } | |
121 | + finally { | |
122 | + tcpClient.Close(); | |
123 | + } | |
124 | + }); | |
125 | + } catch(ObjectDisposedException ex){ | |
126 | + log.Debug("Canceled", ex); | |
127 | + } | |
124 | 128 | if(tcpListener.Server.IsBound) |
125 | 129 | tcpListener.BeginAcceptTcpClient(Accept, tcpListener); |
126 | 130 | } |