示例#1
0
 /// <summary>
 /// 创建iocp服务器Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <returns></returns>
 public static IServerSocket CreateServerSocket(ISocketOption socketOption)
 {
     if (socketOption.SocketType == SAEASocketType.Tcp)
     {
         if (socketOption.UseIocp)
         {
             return(new IocpServerSocket(socketOption));
         }
         else
         {
             CancellationTokenSource source = new CancellationTokenSource();
             CancellationToken       token  = source.Token;
             return(new StreamServerSocket(socketOption, token));
         }
     }
     else
     {
         if (socketOption.UseIocp)
         {
             return(new UdpServerSocket(socketOption));
         }
         else
         {
             throw new NotImplementedException("udp不支持此模式");
         }
     }
 }
示例#2
0
 /// <summary>
 /// 创建客户端Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <returns></returns>
 public static IClientSocket CreateClientSocket(ISocketOption socketOption)
 {
     if (socketOption.SocketType == SAEASocketType.Tcp)
     {
         if (!socketOption.UseIocp)
         {
             return(new StreamClientSocket(socketOption));
         }
         else
         {
             return(new IocpClientSocket(socketOption));
         }
     }
     else
     {
         if (!socketOption.UseIocp)
         {
             throw new NotImplementedException("udp不支持此模式");
         }
         else
         {
             return(new UdpClientSocket(socketOption));
         }
     }
 }
示例#3
0
 /// <summary>
 /// 创建服务器Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public static IServerSocket CreateServerSocket(ISocketOption socketOption, CancellationToken cancellationToken)
 {
     if (socketOption.SocketType == SAEASocketType.Tcp)
     {
         if (!socketOption.UseIocp)
         {
             return(new StreamServerSocket(socketOption, cancellationToken));
         }
         else
         {
             return(new IocpServerSocket(socketOption));
         }
     }
     else
     {
         if (!socketOption.UseIocp)
         {
             throw new NotImplementedException("udp不支持此模式");
         }
         else
         {
             return(new UdpServerSocket(socketOption));
         }
     }
 }
示例#4
0
        public QClient(string name, int bufferSize = 100 * 1024, string ip = "127.0.0.1", int port = 39654)
        {
            _name = name;

            HeartSpan = 1000 * 1000;

            HeartAsync();

            _batcher = new Batcher <byte[]>(3000, 10); //此参数用于控制产生或消费速读,过大会导致溢出异常

            _batcher.OnBatched += _batcher_OnBatched;


            _qUnpacker = new QUnpacker();

            _queueCoder = new QueueCoder();

            ISocketOption socketOption = SocketOptionBuilder.Instance.SetSocket(Sockets.Model.SAEASocketType.Tcp)
                                         .UseIocp <QUnpacker>()
                                         .SetIP(ip)
                                         .SetPort(port)
                                         .SetWriteBufferSize(bufferSize)
                                         .SetReadBufferSize(bufferSize)
                                         .ReusePort(false)
                                         .Build();

            _clientSocket = SocketFactory.CreateClientSocket(socketOption);

            _clientSocket.OnReceive += _clientSocket_OnReceive;

            _clientSocket.OnError += _clientSocket_OnError;

            _clientSocket.OnDisconnected += _clientSocket_OnDisconnected;
        }
示例#5
0
        public MqttTcpServerListener(
            AddressFamily addressFamily,
            MqttServerTcpEndpointBaseOptions options,
            X509Certificate2 tlsCertificate,
            CancellationToken cancellationToken,
            IMqttNetChildLogger logger)
        {
            _cancellationToken = cancellationToken;
            _logger            = logger;
            _addressFamily     = addressFamily;

            var sb = new SocketOptionBuilder().SetSocket(Sockets.Model.SocketType.Tcp).UseStream();

            if (options is MqttServerTlsTcpEndpointOptions tlsOptions)
            {
                sb = sb.WithSsl(tlsCertificate, tlsOptions.SslProtocol);
            }

            sb = sb.SetPort(options.Port);

            if (_addressFamily == AddressFamily.InterNetworkV6)
            {
                sb = sb.UseIPV6();
            }

            socketOption = sb.Build();

            serverSokcet = SocketFactory.CreateServerSocket(socketOption, cancellationToken);

            serverSokcet.OnAccepted += ServerSokcet_OnAccepted;
        }
示例#6
0
 /// <summary>
 /// 创建iocp服务器Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <returns></returns>
 public static IServerSokcet CreateServerSocket(ISocketOption socketOption)
 {
     if (socketOption.UseIocp)
     {
         return(new IocpServerSocket(socketOption));
     }
     return(null);
 }
示例#7
0
 /// <summary>
 /// 创建服务器Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public static IServerSokcet CreateServerSocket(ISocketOption socketOption, CancellationToken cancellationToken)
 {
     if (!socketOption.UseIocp)
     {
         return(new StreamServerSocket(socketOption, cancellationToken));
     }
     else
     {
         return(new IocpServerSocket(socketOption));
     }
 }
示例#8
0
 /// <summary>
 /// 创建客户端Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <returns></returns>
 public static IClientSocket CreateClientSocket(ISocketOption socketOption)
 {
     if (!socketOption.UseIocp)
     {
         return(new StreamClientSocket(socketOption));
     }
     else
     {
         return(new IocpClientSocket(socketOption));
     }
 }
示例#9
0
 /// <summary>
 /// 创建iocp服务器Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <returns></returns>
 public static IServerSokcet CreateServerSocket(ISocketOption socketOption)
 {
     if (socketOption.UseIocp)
     {
         return(new IocpServerSocket(socketOption));
     }
     else
     {
         CancellationTokenSource source = new CancellationTokenSource();
         CancellationToken       token  = source.Token;
         return(new StreamServerSocket(socketOption, token));
     }
 }
        public EventSocketServer(string ipAddress, int port)
        {
            ISocketOption option = SocketOptionBuilder.Instance.UseIocp(new Context())
                                   .SetIP(ipAddress)
                                   .SetPort(port)
                                   .SetSocket(SAEA.Sockets.Model.SAEASocketType.Tcp)
                                   //.SetCount(ushort.MaxValue)
                                   .Build();

            _server                 = SocketFactory.CreateServerSocket(option);
            _server.OnAccepted     += _server_OnAccepted;
            _server.OnDisconnected += _server_OnDisconnected;
            _server.OnError        += _server_OnError;
            _server.OnReceive      += _server_OnReceive;
        }
示例#11
0
        public HttpSocketDebug(int port, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000)
        {
            var optionBuilder = new SocketOptionBuilder()
                                .SetSocket(Sockets.Model.SAEASocketType.Tcp)
                                .UseIocp(new HContext())
                                .SetPort(port)
                                .SetCount(count)
                                .SetReadBufferSize(bufferSize)
                                .SetTimeOut(timeOut)
                                .ReusePort(false);

            _option = optionBuilder.Build();

            _serverSokcet            = SocketFactory.CreateServerSocket(_option);
            _serverSokcet.OnReceive += _serverSokcet_OnReceive;
        }
示例#12
0
        public HttpSocketDebug(int port, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000)
        {
            var optionBuilder = new SocketOptionBuilder()
                                .SetSocket(SAEASocketType.Tcp)
                                .UseIocp <HUnpacker>()
                                .SetPort(port)
                                .SetCount(count)
                                .SetReadBufferSize(bufferSize)
                                .SetTimeOut(timeOut)
                                .ReusePort(false);

            _option = optionBuilder.Build();

            _serverSokcet            = SocketFactory.CreateServerSocket(_option);
            _serverSokcet.OnReceive += _serverSokcet_OnReceive;
            _serverSokcet.OnError   += (i, e) => OnError?.Invoke(e);
        }
        public EventSocketClient(string ipAddress, int port)
        {
            _context = new Context();
            ISocketOption option = SocketOptionBuilder.Instance.UseIocp(_context)
                                   .SetIP(ipAddress)
                                   .SetPort(port)
                                   .Build();

            _client = SocketFactory.CreateClientSocket(option);
            // _client = new Core.Tcp.IocpClientSocket(option);
            _client.OnReceive      += _client_OnReceive;
            _client.OnError        += _client_OnError;
            _client.OnDisconnected += _client_OnDisconnected;

            sendAliveTimer           = new System.Timers.Timer(30000);
            sendAliveTimer.Elapsed  += SendAliveTimer_Elapsed;
            sendAliveTimer.AutoReset = true;
        }
示例#14
0
 /// <summary>
 /// 创建客户端Socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public static IClientSocket CreateClientSocket(ISocketOption socketOption, CancellationToken cancellationToken)
 {
     if (socketOption.SocketType == SocketType.Tcp)
     {
         if (!socketOption.UseIocp)
         {
             return(new StreamClientSocket(socketOption, cancellationToken));
         }
         else
         {
             return(new IocpClientSocket(socketOption));
         }
     }
     else
     {
         return(null);
     }
 }
示例#15
0
        /// <summary>
        /// 流模式下的tcp client socket
        /// </summary>
        /// <param name="socketOption"></param>
        public StreamClientSocket(ISocketOption socketOption) : this(socketOption, CancellationToken.None)
        {
            Context = SocketOption.Context;

            if (SocketOption.UseIPV6)
            {
                _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

                if (string.IsNullOrEmpty(SocketOption.IP))
                {
                    _serverIPEndpint = (new IPEndPoint(IPAddress.IPv6Any, SocketOption.Port));
                }
                else
                {
                    _serverIPEndpint = (new IPEndPoint(IPAddress.Parse(SocketOption.IP), SocketOption.Port));
                }
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                if (string.IsNullOrEmpty(SocketOption.IP))
                {
                    _serverIPEndpint = (new IPEndPoint(IPAddress.Any, SocketOption.Port));
                }
                else
                {
                    _serverIPEndpint = (new IPEndPoint(IPAddress.Parse(SocketOption.IP), SocketOption.Port));
                }
            }

            if (SocketOption.ReusePort)
            {
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, SocketOption.ReusePort);
            }

            _socket.NoDelay = SocketOption.NoDelay;
        }
示例#16
0
 /// <summary>
 /// 客户端 socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <param name="cancellationToken"></param>
 public StreamClientSocket(ISocketOption socketOption, CancellationToken cancellationToken) : this(cancellationToken, socketOption.IP, socketOption.Port, socketOption.ReadBufferSize, socketOption.TimeOut, socketOption.SslProtocol, socketOption.WithSsl)
 {
     _socketOption = socketOption;
 }
示例#17
0
 /// <summary>
 /// 客户端 socket
 /// </summary>
 /// <param name="socketOption"></param>
 public StreamClientSocket(ISocketOption socketOption) : this(socketOption.IP, socketOption.Port, socketOption.ReadBufferSize, socketOption.TimeOut, socketOption.SslProtocol, socketOption.WithSsl)
 {
     _socketOption = socketOption;
 }
示例#18
0
 /// <summary>
 /// 客户端 socket
 /// </summary>
 /// <param name="socketOption"></param>
 /// <param name="cancellationToken"></param>
 public StreamClientSocket(ISocketOption socketOption, CancellationToken cancellationToken)
 {
     SocketOption       = socketOption;
     _cancellationToken = cancellationToken;
 }
示例#19
0
 /// <summary>
 /// socket选项创造器
 /// </summary>
 public SocketOptionBuilder()
 {
     _socketOption = new SocketOption();
 }