/// <summary> /// 多路接收 /// </summary> /// <param name="address"></param> public void Rsp(string address) { while (true) { TCPUserToken tCPUserToken = null; ResponseSocket responseSocket = null; if (token_Pool.Pop(out tCPUserToken)) { responseSocket = tCPUserToken.Socket; responseSocket.Connect(address); } else { responseSocket = new ResponseSocket(); responseSocket.Connect(address); tCPUserToken = new TCPUserToken(token_Pool) { Socket = responseSocket, Address = address }; } byte[] buf = responseSocket.ReceiveFrameBytes(); tCPUserToken.Data = buf; queue.Add(tCPUserToken); } }
public bool Pop(out TCPUserToken token) { if (stack.TryPop(out token)) { Interlocked.Increment(ref token.Use); token.AcessTime = DateTime.Now; return(true); } lastTime = DateTime.Now; return(false); }
public void Start(string address) { using (ResponseSocket responseSocket = new ResponseSocket("@tcp://" + address)) { while (true) { byte[] buf = responseSocket.ReceiveFrameBytes(); TCPUserToken tCPUserToken = new TCPUserToken { Data = buf, Socket = responseSocket }; queue.Add(tCPUserToken); } } }
/// <summary> /// 监测无用的 /// </summary> private void Check() { Task.Factory.StartNew(() => { Thread.Sleep(WaitTime); if ((DateTime.Now - lastTime).TotalMilliseconds > WaitTime) { int count = stack.Count; List <TCPUserToken> lst = new List <TCPUserToken>(count); TCPUserToken token = null; for (int i = 0; i < count; i++) { if (stack.TryPop(out token)) { if ((DateTime.Now - token.AcessTime).TotalMilliseconds < WaitTime) { token.Socket.Close(); token.Socket.Dispose(); } else { int num = stack.Count > lst.Count ? stack.Count - lst.Count : 0; for (int j = 0; j < num; j++) { if (stack.TryPop(out token)) { token.Socket.Close(); token.Socket.Dispose(); } else { break; } } // break; } } else { break; } } } }); }
public void Push(TCPUserToken token) { Interlocked.Decrement(ref token.Use); stack.Push(token); }