/// <summary> /// 创建一个客户端超级套接字 /// <para>客户端会尝试连接3次,如果3次都失败则触发连接失败回调</para> /// </summary> /// <param name="ip">连接地址</param> /// <param name="port">连接端口</param> /// <param name="listener">监听器</param> /// <param name="config">配置</param> /// <returns></returns> public static HyperSocket CreateClient(string ip, uint port, IHyperSocketClient listener, HyperSocketConfig config = null) { try { if (config == null) { config = new HyperSocketConfig(); } var hyperSocket = new HyperSocket(false, ip, port, 0, 0, config); hyperSocket.cntListener = listener; // 开始发起连接 hyperSocket.tcpClient = new HyperSocketClientModule(ip, (int)port, (int)config.TcpReceiveSize, ProtocolType.Tcp, hyperSocket); hyperSocket.tcpClient.SetListener(listener); hyperSocket.tcpClient.Init(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, hyperSocket.tcpClient); // 发送验证标签 hyperSocket.tcpClient.Send(FirstConnectBytes); return(hyperSocket); } catch (Exception ex) { listener.SocketError(null, ex); return(null); } }
/// <summary> /// 创建一个服务器超级套接字 /// </summary> /// <param name="ip">监听地址</param> /// <param name="tcpPort">tcp端口</param> /// <param name="udpPort">udp端口</param> /// <param name="connectMaxNum">允许最大连接数</param> /// <param name="listener">监听器</param> /// <param name="config">配置</param> /// <returns></returns> public static HyperSocket CreateServer(string ip, uint tcpPort, uint udpPort, uint connectMaxNum, IHyperSocketServer listener, HyperSocketConfig config = null) { if (config == null) { config = new HyperSocketConfig(); } if (connectMaxNum > ushort.MaxValue - 1) { connectMaxNum = ushort.MaxValue - 1; } var hyperSocket = new HyperSocket(true, ip, tcpPort, udpPort, connectMaxNum, config); hyperSocket.tcpServer = new HyperSocketServerModule(ip, (int)tcpPort, (int)connectMaxNum, (int)config.TcpReceiveSize, hyperSocket); hyperSocket.udpServer = new HyperSocketServerModule(ip, (int)udpPort, (int)connectMaxNum, (int)config.UdpReceiveSize, hyperSocket); hyperSocket.svrListener = listener; hyperSocket.tcpServer.SetListener(listener); hyperSocket.udpServer.SetListener(listener); var r1 = hyperSocket.tcpServer.Init(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, 0, hyperSocket.tcpServer); var r2 = hyperSocket.udpServer.Init(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp, 0, hyperSocket.udpServer); if (r1 && r2) { hyperSocket.IsAlive = true; hyperSocket.timeFlow.StartTimeFlowES(); return(hyperSocket); } else { return(null); } }
internal HyperSocketClientModule(string ip, int port, int num, ProtocolType protocolType, HyperSocket hyperSocket) : base(ip, port, num) { this.hyperSocket = hyperSocket; if (protocolType == ProtocolType.Udp) { kcpHelper = new KcpHelper(hyperSocket.SessionId, (int)hyperSocket.config.UdpReceiveSize, hyperSocket.config.KcpWinSize, hyperSocket.config.kcpMode, this); } }
internal RemoteHyperSocket(ushort sessionId, HyperSocket hyperSocket, HyperSocketConfig config) { if (config.UseSSL) { ssl = new SSL(SSL.SSLMode.AES); } hyperSocketRef = new WeakReference <HyperSocket>(hyperSocket); SessionId = sessionId; kcpHelper = new KcpHelper(sessionId, (int)config.UdpReceiveSize, config.KcpWinSize, config.kcpMode, this); }
internal void CloseSocket() { Close(); if (kcpHelper != null) { kcpHelper.CloseKcp(); } hyperSocket = null; listener = null; }
internal HyperSocketServerModule(string ip, int port, int num, int size, HyperSocket hyperSocket) : base(ip, port, num, size) { this.hyperSocket = hyperSocket; }
internal void CloseSocket() { CloseServer(); hyperSocket = null; listener = null; }