/// <summary>根据网络标识创建客户端会话并连接(对Tcp)</summary> /// <param name="uri"></param> /// <param name="timeout">数据接收超时时间</param> /// <returns></returns> public static ISocketSession CreateSession(NetUri uri, int timeout = 3000) { if (uri == null) throw new ArgumentNullException("uri"); // 特殊处理UDP if (uri.ProtocolType == ProtocolType.Udp) { var server = Container.Resolve<ISocketServer>(uri.ProtocolType); if (server is UdpServer) { var udp = server as UdpServer; udp.Timeout = timeout; //udp.Open(); //udp.ReceiveAsync(); return udp.CreateSession(uri.EndPoint); } } var client = Container.Resolve<ISocketSession>(uri.ProtocolType); if (uri.EndPoint != null) { //if (uri.ProtocolType == ProtocolType.Tcp) (client as ISocketClient).Connect(uri.EndPoint); if (client is ISocketClient && !uri.EndPoint.IsAny()) //(client as ISocketClient).Connect(uri.EndPoint); { var socketClient = client as ISocketClient; socketClient.Timeout = timeout; } } client.Remote = uri; return client; }
/// <summary>实例化增强TCP</summary> public TcpSession() { Name = GetType().Name; Local = new NetUri(NetType.Tcp, IPAddress.Any, 0); Remote = new NetUri(NetType.Tcp, IPAddress.Any, 0); DisconnectWhenEmptyData = true; AutoReconnect = 3; }
/// <summary>实例化增强UDP</summary> public UdpServer() { SessionTimeout = 30; Local = new NetUri(ProtocolType.Udp, IPAddress.Any, 0); Remote.ProtocolType = ProtocolType.Udp; _Sessions = new SessionCollection(this); StatSession = new Statistics(); }
/// <summary>粘包测试</summary> public static void Test() { var svr = new NetServer { Port = 777, SessionPacket = new PacketFactory { Offset = 0, Size = 0 }, Log = Log.XTrace.Log, //svr.LogSend = true; LogReceive = true }; svr.Received += (s, e) => (s as INetSession).Send(e.Packet); svr.Start(); // 凑齐10个带有长度的数据帧一起发出 var ms = new MemoryStream(); for (var i = 0; i < 5; i++) { var size = i < 4 ? Security.Rand.Next(1400) : Security.Rand.Next(2000, 30000); var str = Security.Rand.NextString(size); var s = str.Substring(0, Math.Min(str.Length, 16)); //var h = str.GetBytes().ToHex(); var mm = new MemoryStream(); mm.WriteArray(str.GetBytes()); var h = mm.ToArray().ToHex(); h = h.Substring(0, Math.Min(h.Length, 32)); Console.WriteLine("{0}\t{1}\t{2}", mm.ToArray().Length, s, h); ms.WriteArray(str.GetBytes()); } var client = new NetUri("tcp://127.0.0.1:777").CreateRemote(); //client.Remote.Address = NetHelper.MyIP(); //client.Remote.Address = System.Net.IPAddress.Parse("1.0.0.13"); client.Packet = new PacketProvider { Offset = 0, Size = 0 }; client.Log = Log.XTrace.Log; client.LogSend = true; //client.LogReceive = true; //client.BufferSize = 1500; client.Received += (s, e) => Log.XTrace.WriteLine("Client {0}", e.Packet.Count); var rs = client.SendAsync(ms.ToArray()).Result; Console.WriteLine("rs={0}", rs.Count); Console.ReadKey(true); client.Close("结束"); svr.Dispose(); }
/// <summary>实例化增强UDP</summary> public UdpServer() { //SessionTimeout = 30; Local = new NetUri(NetType.Udp, IPAddress.Any, 0); Remote.Type = NetType.Udp; _Sessions = new SessionCollection(this); StatSession = new Statistics(); //PacketQueue = new DefaultPacketQueue(); }
/// <summary>构造TCP服务器对象</summary> public TcpServer() { Name = GetType().Name; Local = new NetUri(NetType.Tcp, IPAddress.Any, 0); SessionTimeout = Setting.Current.SessionTimeout; MaxAsync = Environment.ProcessorCount * 16 / 10; _Sessions = new SessionCollection(this); Log = Logger.Null; }
/// <summary>从另一个对象复制</summary> /// <param name="uri"></param> /// <returns></returns> public NetUri CopyFrom(NetUri uri) { if (uri == null) { return(this); } Protocol = uri.Protocol; Host = uri.Host; Port = uri.Port; return(this); }
/// <summary>根据网络标识创建客户端并连接(对Tcp)</summary> /// <param name="uri"></param> /// <returns></returns> public static ISocketClient CreateClient(NetUri uri) { if (uri == null) throw new ArgumentNullException("uri"); var client = Container.Resolve<ISocketClient>(uri.ProtocolType); //if (uri.EndPoint != null) //{ // if (uri.ProtocolType == ProtocolType.Tcp) client.Connect(uri.EndPoint); //} client.Remote = uri; return client; }
/// <summary>构造函数,初始化默认名称</summary> public SessionBase() { Name = this.GetType().Name; Local = new NetUri(); Remote = new NetUri(); Timeout = 3000; StartTime = DateTime.Now; StatSend = new Statistics(); StatReceive = new Statistics(); Log = Logger.Null; }
/// <summary>实例化增强UDP</summary> public UdpServer() { SessionTimeout = 30; // Udp服务器不能关闭自己,但是要关闭会话 // Udp客户端一般不关闭自己 EnableReset = false; Local = new NetUri(NetType.Udp, IPAddress.Any, 0); Remote.Type = NetType.Udp; _Sessions = new SessionCollection(this); StatSession = new Statistics(); }
/// <summary>实例化增强UDP</summary> public UdpServer() { SessionTimeout = 30; // Udp服务器不能关闭自己,但是要关闭会话 // Udp客户端一般不关闭自己 //EnableReset = false; Local = new NetUri(NetType.Udp, IPAddress.Any, 0); Remote.Type = NetType.Udp; _Sessions = new SessionCollection(this); StatSession = new Statistics(); }
public UdpSession(UdpServer server, IPEndPoint remote) { Name = server.Name; Stream = new MemoryStream(); StartTime = DateTime.Now; Server = server; Remote = new NetUri(NetType.Udp, remote); _Filter = remote; //StatSend = new Statistics(); //StatReceive = new Statistics(); StatSend = server.StatSend; StatReceive = server.StatReceive; }
/// <summary>构造函数,初始化默认名称</summary> public SessionBase() { Name = GetType().Name; Local = new NetUri(); Remote = new NetUri(); Timeout = 3000; StartTime = DateTime.Now; StatSend = new Statistics(); StatReceive = new Statistics(); Log = Logger.Null; MaxAsync = 1; }
public UdpSession(UdpServer server, IPEndPoint remote) { Name = server.Name; Stream = new MemoryStream(); StartTime = DateTime.Now; Server = server; Remote = new NetUri(NetType.Udp, remote); StatSend = server.StatSend; StatReceive = server.StatReceive; // 检查并开启广播 server.Client.CheckBroadcast(remote.Address); }
/// <summary>构造TCP服务器对象</summary> public TcpServer() { Name = this.GetType().Name; Local = new NetUri(ProtocolType.Tcp, IPAddress.Any, 0); SessionTimeout = 30; AutoReceiveAsync = true; UseProcessAsync = true; _Sessions = new SessionCollection(this); StatSession = new Statistics(); StatSend = new Statistics(); StatReceive = new Statistics(); Log = Logger.Null; }
/// <summary>根据网络标识创建客户端并连接(对Tcp)</summary> /// <param name="uri"></param> /// <returns></returns> public static ISocketClient CreateClient(NetUri uri) { if (uri == null) { throw new ArgumentNullException("uri"); } var client = Container.Resolve <ISocketClient>(uri.ProtocolType); //if (uri.EndPoint != null) //{ // if (uri.ProtocolType == ProtocolType.Tcp) client.Connect(uri.EndPoint); //} client.Remote = uri; return(client); }
/// <summary>构造TCP服务器对象</summary> public TcpServer() { Name = GetType().Name; Local = new NetUri(NetType.Tcp, IPAddress.Any, 0); SessionTimeout = 30; AutoReceiveAsync = true; UseProcessAsync = true; MaxAsync = Environment.ProcessorCount * 16 / 10; _Sessions = new SessionCollection(this); StatSession = new Statistics(); StatSend = new Statistics(); StatReceive = new Statistics(); Log = Logger.Null; }
/// <summary>粘包测试</summary> public static void Test() { var svr = new NetServer(); svr.Port = 777; svr.SessionPacket = new HeaderLengthPacketFactory(); svr.Log = Log.XTrace.Log; svr.LogReceive = true; svr.Start(); // 凑齐10个带有长度的数据帧一起发出 var ms = new MemoryStream(); for (int i = 0; i < 5; i++) { var size = i < 4 ? Security.Rand.Next(1400) : Security.Rand.Next(2000, 30000); var str = Security.Rand.NextString(size); var s = str.Substring(0, Math.Min(str.Length, 16)); //var h = str.GetBytes().ToHex(); var mm = new MemoryStream(); mm.WriteArray(str.GetBytes()); var h = mm.ToArray().ToHex(); h = h.Substring(0, Math.Min(h.Length, 32)); Console.WriteLine("{0}\t{1}\t{2}", mm.ToArray().Length, s, h); ms.WriteArray(str.GetBytes()); } var client = new NetUri("tcp://127.0.0.1:777").CreateRemote(); //client.Remote.Address = NetHelper.MyIP(); //client.Remote.Address = System.Net.IPAddress.Parse("1.0.0.13"); client.Log = Log.XTrace.Log; client.LogSend = true; //client.BufferSize = 1500; client.SendAsync(ms.ToArray()); Console.ReadKey(true); client.Close(); svr.Dispose(); }
/// <summary>根据网络标识创建客户端会话并连接(对Tcp)</summary> /// <param name="uri"></param> /// <param name="timeout">数据接收超时时间</param> /// <returns></returns> public static ISocketSession CreateSession(NetUri uri, int timeout = 3000) { if (uri == null) { throw new ArgumentNullException("uri"); } // 特殊处理UDP if (uri.ProtocolType == ProtocolType.Udp) { var server = Container.Resolve <ISocketServer>(uri.ProtocolType); if (server is UdpServer) { var udp = server as UdpServer; udp.Timeout = timeout; //udp.Open(); //udp.ReceiveAsync(); return(udp.CreateSession(uri.EndPoint)); } } var client = Container.Resolve <ISocketSession>(uri.ProtocolType); if (uri.EndPoint != null) { //if (uri.ProtocolType == ProtocolType.Tcp) (client as ISocketClient).Connect(uri.EndPoint); if (client is ISocketClient && !uri.EndPoint.IsAny()) //(client as ISocketClient).Connect(uri.EndPoint); { var socketClient = client as ISocketClient; socketClient.Timeout = timeout; } } client.Remote = uri; return(client); }
static void Test3() { var uri = new NetUri("udp://x2:3389"); Console.WriteLine(uri); Console.WriteLine(uri.Type); Console.WriteLine(uri.EndPoint); Console.WriteLine(uri.Address); Console.WriteLine(uri.Host); Console.WriteLine(uri.Port); var xml = uri.ToXml(); Console.WriteLine(xml); uri = xml.ToXmlEntity<NetUri>(); Console.WriteLine(uri); }
/// <summary>从另一个对象复制</summary> /// <param name="uri"></param> /// <returns></returns> public NetUri CopyFrom(NetUri uri) { if (uri == null) return this; Protocol = uri.Protocol; Host = uri.Host; Port = uri.Port; return this; }
/// <summary>是否相等的地址</summary> /// <param name="uri"></param> /// <returns></returns> public Boolean Equals(NetUri uri) { return Type == uri.Type && Port == uri.Port && Address == uri.Address; }
/// <summary>是否相等的地址</summary> /// <param name="uri"></param> /// <returns></returns> public Boolean Equals(NetUri uri) { return(Type == uri.Type && Port == uri.Port && Address == uri.Address); }