/// <summary> /// KeepAlive /// </summary> /// <param name="keepAliveTime">连接多长时间(毫秒)没有数据就开始发送心跳包,有数据传递的时候不发送心跳包</param> /// <param name="keepAliveInterval">每隔多长时间(毫秒)发送一个心跳包,发5次(系统默认值)</param> /// <returns>The number of bytes in the optionOutValue parameter.</returns> public int SetKeepAlive(int keepAliveTime, int keepAliveInterval) { int result = 0; if (this.IsConnected) { result = this.socket.IOControl(IOControlCode.KeepAliveValues, SocketHelper.GetTcpKeepAlive(keepAliveTime, keepAliveInterval), null); } return(result); }
/// <summary> /// 获取发送数据的长度byte[] 与数据组成的新byte[] /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] ToSendData(byte[] data) { byte[] result = null; if (data != null && data.Length > 0) { result = new byte[4 + data.Length]; byte[] prefixBytes = SocketHelper.ToPrefixBytes(data.Length); Array.Copy(prefixBytes, result, prefixBytes.Length); Array.Copy(data, 0, result, prefixBytes.Length, data.Length); } return(result); }
private void BeginSend() { if (!this.isSend && this.IsConnected && sendQueue.Count > 0) { this.isSend = true; byte[] data = this.sendQueue.Dequeue(); TcpSendData state = new TcpSendData(); state.Buffer = SocketHelper.ToSendData(data); state.BufferIndex = 0; try { this.socket.BeginSend(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, this.OnSend, state); } catch (Exception ex) { this.OnErrorEvent(ex); } } }
private void OnReceive(IAsyncResult ar) { try { int readLength = this.socket.EndReceive(ar); if (readLength == 0) { this.OnErrorEvent(new Exception("close!")); return; } this._buffer.Position = this._buffer.Position + readLength; if (this._cache.Size == 0 && this._buffer.Position >= SocketHelper.PREFIX_LENGTH) { byte[] perfixBytes = new byte[SocketHelper.PREFIX_LENGTH]; Array.Copy(this._buffer.Data, 0, perfixBytes, 0, perfixBytes.Length); this._cache.Size = SocketHelper.ToPrefixLength(perfixBytes); this._cache.Data = new byte[this._cache.Size]; this._cache.Position = this._buffer.Position - SocketHelper.PREFIX_LENGTH; Array.Copy(this._buffer.Data, SocketHelper.PREFIX_LENGTH, this._cache.Data, 0, this._cache.Position); this._buffer.Clear(); } else if (this._cache.Size > 0) { Array.Copy(this._buffer.Data, 0, this._cache.Data, this._cache.Position, this._buffer.Position); this._cache.Position = this._cache.Position + this._buffer.Position; this._buffer.Clear(); } this.OnReadingEvent(this._cache.Position, this._cache.Size); if (this._cache.Size > 0 && this._cache.Position == this._cache.Size) { this.OnReceiveEvent(this._cache.Data, this._cache.Size); this._cache.Clear(); } this.socket.BeginReceive(this._buffer.Data, this._buffer.Position, this._buffer.Size - this._buffer.Position, SocketFlags.None, this.OnReceive, null); } catch (Exception ex) { this.OnErrorEvent(ex); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="data">数据</param> public bool Send(byte[] data) { if (data != null || data.Length > 0 && this.IsConnected) { byte[] buffer = SocketHelper.ToSendData(data); int count = 0; while (count < buffer.Length) { count += this.socket.Send(buffer, count, buffer.Length - count, SocketFlags.None); } return(true); } return(false); }
/// <summary> /// 接收数据 /// </summary> /// <returns></returns> public byte[] Receive() { byte[] buffer = new byte[SocketHelper.PREFIX_LENGTH]; int count = 0; while (count < SocketHelper.PREFIX_LENGTH) { count += this.socket.Receive(buffer, count, SocketHelper.PREFIX_LENGTH - count, SocketFlags.None); } int length = SocketHelper.ToPrefixLength(buffer); byte[] data = new byte[length]; count = 0; while (count < length) { count += this.socket.Receive(data, count, length - count, SocketFlags.None); } return(data); }
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); }
public override bool Send(byte[] data) { return(base.Send(SocketHelper.ToSendData(data))); }