public SocketChannel(string ip, int port, SocketListener listener, Protocol protocol, ISocketProtocol socketProtocol) { IP = ip; Port = port; this.socketProtocol = socketProtocol; this.listener = listener; socket = new USocket(listener, protocol); }
/// <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); }
/// <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; }
/// <summary> /// 初始化管理 /// </summary> /// <param name="maxConnectionCount">允许的最大连接数</param> /// <param name="initConnectionResourceCount">启动时初始化多少个连接的资源</param> /// <param name="socketProtocol">socket应用层协议</param> /// <param name="singleBufferMaxSize">每个 socket 读写缓存最大字节数, 默认为8k</param> /// <param name="sendTimeOut">socket 发送超时时长, 以毫秒为单位</param> /// <param name="receiveTimeOut">socket 接收超时时长, 以毫秒为单位</param> /// <returns></returns> public virtual async Task InitAsync(int maxConnectionCount, int initConnectionResourceCount , ISocketProtocol socketProtocol, int singleBufferMaxSize = 8 * 1024 , int sendTimeOut = 10000, int receiveTimeOut = 10000) { maxConnCount = maxConnectionCount; this.initConnectionResourceCount = initConnectionResourceCount; this.singleBufferMaxSize = singleBufferMaxSize; this.sendTimeOut = sendTimeOut; this.receiveTimeOut = receiveTimeOut; await Task.Run(() => { semaphore = new Semaphore(maxConnCount, maxConnCount); //设置初始线程数为cpu核数*2 connectedClients = new ConcurrentDictionary <Guid, IOCPClient>(Environment.ProcessorCount * 2, maxConnCount); //读写分离, 每个socket连接需要2个SocketAsyncEventArgs. var saePool = new SocketAsyncEventArgsPool(initConnectionResourceCount * 2, SendReceiveArgsCompleted, singleBufferMaxSize); iocpClientPool = new IOCPClientPool(initConnectionResourceCount, saePool); }); }
/// <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(); }
public ValueTask <object?> CreateConnectionInitPayload( ISocketProtocol protocol, CancellationToken cancellationToken) { return(new(_payload)); }
public DefaultSocketListener(ISocketProtocol protocol, IErrorHandle errorHandle) { this.protocol = protocol; this.errorHandle = errorHandle; }
/// <summary> /// /// </summary> public ClientSocket(SocketSetting setting, EndPoint serverEndPoint, ISocketProtocol socketProtocol = null, EndPoint localEndPoint = null) : this(setting, new SocketBufferProvider(setting), serverEndPoint, socketProtocol, localEndPoint) { }
/// <summary> /// /// </summary> public ServerSocket(SocketSetting setting, EndPoint listeningEndPoint, ISocketProtocol socketProtocol = null) : this(setting, new SocketBufferProvider(setting), listeningEndPoint, socketProtocol) { }
public DefaultSocketListener(ISocketProtocol protocol) { this.protocol = protocol; }
/// <summary> /// 发送数据包 /// </summary> /// <param name="_package">数据包对象</param> /// <param name="_protocol">数据协议对象</param> /// <returns>是否发送成功</returns> public bool SendPackage(object _package, ISocketProtocol _protocol) { return(this.SendBytes(_protocol.EnPackage(_package))); }