/// <summary>
 /// 移除TCP调用客户端
 /// </summary>
 /// <param name="host">TCP调用端口信息</param>
 /// <returns>TCP调用客户端+未完成处理数量</returns>
 internal keyValue <clientType, int> Remove(ref host host)
 {
     if (Client.Client != null && Host.Equals(host))
     {
         return(Remove());
     }
     return(default(keyValue <clientType, int>));
 }
Beispiel #2
0
        /// <summary>
        /// 注销TCP服务信息
        /// </summary>
        /// <param name="identity">TCP服务端标识</param>
        /// <param name="service">TCP服务信息</param>
        /// <returns>TCP服务端口信息集合信息是否被修改</returns>
        private unsafe bool removeRegister(indexIdentity identity, services service)
        {
            int      count = (service.Hosts.Length + 7) >> 3, index = 0;
            byte *   isRemove  = stackalloc byte[count];
            fixedMap removeMap = new fixedMap(isRemove, count);

            count = 0;
            indexIdentity hostIdentity;

            foreach (host host in service.Hosts)
            {
                if (hostClients.TryGetValue(host, out hostIdentity) && hostIdentity.Equals(identity) == 0)
                {
                    removeMap.Set(index);
                }
                else
                {
                    ++count;
                }
                ++index;
            }
            if (count == service.Hosts.Length)
            {
                return(false);
            }
            hashString serviceName = service.Name;

            if (count == 0)
            {
                foreach (host host in service.Hosts)
                {
                    hostClients.Remove(host);
                }
                service.Hosts = nullValue <host> .Array;
                return(true);
            }
            host[] hosts = new host[count];
            count = index = 0;
            foreach (host host in service.Hosts)
            {
                if (removeMap.Get(index++))
                {
                    hostClients.Remove(host);
                }
                else
                {
                    hosts[count++] = host;
                }
            }
            service.Hosts             = hosts;
            serviceCache[serviceName] = service;
            return(true);
        }
 /// <summary>
 /// 重置TCP调用客户端
 /// </summary>
 /// <param name="host">TCP调用端口信息</param>
 /// <param name="client">TCP调用客户端</param>
 /// <returns>TCP调用客户端+未完成处理数量</returns>
 internal keyValue <clientType, int> ReSet(ref host host, clientType client)
 {
     if (Client.Client != null && Host.Equals(host))
     {
         clientType removeClient = Client.Client;
         int        count        = Count;
         Client.Set(client);
         Count = 0;
         return(new keyValue <clientType, int>(removeClient, count));
     }
     return(default(keyValue <clientType, int>));
 }
        /// <summary>
        /// TCP调用客户端调用结束
        /// </summary>
        /// <param name="client">TCP调用服务器信息</param>
        /// <param name="returnType">是否回调成功</param>
        protected void _end_(ref clientIdentity client, fastCSharp.net.returnValue.type returnType)
        {
            if (isDisposed == 0)
            {
                if (returnType == fastCSharp.net.returnValue.type.Success)
                {
                    Monitor.Enter(clientLock);
                    callCount -= clients[client.Index].End(client.Identity);
                    Monitor.Exit(clientLock);
                }
                else
                {
                    Monitor.Enter(clientLock);
                    keyValue <clientType, keyValue <host, int> > errorClient = clients[client.Index].Error(client.Identity);
                    if (errorClient.Key == null)
                    {
                        Monitor.Exit(clientLock);
                    }
                    else
                    {
                        callCount -= errorClient.Value.Value;
                        try
                        {
                            freeIndexs.Add(client.Index);
                        }
                        finally
                        {
                            Monitor.Exit(clientLock);
                            pub.Dispose(ref errorClient.Key);

                            host host     = errorClient.Value.Key;
                            bool isCreate = newServer(ref host);
                            if (isCreate)
                            {
                                tryCheck();
                                log.Default.Add("恢复TCP调用服务端[调用错误] " + host.Host + ":" + host.Port.toString(), new System.Diagnostics.StackFrame(), false);
                            }
                            else
                            {
                                log.Default.Add("移除TCP调用服务端[调用错误] " + host.Host + ":" + host.Port.toString(), new System.Diagnostics.StackFrame(), false);
                            }
                        }
                    }
                }
            }
            client.Identity = int.MinValue;
            client.Client   = null;
        }
 /// <summary>
 /// 添加TCP调用服务端
 /// </summary>
 /// <param name="host">TCP调用服务端端口信息</param>
 /// <param name="onReturn">创建完成回调处理</param>
 public void NewServer(ref host host, Action <bool> onReturn)
 {
     try
     {
         bool output = false;
         client.StreamSocket.Get(onReturn == null ? null : new serverReturn {
             OnReturn = onReturn
         }, newServerCommand, ref host, ref output, true);
         return;
     }
     catch (Exception error)
     {
         log.Error.Add(error, null, false);
     }
     if (onReturn != null)
     {
         onReturn(false);
     }
 }
            /// <summary>
            /// 移除TCP调用服务端
            /// </summary>
            /// <param name="host">TCP调用服务端端口信息</param>
            /// <returns>是否移除成功</returns>
            public bool RemoveServer(host host)
            {
                keyValue <tcp.commandClient.streamCommandSocket, fastCSharp.net.waitCall <bool> > wait = getWait <bool>(false);

                if (wait.Value != null)
                {
                    try
                    {
                        fastCSharp.net.returnValue <bool> value = new returnValue <bool>();
                        wait.Key.Get(wait.Value, removeServerCommand, ref host, ref value.Value, false);
                        wait.Value.Get(out value);
                        return(value.Type == returnValue.type.Success && value.Value);
                    }
                    catch (Exception error)
                    {
                        log.Error.Add(error, null, false);
                    }
                }
                return(false);
            }
 /// <summary>
 /// 添加TCP调用服务端
 /// </summary>
 /// <param name="socket">TCP调用套接字</param>
 /// <param name="data">参数序列化数据</param>
 private void newServer(socket socket, ref subArray <byte> data)
 {
     fastCSharp.net.returnValue.type returnType;
     try
     {
         host host = new host();
         if (fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref host))
         {
             fastCSharp.threading.threadPool.TinyPool.FastStart(new serverInfo {
                 Server = server, Socket = socket, Identity = socket.Identity, Host = host
             }, thread.callType.TcpCommandLoadBalancingServerInfo);
             return;
         }
         returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError;
     }
     catch (Exception error)
     {
         returnType = fastCSharp.net.returnValue.type.ServerException;
         fastCSharp.log.Error.Add(error, null, true);
     }
     socket.SendStream(ref socket.identity, returnType);
 }
 /// <summary>
 /// 移除TCP调用服务端
 /// </summary>
 /// <param name="socket">TCP调用套接字</param>
 /// <param name="data">参数序列化数据</param>
 private void removeServer(socket socket, ref subArray <byte> data)
 {
     fastCSharp.net.returnValue.type returnType;
     try
     {
         host host = new host();
         if (fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref host))
         {
             fastCSharp.net.returnValue <bool> output = new fastCSharp.net.returnValue <bool> {
                 Type = returnValue.type.Success, Value = server.removeServer(ref host)
             };
             socket.SendStream(ref socket.identity, ref output, default(tcp.commandServer.commandFlags));
             return;
         }
         returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError;
     }
     catch (Exception error)
     {
         returnType = fastCSharp.net.returnValue.type.ServerException;
         fastCSharp.log.Error.Add(error, null, true);
     }
     socket.SendStream(ref socket.identity, returnType);
 }
Beispiel #9
0
        /// <summary>
        /// 注册TCP服务信息
        /// </summary>
        /// <param name="identity">TCP服务端标识</param>
        /// <param name="service">TCP服务信息</param>
        /// <param name="services">TCP服务信息集合</param>
        /// <param name="serviceName">TCP服务名称标识</param>
        /// <returns>注册状态</returns>
        private registerResult register(indexIdentity identity, service service, services services, ref hashString serviceName)
        {
            int hostCount = 0;

            if (services.IsSingle || service.IsSingle)
            {
                foreach (host host in services.Hosts)
                {
                    indexIdentity oldClientIdentity;

                    if (hostClients.TryGetValue(host, out oldClientIdentity))
                    {
                        clientInfo oldClient = getClient(oldClientIdentity);
                        if (oldClient != null && oldClient.OnLog != null && oldClient.OnLog(log.Check))
                        {
                            services.Hosts[hostCount++] = host;
                        }
                        else
                        {
                            hostClients.Remove(host);
                        }
                    }
                }
                if (hostCount != 0)
                {
                    if (hostCount != services.Hosts.Length)
                    {
                        Array.Resize(ref services.Hosts, hostCount);
                        appendLog(new log {
                            Type = log.type.HostChanged, Services = new services {
                                Name = service.Name, Hosts = services.Hosts.copy()
                            }
                        });
                    }
                    if (service.IsPerp)
                    {
                        getPort(identity, ref service.Host, true);
                        if (service.Host.Port == 0)
                        {
                            return new registerResult {
                                       State = registerState.PortError
                            }
                        }
                        ;
                        services.SetPerpService(identity, service);
                        return(new registerResult {
                            State = registerState.Success, Service = service
                        });
                    }
                    return(new registerResult {
                        State = registerState.SingleError
                    });
                }
                services.IsSingle = false;
                services.Hosts    = nullValue <host> .Array;
                getPort(identity, ref service.Host, true);
                if (service.Host.Port == 0 || (service.IsCheck && hostClients.ContainsKey(service.Host)))
                {
                    serviceCache.Remove(serviceName);
                    appendLog(new log {
                        Type = log.type.RemoveServiceName, Services = new services {
                            Name = service.Name
                        }
                    });
                    return(new registerResult {
                        State = service.Host.Port == 0 ? registerState.PortError : registerState.HostExists
                    });
                }
                hostClients[service.Host] = identity;
                services.Hosts            = new host[] { service.Host };
                services.IsSingle         = service.IsSingle;
                appendLog(new log {
                    Type = log.type.HostChanged, Services = new services {
                        Name = service.Name, Hosts = new host[] { service.Host }
                    }
                });
            }
            else
            {
                getPort(identity, ref service.Host, true);
                if (service.Host.Port == 0)
                {
                    return new registerResult {
                               State = registerState.PortError
                    }
                }
                ;
                if (service.IsCheck && hostClients.ContainsKey(service.Host))
                {
                    return new registerResult {
                               State = registerState.HostExists
                    }
                }
                ;
                hostClients[service.Host] = identity;
                host[] hosts = new host[services.Hosts.Length + 1];
                Array.Copy(services.Hosts, 0, hosts, 1, services.Hosts.Length);
                hosts[0]       = service.Host;
                services.Hosts = hosts;
                appendLog(new log {
                    Type = log.type.HostChanged, Services = new services {
                        Name = service.Name, Hosts = services.Hosts.copy()
                    }
                });
            }
            return(new registerResult {
                State = registerState.Success, Service = service
            });
        }
 internal void Set(ref host host, clientType client)
 {
     Client.Client = client;
     Host          = host;
     Count         = 0;
 }
 internal void Set(ref host host, clientType client, int index)
 {
     Client.Set(client, index);
     Host  = host;
     Count = 0;
 }
 /// <summary>
 /// 添加TCP调用服务端
 /// </summary>
 /// <param name="host">TCP服务端口信息</param>
 /// <returns>是否添加成功</returns>
 private bool newServer(ref host host)
 {
     if (isDisposed == 0)
     {
         try
         {
             fastCSharp.code.cSharp.tcpServer attribute = this.attribute.Clone();
             attribute.IsLoadBalancing = true;
             attribute.Host            = host.Host;
             attribute.Port            = host.Port;
             clientType client = _createClient_(attribute);
             if (client != null)
             {
                 Monitor.Enter(clientLock);
                 for (int index = 0; index != currentCount; ++index)
                 {
                     keyValue <clientType, int> removeClient = this.clients[index].ReSet(ref host, client);
                     if (removeClient.Key != null)
                     {
                         callCount -= removeClient.Value;
                         Monitor.Exit(clientLock);
                         pub.Dispose(ref removeClient.Key);
                         return(true);
                     }
                 }
                 if (freeIndexs.length == 0)
                 {
                     if (currentCount == this.clients.Length)
                     {
                         try
                         {
                             clientHost[] clients = new clientHost[currentCount << 1];
                             this.clients.CopyTo(clients, 0);
                             clients[currentCount].Set(ref host, client, currentCount);
                             this.clients = clients;
                             ++currentCount;
                             client = null;
                         }
                         finally
                         {
                             Monitor.Exit(clientLock);
                             pub.Dispose(ref client);
                         }
                     }
                     else
                     {
                         clients[currentCount].Set(ref host, client, currentCount);
                         ++currentCount;
                         Monitor.Exit(clientLock);
                     }
                 }
                 else
                 {
                     clients[freeIndexs.UnsafePop()].Set(ref host, client);
                     Monitor.Exit(clientLock);
                 }
                 return(true);
             }
         }
         catch (Exception error)
         {
             log.Error.Add(error, null, false);
         }
     }
     return(false);
 }