Exemplo n.º 1
0
        //void OnSocketReceive(SocketAsyncEventArgs e, MemoryStream ms = null) {
        //	if (e.SocketError == SocketError.Success) {
        //		// 检查远程主机是否关闭连接
        //		if (e.BytesTransferred > 0) {
        //			var s = (Socket)e.UserToken;

        //			if (s.Available == 0) {
        //				byte[] data = new byte[e.BytesTransferred];
        //				Array.Copy(e.Buffer, e.Offset, data, 0, data.Length);//从e.Buffer块中复制数据出来,保证它可重用

        //				Exception ex = null;
        //				object result = null;
        //				bool isended = false;
        //				try {
        //					if (ms == null) {
        //						if (data[0] == '+') {
        //							for (var a = 1; a < data.Length; a++)
        //								if (data[a] == '\r' && a < data.Length - 1 && data[a + 1] == '\n') {
        //									result = a == 1 ? "" : _io.Encoding.GetString(data, 1, a - 1);
        //									isended = true;
        //									break;
        //								}
        //						}
        //						if (data[0] == '-') {
        //							for (var a = 1; a < data.Length; a++)
        //								if (data[a] == '\r' && a < data.Length - 1 && data[a + 1] == '\n')
        //									throw new CSRedis.RedisException(a == 1 ? "" : _io.Encoding.GetString(data, 1, a - 1));
        //						}
        //						if (data[0] == ':') {
        //							for (var a = 2; a < data.Length; a++)
        //								if (data[a] == '\r' && a < data.Length - 1 && data[a + 1] == '\n') {
        //									result = long.Parse(_io.Encoding.GetString(data, 1, a - 1));
        //									isended = true;
        //									break;
        //								}
        //						}
        //						if (data[0] == '$') {
        //							long startIndex = 0;
        //							long len = -999;
        //							for (var a = 2; a < data.Length; a++)
        //								if (data[a] == '\r' && a < data.Length - 1 && data[a + 1] == '\n') {
        //									if (len > 0) {
        //										byte[] dest = new byte[len];
        //										Array.Copy(data, startIndex, dest, 0, len);
        //										result = dest;
        //										break;
        //									}
        //									len = long.Parse(_io.Encoding.GetString(data, 1, a - 1));
        //									startIndex = a + 1;
        //									if (len < 0) {
        //										result = null;
        //										isended = true;
        //										break;
        //									}
        //									if (len == 0) {
        //										result = "";
        //										isended = true;
        //										break;
        //									}
        //								}
        //						}
        //						//if (data[0] == '*') {
        //						//	long startIndex = 0;
        //						//	long len = -999;
        //						//	for (var a = 2; a < data.Length; a++)
        //						//		if (data[a] == '\r' && a < data.Length - 1 && data[a + 1] == '\n') {
        //						//			if (len > 0) {
        //						//				byte[] dest = new byte[len];
        //						//				Array.Copy(data, startIndex, dest, 0, len);
        //						//				result = dest;
        //						//				break;
        //						//			}
        //						//			len = long.Parse(_io.Encoding.GetString(data, 1, a - 1));
        //						//			startIndex = a + 1;
        //						//			if (len < 0) {
        //						//				result = null;
        //						//				isended = true;
        //						//				break;
        //						//			}
        //						//			if (len == 0) {
        //						//				result = "";
        //						//				isended = true;
        //						//				break;
        //						//			}
        //						//		}
        //						//}
        //					}
        //				} catch (Exception ex2) {
        //					ex = ex2;
        //				} finally {

        //				}
        //			}

        //			//ms.Write(data, 0, data.Length);


        //			//为接收下一段数据,投递接收请求,这个函数有可能同步完成,这时返回false,并且不会引发SocketAsyncEventArgs.Completed事件
        //			if (!s.ReceiveAsync(e)) {
        //				//同步接收时处理接收完成事件
        //				OnSocketReceive(e, ms);
        //			}
        //		}
        //	}
        //}

        public void Dispose()
        {
            while (_asyncReadQueue.TryDequeue(out var token))
            {
                try { token.SetException(new Exception("Error: Disposing...")); } catch { }
            }

            while (_asyncWriteQueue.TryDequeue(out var token))
            {
                try { token.SetException(new Exception("Error: Disposing...")); } catch { }
            }

            this.SetConnectionTaskSourceResult(false, null, true);

            _asyncTransferPool.Dispose();
            _asyncConnectArgs.Dispose();
        }
Exemplo n.º 2
0
        public void Dispose()
        {
            while (_asyncReadQueue.TryDequeue(out var token))
            {
                try { token.SetException(new Exception("Error: Disposing...")); } catch { }
            }

            while (_asyncWriteQueue.TryDequeue(out var token))
            {
                try { token.SetException(new Exception("Error: Disposing...")); } catch { }
            }

            _connectionTaskSource.TrySetCanceled();

            _asyncTransferPool.Dispose();
            _asyncConnectArgs.Dispose();
        }
Exemplo n.º 3
0
 public void Dispose()
 {
     _asyncTransferPool.Dispose();
     _asyncConnectArgs.Dispose();
 }