private static ServerOptions CreateServerOptions(Baize.IPlugin.SuperSocket.ProtocolType protocolType) { return(new ServerOptions() { Listeners = new List <ListenOptions>() { new ListenOptions() { BackLog = 100, ChannelOptions = new ChannelOptions() { MaxPackageLength = 4096, ReceiveBufferSize = 1024 }, Ip = "Any", NoDelay = true, Port = 18080, ProtocolType = protocolType } }, Name = "BaizeServerUdp", ProductInfos = new List <ProductConfig>() { new ProductConfig() { BasePortocalFilterInfo = new BasePortocalFilterInfo() { ProtoType = ProtoType.ShortMessage }, ProductOID = "1" }, new ProductConfig() { BasePortocalFilterInfo = new BasePortocalFilterInfo() { ProtoType = ProtoType.ShortMessage }, ProductOID = "2" }, } }); }
/// <summary> /// 连接远程地址 /// </summary> /// <param name="ip">远程IP地址</param> /// <param name="port">端口号</param> /// <param name="protocolType">协议类型</param> /// <returns>连接成功返回会话,失败返回空</returns> public async ValueTask <BaizeSession> CreateClientAsync(string ip, int port, Baize.IPlugin.SuperSocket.ProtocolType protocolType) { BaizeSession rtn = null; try { IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), port); if (protocolType == Baize.IPlugin.SuperSocket.ProtocolType.TCP) { var socket = new Socket(iPEndPoint.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); await socket.ConnectAsync(iPEndPoint); TcpPipeChannel channel = new TcpPipeChannel(socket, _serverOptions.Value.FilterDataOptions, _logger); var appSession = new AppSession(channel); rtn = appSession.Session; HandleSession(appSession).DoNotAwait(); } else { var listener = _listeners.Where(i => i.Socket.ProtocolType == System.Net.Sockets.ProtocolType.Udp).FirstOrDefault(); if (listener != null) { UdpPipeChannel channel = new UdpPipeChannel(listener.Socket, iPEndPoint, _serverOptions.Value.FilterDataOptions, _logger); var appSession = new AppSession(channel); rtn = appSession.Session; HandleSession(appSession).DoNotAwait(); } } return(rtn); } catch (Exception e) { _logger.LogError($"Failed to connect to {ip}:{port}", e); return(null); } }