示例#1
0
        /// <summary>
        ///
        /// </summary>
        public ClientSocket(SocketSetting setting, ISocketBufferProvider bufferProvider, EndPoint serverEndPoint, ISocketProtocol socketProtocol = null, EndPoint localEndPoint = null)
        {
            this.serverEndPoint = serverEndPoint;
            this.localEndPoint  = localEndPoint;
            this.bufferProvider = bufferProvider;
            this.socketProtocol = socketProtocol;
            this.eventHandlers  = new List <ResultEventHandler <OnReceivedSocketEventArgs, byte[]> >();
            this.socket         = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
            {
                ReceiveBufferSize = setting.ReceiveBufferSize,
                SendBufferSize    = setting.SendBufferSize,
                NoDelay           = true,
                Blocking          = false,
            };

            //用来控制开始连接超时检查
            this.manualResetEvent = new System.Threading.ManualResetEvent(false);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        public ServerSocket(SocketSetting setting, ISocketBufferProvider bufferProvider, EndPoint listeningEndPoint, ISocketProtocol socketProtocol = null)
        {
            this.listeningEndPoint = listeningEndPoint;
            this.bufferProvider    = bufferProvider;
            this.setting           = setting;
            this.socketProtocol    = socketProtocol;
            this.eventHandlers     = new List <ResultEventHandler <OnReceivedSocketEventArgs, byte[]> >();
            this.socket            = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveBufferSize = setting.ReceiveBufferSize,
                SendBufferSize    = setting.SendBufferSize,
                NoDelay           = true,
                Blocking          = false,
            };

            this.connections = new ConcurrentDictionary <ulong, Connection>();
            this.acceptSocketAsyncEventArgs            = new SocketAsyncEventArgs();
            this.acceptSocketAsyncEventArgs.Completed += AcceptAsyncCompleted;
        }
示例#3
0
        /// <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();
        }