/// <summary> /// 构造函数 /// </summary> public TcpSocketAsync(int sendBufferSize = 8 * 1024, int receiveBufferSize = 8 * 1024) { int max = 8 * 1024; int min = 16; this.SendBufferSize = sendBufferSize < min ? min : (sendBufferSize > max ? max : sendBufferSize); this.ReceiveBufferSize = receiveBufferSize < min ? min : (receiveBufferSize > max ? max : receiveBufferSize); this._buffer = new BufferModel(this.ReceiveBufferSize); this._cache = new CacheModel(); }
/// <summary> /// 释放所有资源 /// </summary> public void Dispose() { this.Close(); if (!this.isDisposed) { this.isDisposed = true; this.socket = null; this.isSend = false; if (this.sendQueue != null) { this.sendQueue.Clear(); } this.sendQueue = null; this.UserState = null; this.AsyncConnectEvent = null; this.ErrorEvent = null; this.ReadingEvent = null; this._buffer = null; this._cache = null; this.ReceiveEvent = null; this.sendLock = null; } }
internal TcpSocketAsync(Socket socket, int sendBufferSize = 8 * 1024, int receiveBufferSize = 8 * 1024) { int max = 8 * 1024; int min = 16; this.SendBufferSize = sendBufferSize < min ? min : (sendBufferSize > max ? max : sendBufferSize); this.ReceiveBufferSize = receiveBufferSize < min ? min : (receiveBufferSize > max ? max : receiveBufferSize); this._buffer = new BufferModel(this.ReceiveBufferSize); this._cache = new CacheModel(); this.socket = socket; this.isClose = false; this.isSend = false; this.sendQueue.Clear(); this.Handle = this.socket.Handle; this.LocalEndPoint = this.socket.LocalEndPoint; this.RemoteEndPoint = this.socket.RemoteEndPoint; //this.SetKeepAlive(60 * 1000, 60 * 1000); this.socket.SendBufferSize = this.SendBufferSize; this.socket.ReceiveBufferSize = this.ReceiveBufferSize; }
protected void Dispose(bool dispose) { if (dispose) { this.Close(); this.isDisposed = true; this.m_socket = null; this.isSend = false; if (this.sendQueue != null) { this.sendQueue.Clear(); } this.sendQueue = null; this.UserState = null; this.AsyncConnectEvent = null; this.ErrorEvent = null; this.ReadingEvent = null; this.m_buffer = null; this.m_cache = null; this.ReceiveEvent = null; this.sendLock = null; } }
protected override bool ReceiveData(BufferModel buffer, CacheModel cache, out List <byte[]> data) { data = null; if (cache.Size == 0 && buffer.Position < SocketHelper.PREFIX_LENGTH) { return(true); } else { int start = 0; while (start < cache.Position + buffer.Position) { int len = 0; if (cache.Position > 0) { len = cache.Size; } else { byte[] arrlen = new byte[SocketHelper.PREFIX_LENGTH]; Array.Copy(buffer.Data, start, arrlen, 0, arrlen.Length); len = SocketHelper.ToPrefixLength(arrlen); } if (len <= 0 || len > SocketHelper.MAX_PREFIX_LENGTH) { return(false); } if (start + len <= cache.Position + buffer.Position) { if (cache.Position > 0) { Array.Copy(buffer.Data, start, cache.Data, cache.Position, len - cache.Position); if (data == null) { data = new List <byte[]>(3); } data.Add(cache.Data); start = len - cache.Position; cache.Clear(); } else { var temp = new byte[len]; Array.Copy(buffer.Data, start, temp, 0, temp.Length); if (data == null) { data = new List <byte[]>(3); } data.Add(temp); start += len; } if (start == buffer.Position) { buffer.Clear(); } } else { if (cache.Position == 0) { cache.Data = new byte[len]; cache.Size = len; } Array.Copy(buffer.Data, start, cache.Data, cache.Position, buffer.Position - start); cache.Position = cache.Position + buffer.Position - start; start = cache.Position; buffer.Clear(); } } } return(true); }
protected abstract bool ReceiveData(BufferModel buffer, CacheModel cache, out List <byte[]> data);