public bool Set(byte[] buff, int offset, int size) { using (LockWait wait = new LockWait(ref lockParam)) { return(packetQueue.Set(buff, offset, size)); } }
public void Clear() { using (LockWait wait = new LockWait(ref lockParam)) { packetQueue.Clear(); } }
public List <Packet> Get() { using (LockWait wait = new LockWait(ref lockParam)) { return(packetQueue.Get()); } }
/// <summary> /// 获取第一个元素 /// </summary> /// <returns></returns> public T Peek() { using (LockWait wait = new LockWait(ref lockParam)) { return(pool.QueuePeek()); } }
public void AddToken(NetConnectionToken ncToken) { using (LockWait lwait = new LockWait(ref lockParam)) { list.AddLast(ncToken); } }
public bool ConnectSync(int port, string ip) { if (connected == false || sock == null || sock.Connected == false) { Close(); } connected = false; channelType = ClientChannelType.Sync; int retry = 3; using (LockWait wait = new LockWait(ref _lockParam)) { CreateTcpSocket(port, ip); } while (retry > 0) { try { --retry; sock.Connect(endPoint); connected = true; return(true); } catch (Exception) { Close(); if (retry <= 0) { throw; } Thread.Sleep(1000); } } return(false); }
public void Clear() { using (LockWait wait = new LockWait(ref _lockParam)) { _freeOffsets.Clear(); } }
public void Set(T item) { using (LockWait lwait = new LockWait(ref lockParam)) { collection.Enqueue(item); } }
public void Clear() { using (LockWait lwait = new LockWait(ref lockParam)) { collection.Clear(); } }
/// <summary> /// 遍历 /// </summary> /// <param name="action"></param> public void ForEach(Action <T> action) { using (LockWait wait = new LockWait(ref lockParam)) { pool.ForEach(action); } }
public bool Start(int port, string ip = "0.0.0.0") { Stop(); InitArgs(); int errCount = 0; reStart: try { using (LockWait wait = new LockWait(ref _lockParam)) { CreateTcpSocket(port, ip); sock.Bind(endPoint); sock.Listen(_maxConCount); _isRunning = true; } StartAcceptAsync(null); return(true); } catch (Exception) { SafeClose(); errCount++; if (errCount >= 3) { throw; } else { Thread.Sleep(1000); goto reStart; } } }
/// <summary> /// 异步等待连接返回结果 /// </summary> /// <param name="port"></param> /// <param name="ip"></param> /// <returns></returns> public bool ConnectTo(int port, string ip) { try { if (!IsClose()) { Close(); } isConnected = false; channelProviderState = ChannelProviderType.AsyncWait; using (LockWait lwait = new LockWait(ref lParam)) { CreatedConnectToBindArgs(port, ip); } mReset.WaitOne(connectioTimeout); isConnected = socket.Connected; return(isConnected); } catch (Exception ex) { Close(); throw ex; } }
public void Stop() { try { using (LockWait lwait = new LockWait(ref lParam)) { DisposePoolToken(); if (numberOfConnections > 0) { if (maxNumberAcceptedClients != null) { maxNumberAcceptedClients.Release(numberOfConnections); } numberOfConnections = 0; } SafeClose(); isStoped = true; } } catch (Exception ex) { } }
public void Clear() { using (LockWait lwait = new LockWait(ref lockParam)) { freeBufferIndexPool.Clear(); } }
public NetConnectionToken GetTokenBySocketToken(SocketToken sToken) { using (LockWait lwait = new LockWait(ref lockParam)) { return(list.Where(x => x.Token.CompareTo(sToken) == 0).FirstOrDefault()); } }
/// <summary> /// 是否包含 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool Contains(T t) { using (LockWait wait = new LockWait(ref lockParam)) { return(pool.Contains(t)); } }
public void ConnectAsync(int port, string ip) { try { if (connected == false || sock == null || sock.Connected == false) { Close(); } connected = false; channelType = ClientChannelType.Async; using (LockWait wait = new LockWait(ref _lockParam)) { CreateTcpSocket(port, ip); SocketAsyncEventArgs conArg = new SocketAsyncEventArgs { RemoteEndPoint = endPoint, UserToken = new SocketToken(-1) { sock = sock } }; conArg.AcceptSocket = sock; conArg.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted); if (!sock.ConnectAsync(conArg)) { ConnectCallback(conArg); } } } catch (Exception) { Close(); throw; } }
public NetConnectionToken GetTokenById(int Id) { using (LockWait lwait = new LockWait(ref lockParam)) { return(list.Where(x => x.Token.tokenId == Id).FirstOrDefault()); } }
private void ProcessReceive(SocketAsyncEventArgs e) { using (var lwait = new LockWait(ref _lParam)) { var sToken = new SocketToken() { TokenSocket = e.UserToken as Socket, TokenIpEndPoint = (IPEndPoint)e.RemoteEndPoint }; try { if (e.SocketError != SocketError.Success || e.BytesTransferred == 0) { return; } //初次连接心跳 if (IsServerResponse(e) == false) { return; } //缓冲区偏移量返回 ReceiveOffsetHandler?.Invoke(sToken, e.Buffer, e.Offset, e.BytesTransferred); //截取后返回 if (ReceiveCallbackHandler != null) { if (e.Offset == 0 && e.BytesTransferred == e.Buffer.Length) { ReceiveCallbackHandler(sToken, e.Buffer); } else { var realbytes = new byte[e.BytesTransferred]; Buffer.BlockCopy(e.Buffer, e.Offset, realbytes, 0, e.BytesTransferred); ReceiveCallbackHandler(sToken, realbytes); } } } catch (Exception ex) { throw ex; } finally { if (e.SocketError == SocketError.Success) { //继续下一个接收 if (!sToken.TokenSocket.ReceiveFromAsync(e)) { ProcessReceive(e); } } } } }
public void FreeBuffer(SocketAsyncEventArgs arg) { using (LockWait wait = new LockWait(ref _lockParam)) { this._freeOffsets.Enqueue(arg.Offset); arg.SetBuffer(null, 0, 0); } }
/// <summary> /// 是否包含 /// </summary> /// <param name="p"></param> /// <returns></returns> public bool Contains(Predicate <T> p) { using (LockWait wait = new LockWait(ref lockParam)) { var list = pool.FindAll(p); return(list != null && list.Count > 0); } }
public void FreeBuffer() { using (LockWait wait = new LockWait(ref _lockParam)) { _curOffset = 0; _freeOffsets.Clear(); } }
/// <summary> /// 释放缓冲区 /// </summary> /// <param name="args"></param> public void FreeBuffer(SocketAsyncEventArgs args) { using (LockWait lwait = new LockWait(ref lockParam)) { this.freeBufferIndexPool.Enqueue(args.Offset); args.SetBuffer(null, 0, 0); } }
private void Close() { using (LockWait lwait = new LockWait(ref lParam)) { DisposeSocketPool(); SafeClose(); isConnected = false; } }
public IEnumerable <NetConnectionToken> ReadNext() { using (LockWait lwait = new LockWait(ref lockParam)) { foreach (var l in list) { yield return(l); } } }
public bool RemoveToken(NetConnectionToken ncToken, bool isClose) { using (LockWait lwait = new LockWait(ref lockParam)) { if (isClose) { ncToken.Token.Close(); } return(list.Remove(ncToken)); } }
public NetConnectionToken GetTop() { using (LockWait lwait = new LockWait(ref lockParam)) { if (list.Count > 0) { return(list.First()); } return(null); } }
public bool RemoveToken(SocketToken sToken) { using (LockWait lwait = new LockWait(ref lockParam)) { var item = list.Where(x => x.Token.CompareTo(sToken) == 0).FirstOrDefault(); if (item != null) { return(list.Remove(item)); } } return(false); }
public bool RefreshConnectionToken(SocketToken sToken) { using (LockWait lwait = new LockWait(ref lockParam)) { var rt = list.Find(new NetConnectionToken(sToken)); if (rt == null) { return(false); } rt.Value.ConnectionTime = DateTime.Now; return(true); } }
private void Close() { using (LockWait wait = new LockWait(ref _lockParam)) { _sendArgs.Clear(); if (_sendBuffMgr != null) { _sendBuffMgr.Clear(); _sendBuffMgr.FreeBuffer(); } SafeClose(); connected = false; } }