/// <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> /// 停止服务 /// </summary> public virtual void Dispose() { if (Interlocked.CompareExchange(ref isStart, 0, 1) == 1) { TmphLog.Default.Add( "停止服务 " + attribute.ServiceName + "[" + attribute.Host + ":" + attribute.Port.toString() + "]", true, false); TmphDomainUnload.Remove(Dispose, false); if (tcpRegisterClient != null) { tcpRegisterClient.RemoveRegister(attribute); tcpRegisterClient = null; } TmphPub.Dispose(ref this.socket); Socket[] sockets = null; TmphInterlocked.NoCheckCompareSetSleep0(ref newClientLock); if (newClients == null || newClientCount == 0) newClientLock = 0; else { try { Array.Copy(newClients, sockets = new Socket[newClientCount], newClientCount); Array.Clear(newClients, 0, newClientCount); newClientCount = 0; } finally { newClientLock = 0; } if (sockets != null) { foreach (var socket in sockets) socket.shutdown(); } } if (OnDisposed != null) OnDisposed(); } }