/// <summary> /// /// </summary> /// <param name="socket"></param> /// <param name="stream"></param> public Connection(Socket socket, Stream stream) { this.Socket = socket; this.Stream = stream; this.LocalEndPoint = this.Socket.LocalEndPoint; this.RemoteEndPoint = this.Socket.RemoteEndPoint; this.ConnectTime = DateTime.Now; this.ProtocolType = this.Socket.ProtocolType; this.sendLocker = new InterLocker(); this.receiveLocker = new InterLocker(); this.Id = NextId.Next(); }
/// <summary> /// /// </summary> public Connection(Socket socket, ISocketBufferProvider bufferProvider, ISocketProtocol socketProtocol = null) { this.socket = socket; this.LocalEndPoint = this.socket.LocalEndPoint; this.RemoteEndPoint = this.socket.RemoteEndPoint; this.ConnectTime = DateTime.Now; this.ProtocolType = this.socket.ProtocolType; this.bufferProvider = bufferProvider; //发送方根据得到的数据去setbuffer this.sendSocketAsyncEvent = new SocketAsyncEventArgs() { AcceptSocket = this.socket, }; this.sendSocketAsyncEvent.Completed += SendSocketAsyncEvent_Completed; var buffer = this.bufferProvider.Alloc(); this.receiveSocketAsyncEvent = new SocketAsyncEventArgs() { AcceptSocket = this.socket, UserToken = new SocketUserToken() { SocketBuffer = buffer, }, }; this.receiveSocketAsyncEvent.Completed += ReceiveSocketAsyncEvent_Completed; this.receiveSocketAsyncEvent.SetBuffer(buffer.Segment.Array, buffer.Segment.Offset, buffer.Segment.Count); //注意这里塞入的是原始数据,拿出的再加工发送 this.sendDataQueue = new ConcurrentQueue <byte[]>(); //这里是获取了传输的数据,并不是buffer里面的 this.receiveDataQueue = new ConcurrentQueue <byte[]>(); this.sendLocker = new InterLocker(); this.receiveLocker = new InterLocker(); this.dataProtocol = socketProtocol ?? new DataProtocol(); this.Id = NextId.Next(); }