/// <summary> /// TCP调用服务端 /// </summary> /// <param name="attribute">配置信息</param> protected TmphServer(TmphTcpServer attribute) { if (attribute == null) TmphLog.Error.Throw(TmphLog.TmphExceptionType.Null); if (attribute.TcpRegisterName != null) { tcpRegisterClient = TmphTcpRegister.TmphClient.Get(attribute.TcpRegisterName); if (tcpRegisterClient == null) TmphLog.Error.Throw("TCP注册服务 " + attribute.TcpRegisterName + " 链接失败", true, false); var state = tcpRegisterClient.Register(attribute); if (state != TmphTcpRegister.TmphRegisterState.Success) TmphLog.Error.Throw("TCP服务注册 " + attribute.ServiceName + " 失败 " + state, true, false); TmphLog.Default.Add(attribute.ServiceName + " 注册 " + attribute.Host + ":" + attribute.Port.toString(), false, false); } if (!attribute.IsServer) TmphLog.Default.Add("配置未指明的TCP服务端 " + attribute.ServiceName, true, false); this.attribute = attribute; }
/// <summary> /// TCP调用客户端 /// </summary> /// <param name="attribute">TCP调用服务器端配置信息</param> /// <param name="verifyMethod">TCP验证方法</param> public TmphCommandClient(TmphTcpServer attribute, TmphTcpBase.ITcpClientVerifyMethod<TmphCommandClient> verifyMethod) { if (attribute == null) TmphLog.Error.Throw(TmphLog.TmphExceptionType.Null); TmphClient = new TmphCommandClient<TmphCommandClient>(attribute, 1024, verifyMethod ?? new TmphVerifyMethod(), this); }
/// <summary> /// TCP调用客户端 /// </summary> /// <param name="attribute">TCP调用服务器端配置信息</param> public TmphCommandClient(TmphTcpServer attribute) : this(attribute, null) { }
/// <summary> /// TCP调用服务端 /// </summary> /// <param name="attribute">配置信息</param> /// <param name="isStart">是否启动客户端</param> public TmphClient(TmphTcpServer attribute, bool isStart) { this.attribute = attribute; if (isStart) start(); }
/// <summary> /// 创建TCP客户端 /// </summary> /// <param name="attribute">配置信息</param> /// <returns>TCP客户端,失败返回null</returns> internal static Socket Create(TmphTcpServer attribute) { Socket socket = null; try { if (attribute.IpAddress == IPAddress.Any) { if (!isAnyIpAddress) TmphLog.Error.Add( "客户端TCP连接失败(" + attribute.ServiceName + " " + attribute.Host + ":" + attribute.Port.toString() + ")", true, false); isAnyIpAddress = true; return null; } isAnyIpAddress = false; socket = new Socket(attribute.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(attribute.IpAddress, attribute.Port); return socket; } catch (Exception error) { TmphLog.Error.Add(error, "客户端TCP连接失败(" + attribute.ServiceName + " " + attribute.IpAddress + ":" + attribute.Port.toString() + ")", false); TmphLog.Error.Add( "客户端TCP连接失败(" + attribute.ServiceName + " " + attribute.IpAddress + ":" + attribute.Port.toString() + ")", true, false); if (socket != null) socket.Close(); } return null; }
/// <summary> /// 获取配置信息 /// </summary> /// <param name="serviceName">TCP调用服务名称</param> /// <param name="type">TCP服务器类型</param> /// <returns>TCP调用服务器端配置信息</returns> public static TmphTcpServer GetTcpCallConfig(string serviceName, Type type = null) { var attribute = new TmphTcpServer(); if (type != null) { var tcpCall = TmphTypeAttribute.GetAttribute<TmphTcpCall>(type, false, true); if (tcpCall != null) attribute.CopyFrom(tcpCall); } attribute = pub.LoadConfig(attribute, serviceName); if (attribute.Service == null) attribute.Service = serviceName; return attribute; }