Exemple #1
0
 /// <summary>
 /// 获取TCP服务端口号
 /// </summary>
 /// <param name="identity">TCP服务端标识</param>
 /// <param name="host">TCP服务端口信息</param>
 /// <param name="isPerp"></param>
 private void getPort(indexIdentity identity, ref host host, bool isPerp)
 {
     if (host.Port == 0)
     {
         hashString ipKey = host.Host;
         if (!hostPorts.TryGetValue(ipKey, out host.Port))
         {
             host.Port = fastCSharp.config.tcpRegister.Default.PortStart;
         }
         int startPort = host.Port;
         while (hostClients.ContainsKey(host))
         {
             ++host.Port;
         }
         if (host.Port >= 65536)
         {
             host.Port = fastCSharp.config.tcpRegister.Default.PortStart;
             while (host.Port != startPort && hostClients.ContainsKey(host))
             {
                 ++host.Port;
             }
             if (host.Port == startPort)
             {
                 host.Port = 0;
                 return;
             }
         }
         hostPorts[ipKey] = host.Port + 1;
         if (!isPerp)
         {
             hostClients.Add(host, identity);
         }
     }
 }
Exemple #2
0
 private bool login(fastCSharp.net.tcp.commandServer.socket client, string user)
 {
     if (user.length() != 0)
     {
         hashString userHash = user;
         bool       isUser   = false;
         Monitor.Enter(userLock);
         try
         {
             if (!users.ContainsKey(userHash))
             {
                 users.Add(userHash, new userInfo {
                     User = user
                 });
                 client.ClientUserInfo = user;
                 ++userVersion;
                 isUser = true;
             }
         }
         finally { Monitor.Exit(userLock); }
         if (isUser)
         {
             if (userChangeHandle == null)
             {
                 userChangeHandle = userChange;
             }
             task.Tiny.Add(userChangeHandle, user);
             return(true);
         }
     }
     return(false);
 }
Exemple #3
0
        private void logout(fastCSharp.net.tcp.commandServer.socket client)
        {
            string     user = (string)client.ClientUserInfo;
            userInfo   userInfo;
            hashString userHash = user;

            Monitor.Enter(userLock);
            try
            {
                if (users.TryGetValue(userHash, out userInfo))
                {
                    users.Remove(userHash);
                }
                ++userVersion;
            }
            finally { Monitor.Exit(userLock); }
            if (userInfo != null)
            {
                if (userChangeHandle == null)
                {
                    userChangeHandle = userChange;
                }
                task.Tiny.Add(userChangeHandle, null);
                if (OnLogout != null)
                {
                    task.Tiny.Add(OnLogout, user);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="user">用户名</param>
        private void logout(string user)
        {
            userInfo   userInfo;
            hashString userHash = user;

            Monitor.Enter(userLock);
            try
            {
                if (users.TryGetValue(userHash, out userInfo))
                {
                    users.Remove(userHash);
                }
                ++userVersion;
            }
            finally { Monitor.Exit(userLock); }
            if (userInfo != null)
            {
                if (userChangeHandle == null)
                {
                    userChangeHandle = userChange;
                }
                task.Tiny.Add(userChangeHandle, null);
                if (OnLogout != null)
                {
                    task.Tiny.Add(OnLogout, user);
                }
            }
        }
Exemple #5
0
 private void removeRegister(clientId clientId)
 {
     if (clientId.Tick == logStream.Ticks && clientPool.Enter())
     {
         if (clientPool.Pool[clientId.Identity.Index].Free(clientId.Identity.Identity))
         {
             clientPool.FreeExit(clientId.Identity.Index);
             subArray <string> removeNames = default(subArray <string>);
             Monitor.Enter(serviceLock);
             try
             {
                 foreach (services service in serviceCache.Values.getArray())
                 {
                     hashString serviceName = service.Name;
                     if (removeRegister(clientId.Identity, service, ref serviceName))
                     {
                         removeNames.Add(service.Name);
                     }
                 }
             }
             finally { Monitor.Exit(serviceLock); }
             if (removeNames.length != 0)
             {
                 fastCSharp.log.Default.Add("TCP服务 " + removeNames.ToArray().joinString(',') + " 被注销", null, false);
             }
         }
         else
         {
             clientPool.Exit();
         }
     }
 }
Exemple #6
0
            /// <summary>
            /// 获取日志
            /// </summary>
            /// <returns></returns>
            private bool getLog()
            {
                int logIdentity;

                services[] services = registerReaderClient.getServices(out logIdentity).Value;
                if (services != null)
                {
                    services cacheServices;
                    Monitor.Enter(servicesLock);
                    try
                    {
                        foreach (services service in services)
                        {
                            hashString name = service.Name;
                            if (this.services.TryGetValue(name, out cacheServices))
                            {
                                cacheServices.Copy(service);
                            }
                            else
                            {
                                //service.Version = 1;
                                this.services.Add(name, service);
                            }
                        }
                    }
                    finally { Monitor.Exit(servicesLock); }
                    //if (OnServiceChanged != null) OnServiceChanged(null);
                    logKeep = registerReaderClient.getLog(clientId, logIdentity, logHandle);
                    return(true);
                }
                return(false);
            }
Exemple #7
0
 /// <summary>
 /// 公用错误处理函数
 /// </summary>
 /// <param name="error">错误信息</param>
 public void Error(string error)
 {
     if (!string.IsNullOrEmpty(error))
     {
         bool isLog = false;
         if (error.Length <= config.web.Default.PubErrorMaxSize)
         {
             hashString errorHash = error;
             Monitor.Enter(errorQueueLock);
             try
             {
                 if (errorQueue.Set(errorHash, error) == null)
                 {
                     isLog = true;
                     if (errorQueue.Count > config.web.Default.PubErrorMaxCacheCount)
                     {
                         errorQueue.UnsafePopValue();
                     }
                 }
             }
             finally { Monitor.Exit(errorQueueLock); }
         }
         else
         {
             isLog = true;
         }
         if (isLog)
         {
             fastCSharp.log.Default.Add(error, new System.Diagnostics.StackFrame(), false);
         }
     }
 }
Exemple #8
0
        /// <summary>
        /// 删除监视路径
        /// </summary>
        /// <param name="path">监视路径</param>
        public void Remove(string path)
        {
            path = fastCSharp.io.file.FileNameToLower(path);
            counter    counter;
            hashString pathKey = path;

            Monitor.Enter(watcherLock);
            try
            {
                if (watchers.TryGetValue(pathKey, out counter))
                {
                    if (--counter.Count == 0)
                    {
                        watchers.Remove(pathKey);
                    }
                    else
                    {
                        watchers[pathKey] = counter;
                    }
                }
            }
            finally { Monitor.Exit(watcherLock); }
            if (counter.Count == 0 && counter.Watcher != null)
            {
                dispose(counter.Watcher);
            }
        }
Exemple #9
0
 /// <summary>
 /// 用户注销
 /// </summary>
 /// <param name="user">用户名</param>
 public void Logout(string user)
 {
     if (user.length() != 0)
     {
         RemoveSession();
         int      version = 0;
         userInfo userInfo;
         keyValue <list <Action <poll.message> >, string[]> onPolls = default(keyValue <list <Action <poll.message> >, string[]>);
         Action <poll.message> onPoll   = null;
         hashString            userHash = user;
         Monitor.Enter(userLock);
         try
         {
             if (userInfos.TryGetValue(userHash, out userInfo))
             {
                 onPoll = userInfo.OnPoll;
                 userInfos.Remove(userHash);
                 version         = ++userVersion;
                 onPolls         = getOnPolls(userInfo, false);
                 userInfo.OnPoll = null;
             }
         }
         finally { Monitor.Exit(userLock); }
         if (onPolls.Key != null)
         {
             threadPool.TinyPool.Start(onUserChange, new keyValue <list <Action <poll.message> >, poll.message>(onPolls.Key, new poll.message {
                 UserVersion = version, Users = onPolls.Value
             }));
         }
         remove(onPoll);
     }
 }
Exemple #10
0
        private registerResult register(clientId clientId, service service)
        {
            if (getClient(ref clientId) == null)
            {
                return new registerResult {
                           State = registerState.ClientError
                }
            }
            ;
            if (!service.Host.HostToIpAddress())
            {
                return new registerResult {
                           State = registerState.HostError
                }
            }
            ;
            services   services;
            hashString serviceName = service.Name;

            Monitor.Enter(serviceLock);
            try
            {
                if (serviceCache.TryGetValue(serviceName, out services))
                {
                    return(register(clientId.Identity, service, services, ref serviceName));
                }
                getPort(clientId.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] = clientId.Identity;
                services = new services {
                    Name = service.Name, Hosts = new host[] { service.Host }, IsSingle = service.IsSingle
                };
                serviceCache.Add(serviceName, services);
                appendLog(new log {
                    Type = log.type.HostChanged, Services = new services {
                        Name = service.Name, Hosts = new host[] { service.Host }
                    }
                });
            }
            finally { Monitor.Exit(serviceLock); }
            return(new registerResult {
                State = registerState.Success, Service = service
            });
        }
Exemple #11
0
        private void send(fastCSharp.net.tcp.commandServer.socket client, string message, string[] users)
        {
            list <userInfo> userInfos = new list <userInfo>(this.users.Count);
            string          user      = (string)client.ClientUserInfo;
            hashString      userHash  = user;

            Monitor.Enter(userLock);
            try
            {
                if (this.users.ContainsKey(userHash))
                {
                    if (users == null)
                    {
                        foreach (userInfo userInfo in this.users.Values)
                        {
                            if (userInfo.User != user && userInfo.OnMessage != null)
                            {
                                userInfos.Add(userInfo);
                            }
                        }
                    }
                    else
                    {
                        userInfo userInfo;
                        foreach (string receiveUser in users)
                        {
                            if (receiveUser != user && this.users.TryGetValue(receiveUser, out userInfo))
                            {
                                if (userInfo.OnMessage != null)
                                {
                                    userInfos.Add(userInfo);
                                }
                            }
                        }
                    }
                }
            }
            finally { Monitor.Exit(userLock); }
            if (OnMessage != null)
            {
                OnMessage(new message {
                    User = user, Time = date.NowSecond, Message = message
                });
            }
            if (userInfos.Count != 0)
            {
                if (newMessageHandle == null)
                {
                    newMessageHandle = newMessage;
                }
                task.Tiny.Add(newMessageHandle, new messageUsers {
                    Users = userInfos, Message = new message {
                        User = user, Time = date.NowSecond, Message = message
                    }
                });
            }
        }
Exemple #12
0
        /// <summary>
        /// 长轮询回调注册
        /// </summary>
        /// <param name="user">登录用户</param>
        /// <param name="version">登录用户版本信息</param>
        /// <param name="onPoll">长轮询回调委托</param>
        /// <returns>长轮询消息</returns>
        public static poll.message GetMessage(string user, int version, Action <poll.message> onPoll)
        {
            poll.message          message   = null;
            Action <poll.message> oldOnPoll = null;
            userInfo   userInfo             = null;
            hashString userHash             = user;
            bool       isTimeout            = false;

            Monitor.Enter(userLock);
            try
            {
                if (userInfos.TryGetValue(userHash, out userInfo))
                {
                    userInfo.LastTime = date.NowSecond;
                    if (version == userVersion)
                    {
                        oldOnPoll = userInfo.OnPoll;
                        if (userInfo.Messages.Count == 0)
                        {
                            userInfo.PollTimeout = date.NowSecond.AddSeconds(60);
                            userInfo.OnPoll      = onPoll;
                            isTimeout            = true;
                        }
                        else
                        {
                            message = new poll.message {
                                Messages = userInfo.Messages.GetArray()
                            };
                            userInfo.Messages.Clear();
                        }
                    }
                    else
                    {
                        message = new poll.message {
                            UserVersion = userVersion, Users = userInfos.Keys.getArray(value => value.ToString())
                        }
                    };
                }
                else
                {
                    message = chatWeb.poll.message.Null;
                }
            }
            finally { Monitor.Exit(userLock); }
            if (oldOnPoll != null)
            {
                oldOnPoll(null);
            }
            if (isTimeout)
            {
                threading.timerTask.Default.Add(pollTimeout, userInfo, userInfo.PollTimeout, null);
            }
            return(message);
        }
Exemple #13
0
 /// <summary>
 /// 打开数据库
 /// </summary>
 /// <param name="fileName">数据文件名</param>
 /// <returns>数据库物理层初始化信息</returns>
 internal physicalServer.physicalIdentity Open(string fileName)
 {
     physicalServer.physicalIdentity physicalInfo = new physicalServer.physicalIdentity {
         Identity = new physicalServer.timeIdentity {
             TimeTick = 0, Index = -1
         }
     };
     if (isDisposed == 0)
     {
         hashString key = fileName;
         if (physicalPool.Enter())
         {
             if (fileNameIndexs.ContainsKey(key))
             {
                 physicalPool.Exit();
             }
             else
             {
                 try
                 {
                     fileNameIndexs.Add(key, physicalInfo.Identity.Index = physicalPool.GetIndexContinue());
                 }
                 finally { physicalPool.Exit(); }
             }
             if (physicalInfo.Identity.Index != -1)
             {
                 try
                 {
                     physical physical = new physical(fileName, false);
                     if (!physical.IsDisposed)
                     {
                         if (physicalPool.Enter())
                         {
                             physicalPool.Pool[physicalInfo.Identity.Index].Set(fileName, physical);
                             physicalPool.Exit();
                         }
                         physicalInfo.Identity.Identity = physicalPool.Pool[physicalInfo.Identity.Index].Identity;
                         physicalInfo.Identity.TimeTick = fastCSharp.pub.StartTime.Ticks;
                         physicalInfo.IsLoader          = physical.IsLoader;
                     }
                 }
                 finally
                 {
                     if (physicalInfo.Identity.TimeTick == 0 && physicalPool.Enter())
                     {
                         fileNameIndexs.Remove(key);
                         physicalPool.Exit();
                     }
                 }
             }
         }
     }
     return(physicalInfo);
 }
Exemple #14
0
        /// <summary>
        /// 数据文本分词
        /// </summary>
        /// <param name="text">文本</param>
        /// <returns>分词结果</returns>
        protected unsafe Dictionary <hashString, list <int> > getAllWords(string text)
        {
            if (text != null)
            {
                int length = text.Length;
                if (length != 0)
                {
                    list <subString> words = getWords(text + " ", length);
                    if (words != null)
                    {
                        Dictionary <hashString, list <int> > dictionary = typePool <Dictionary <hashString, list <int> > > .Pop();

                        if (dictionary == null)
                        {
                            dictionary = fastCSharp.dictionary.CreateHashString <list <int> >();
                        }
                        else if (dictionary.Count != 0)
                        {
                            dictionary.Clear();
                        }
                        list <int> indexs;
                        int        count = words.Count;
                        foreach (subString word in words.UnsafeArray)
                        {
                            hashString wordKey = word;
                            if (!dictionary.TryGetValue(wordKey, out indexs))
                            {
                                indexs = typePool <list <int> > .Pop();

                                if (indexs == null)
                                {
                                    indexs = new list <int>();
                                }
                                else
                                {
                                    indexs.Empty();
                                }
                                dictionary.Add(wordKey, indexs);
                            }
                            indexs.Add(word.StartIndex);
                            if (--count == 0)
                            {
                                break;
                            }
                        }
                        words.Clear();
                        typePool <list <subString> > .PushNotNull(words);

                        return(dictionary);
                    }
                }
            }
            return(null);
        }
Exemple #15
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);
        }
Exemple #16
0
 /// <summary>
 /// 根据连接类型获取连接信息
 /// </summary>
 /// <param name="type">连接类型</param>
 /// <returns>连接信息</returns>
 public static connection GetConnection(string type)
 {
     if (type != null)
     {
         connection value;
         hashString key = type;
         if (!connections.TryGetValue(ref key, out value))
         {
             connections.Set(ref key, value = config.pub.LoadConfig(new connection(), type));
         }
         return(value);
     }
     return(null);
 }
Exemple #17
0
        private void receive(fastCSharp.net.tcp.commandServer.socket client, Func <returnValue <message>, bool> onMessage)
        {
            userInfo   userInfo;
            hashString user = (string)client.ClientUserInfo;

            Monitor.Enter(userLock);
            try
            {
                if (users.TryGetValue(user, out userInfo))
                {
                    userInfo.OnMessage = onMessage;
                }
            }
            finally { Monitor.Exit(userLock); }
        }
Exemple #18
0
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="user">用户名</param>
        /// <param name="version">用户版本</param>
        /// <returns>用户集合</returns>
        public string[] Login(string user, ref int version)
        {
            if (user.length() != 0)
            {
                keyValue <list <Action <poll.message> >, string[]> onPolls = default(keyValue <list <Action <poll.message> >, string[]>);
                userInfo userInfo = new userInfo(), removeUserInfo = null;
                Action <poll.message> onPoll = null;
                hashString            userHash = user;
                bool isLogin = false;
                Monitor.Enter(userLock);
                try
                {
                    if (userInfos.TryGetValue(userHash, out removeUserInfo))
                    {
                        if (removeUserInfo.LastTime.AddMinutes(2) <= date.NowSecond)
                        {
                            onPoll = removeUserInfo.OnPoll;
                            removeUserInfo.OnPoll = null;

                            isLogin             = true;
                            version             = ++userVersion;
                            userInfos[userHash] = userInfo;
                            onPolls             = getOnPolls(userInfo, true);
                        }
                    }
                    else
                    {
                        isLogin = true;
                        version = ++userVersion;
                        userInfos.Add(userHash, userInfo);
                        onPolls = getOnPolls(userInfo, true);
                    }
                }
                finally { Monitor.Exit(userLock); }
                if (onPolls.Key != null)
                {
                    threadPool.TinyPool.Start(onUserChange, new keyValue <list <Action <poll.message> >, poll.message>(onPolls.Key, new poll.message {
                        UserVersion = version, Users = onPolls.Value
                    }));
                }
                remove(onPoll);
                if (isLogin)
                {
                    return(onPolls.Value ?? nullValue <string> .Array);
                }
            }
            return(null);
        }
Exemple #19
0
 /// <summary>
 /// 等待检测链接
 /// </summary>
 /// <param name="assembly">程序集</param>
 public static void WaitCheckConnection(Assembly assembly)
 {
     if (assembly != null)
     {
         EventWaitHandle wait         = null;
         hashString      assemblyName = assembly.FullName;
         if (!checkAssemblyNames.TryGetValue(assemblyName, out wait))
         {
             checkAssemblyNames.Set(assemblyName, wait = new EventWaitHandle(false, EventResetMode.ManualReset));
         }
         if (wait != null)
         {
             wait.WaitOne();
         }
     }
 }
Exemple #20
0
            /// <summary>
            /// 绑定TCP调用客户端
            /// </summary>
            /// <param name="commandClient">TCP调用客户端</param>
            public void Register(commandClient commandClient)
            {
                hashString name = commandClient.ServiceName;

                Monitor.Enter(servicesLock);
                try
                {
                    if (!this.services.TryGetValue(name, out commandClient.TcpRegisterServices))
                    {
                        services.Add(name, commandClient.TcpRegisterServices = new services {
                            Hosts = nullValue <host> .Array
                        });
                    }
                    commandClient.TcpRegisterServices.AddClient(commandClient);
                }
                finally { Monitor.Exit(servicesLock); }
            }
Exemple #21
0
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <param name="user">消息发送者</param>
 /// <param name="message">聊天消息</param>
 /// <param name="users">接收用户列表</param>
 public void Send(string user, string message, string[] users)
 {
     if (user.length() != 0)
     {
         messagePoll messagePoll = new messagePoll {
             Message = new data.message {
                 User = user, Message = fastCSharp.web.formatHtml.Format(message)
             }, OnPolls = new list <Action <poll.message> >()
         };
         userInfo   currentUserInfo;
         hashString userHash = user;
         Monitor.Enter(userLock);
         try
         {
             if (userInfos.TryGetValue(userHash, out currentUserInfo))
             {
                 if (users.length() == 0)
                 {
                     foreach (userInfo userInfo in userInfos.Values)
                     {
                         if (userInfo != currentUserInfo)
                         {
                             messagePoll.Add(userInfo);
                         }
                     }
                 }
                 else
                 {
                     userInfo userInfo;
                     foreach (string name in users)
                     {
                         if (userInfos.TryGetValue(name, out userInfo))
                         {
                             messagePoll.Add(userInfo);
                         }
                     }
                 }
             }
         }
         finally { Monitor.Exit(userLock); }
         if (messagePoll.OnPolls.Count != 0)
         {
             threadPool.TinyPool.Start(messagePoll.Poll);
         }
     }
 }
Exemple #22
0
            /// <summary>
            /// 下一个函数处理
            /// </summary>
            /// <param name="methodIndex"></param>
            private void next(methodIndex methodIndex)
            {
                fastCSharp.code.cSharp.tcpMethod attribute = methodIndex.Attribute;
                server server = defaultServer;

                server.type serverType  = defaultType;
                string      serviceName = attribute.ServiceName;

                if (serviceName == null)
                {
                    serviceName = Attribute.ServiceName;
                }
                if (serviceName != defaultServiceName)
                {
                    if (serviceName == null)
                    {
                        serverType = null;
                    }
                    else
                    {
                        hashString nameKey = serviceName;
                        if (!servers.TryGetValue(nameKey, out server))
                        {
                            servers.Add(nameKey, server = new server());
                            server.TcpServer.Service    = serviceName;
                        }
                        if (!serverTypes.TryGetValue(nameKey, out serverType))
                        {
                            server.Types.Add(serverType = new server.type {
                                Type = type
                            });
                            serverTypes.Add(nameKey, serverType);
                        }
                    }
                }
                if (serverType != null)
                {
                    server.IsMethod = true;
                    methodIndex.ServiceAttribute = server.TcpServer;
                    //methodIndex.MethodIndex = server.MethodIndex++;
                    methodIndex.ParameterIndex = parameterIndex++;
                    serverType.Methods.Add(methodIndex);
                }
            }
Exemple #23
0
        ///// <summary>
        ///// 获取一个可用的集合索引
        ///// </summary>
        ///// <returns>集合索引</returns>
        //private int newIndex()
        //{
        //    if (freeIndexs.Count != 0) return freeIndexs.UnsafePop();
        //    if (maxIndex == physicals.Length)
        //    {
        //        physicalInfo[] newPhysicals = new physicalInfo[maxIndex << 1];
        //        Array.Copy(physicals, 0, newPhysicals, 0, maxIndex);
        //        physicals = newPhysicals;
        //    }
        //    return maxIndex++;
        //}
        /// <summary>
        /// 获取数据库物理层集合唯一标识
        /// </summary>
        /// <param name="fileName">数据文件名</param>
        /// <returns>数据库物理层集合唯一标识</returns>
        internal indexIdentity GetIdentity(string fileName)
        {
            int           index;
            indexIdentity identity = new indexIdentity {
                Index = indexIdentity.ErrorIndex
            };
            hashString key = fileName;

            if (fileNameIndexs.TryGetValue(key, out index))
            {
                identity.Identity = physicalPool.Pool[index].Identity;
                //identity.Identity = physicals[index].Identity;
                int nextIndex;
                if (fileNameIndexs.TryGetValue(key, out nextIndex) && index == nextIndex)
                {
                    identity.Index = index;
                }
            }
            return(identity);
        }
Exemple #24
0
        private void getUsers(fastCSharp.net.tcp.commandServer.socket client, Func <returnValue <string[]>, bool> onUserChanged)
        {
            string[]   users = null;
            hashString user  = (string)client.ClientUserInfo;

            Monitor.Enter(userLock);
            try
            {
                userInfo userInfo;
                if (this.users.TryGetValue(user, out userInfo))
                {
                    userInfo.OnUserChanged = onUserChanged;
                    users = this.users.Keys.getArray(value => value.ToString());
                }
            }
            finally
            {
                Monitor.Exit(userLock);
                onUserChanged(users);
            }
        }
Exemple #25
0
 private void removeRegister(clientId clientId, string serviceName)
 {
     if (getClient(ref clientId) != null)
     {
         services   services;
         hashString nameKey  = serviceName;
         bool       isRemove = false;
         Monitor.Enter(serviceLock);
         try
         {
             if (serviceCache.TryGetValue(nameKey, out services))
             {
                 isRemove = removeRegister(clientId.Identity, services, ref nameKey);
             }
         }
         finally { Monitor.Exit(serviceLock); }
         if (isRemove)
         {
             fastCSharp.log.Default.Add("TCP服务 " + serviceName + " 被注销", new System.Diagnostics.StackFrame(), false);
         }
     }
 }
Exemple #26
0
 /// <summary>
 /// 注销TCP服务信息
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="services"></param>
 /// <param name="serviceName"></param>
 /// <returns></returns>
 private bool removeRegister(indexIdentity identity, services services, ref hashString serviceName)
 {
     services.RemovePerpService(identity);
     if (removeRegister(identity, services))
     {
         if (services.Hosts.Length == 0)
         {
             services.IsSingle = false;
             foreach (keyValue <indexIdentity, service> perpService in services.GetPerpServices())
             {
                 if (register(perpService.Key, perpService.Value, services, ref serviceName).State != registerState.Success)
                 {
                     break;
                 }
             }
             if (services.Hosts.Length == 0)
             {
                 serviceCache.Remove(serviceName);
                 appendLog(new log {
                     Type = log.type.RemoveServiceName, Services = new services {
                         Name = services.Name
                     }
                 });
             }
             return(true);
         }
         else
         {
             appendLog(new log {
                 Type = log.type.HostChanged, Services = new services {
                     Name = services.Name, Hosts = services.Hosts.copy()
                 }
             });
         }
     }
     return(false);
 }
Exemple #27
0
 /// <summary>
 /// 添加监视路径
 /// </summary>
 /// <param name="path">监视路径</param>
 public void Add(string path)
 {
     if (isDisposed == 0)
     {
         path = fastCSharp.io.file.FileNameToLower(path);
         counter    counter;
         hashString pathKey = path;
         Monitor.Enter(watcherLock);
         try
         {
             if (watchers.TryGetValue(pathKey, out counter))
             {
                 ++counter.Count;
                 watchers[pathKey] = counter;
             }
             else
             {
                 counter.Create(path, onCreatedHandle);
                 watchers.Add(pathKey, counter);
             }
         }
         finally { Monitor.Exit(watcherLock); }
     }
 }
Exemple #28
0
 /// <summary>
 /// 获取TCP注册服务客户端
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <returns>TCP注册服务客户端,失败返回null</returns>
 public static client Get(string serviceName)
 {
     if (!string.IsNullOrEmpty(serviceName))
     {
         int        count   = int.MinValue;
         client     client  = null;
         hashString nameKey = serviceName;
         Monitor.Enter(clientsLock);
         try
         {
             if (clients != null && !clients.TryGetValue(nameKey, out client))
             {
                 try
                 {
                     client = new client(serviceName);
                 }
                 catch (Exception error)
                 {
                     fastCSharp.log.Error.Add(error, null, false);
                 }
                 if (client != null)
                 {
                     count = clients.Count;
                     clients.Add(nameKey, client);
                 }
             }
         }
         finally { Monitor.Exit(clientsLock); }
         if (count == 0)
         {
             fastCSharp.domainUnload.Add(null, domainUnload.unloadType.TcpRegisterClientDispose);
         }
         return(client);
     }
     return(null);
 }
Exemple #29
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
            });
        }
Exemple #30
0
 /// <summary>
 /// 安装下一个类型
 /// </summary>
 protected override void nextCreate()
 {
     if (Attribute.IsAbstract || type.Type.IsSealed || !type.Type.IsAbstract)
     {
         defaultServiceName = Attribute.ServiceName;
         defaultServer      = null;
         defaultType        = null;
         if (defaultServiceName != null)
         {
             hashString nameKey = defaultServiceName;
             if (!servers.TryGetValue(nameKey, out defaultServer))
             {
                 servers.Add(nameKey, defaultServer = new server());
             }
             defaultServer.TcpServer.Service     = defaultServiceName;
             defaultServer.Types.Add(defaultType = new server.type {
                 Type = type, Attribute = Attribute
             });
             if (Attribute.IsServer)
             {
                 defaultServer.AttributeType = type;
                 defaultServer.TcpServer.CopyFrom(Attribute);
             }
         }
         foreach (methodInfo method in code.methodInfo.GetMethods <fastCSharp.code.cSharp.tcpMethod>(type, Attribute.MemberFilter, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute, Attribute.IsInheritAttribute))
         {
             next(new methodIndex
             {
                 Method     = method,
                 MethodType = type,
                 IsTypeGenericParameterName = type.Type.IsGenericType
             });
         }
         if (!type.Type.IsGenericType)
         {
             foreach (memberIndex member in code.memberIndexGroup.GetStatic <fastCSharp.code.cSharp.tcpMethod>(type, Attribute.MemberFilter, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute, Attribute.IsInheritAttribute))
             {
                 if (member.IsField)
                 {
                     FieldInfo   field     = (FieldInfo)member.Member;
                     methodIndex getMethod = new methodIndex
                     {
                         Method      = new methodInfo(field, true),
                         MemberIndex = member,
                         MethodType  = type
                     };
                     if (!getMethod.Attribute.IsOnlyGetMember)
                     {
                         getMethod.SetMethod = new methodIndex {
                             Method = new methodInfo(field, false), MemberIndex = member, MethodType = type
                         };
                     }
                     next(getMethod);
                     if (getMethod.SetMethod != null)
                     {
                         next(getMethod.SetMethod);
                     }
                 }
                 else if (member.CanGet)
                 {
                     PropertyInfo property  = (PropertyInfo)member.Member;
                     methodIndex  getMethod = new methodIndex
                     {
                         Method      = new methodInfo(property, true),
                         MemberIndex = member,
                         MethodType  = type
                     };
                     if (member.CanSet && !getMethod.Attribute.IsOnlyGetMember)
                     {
                         getMethod.SetMethod = new methodIndex {
                             Method = new methodInfo(property, false), MemberIndex = member, MethodType = type
                         };
                     }
                     next(getMethod);
                     if (getMethod.SetMethod != null)
                     {
                         next(getMethod.SetMethod);
                     }
                 }
             }
         }
         serverTypes.Clear();
     }
 }
Exemple #31
0
 /// <summary>
 /// 检测缓存是否存在
 /// </summary>
 /// <param name="value">日志信息</param>
 /// <param name="isQueue">是否缓存队列</param>
 /// <returns>是否继续输出日志</returns>
 private bool CheckCache(DebugPlus value, bool isQueue)
 {
     hashString key = value.ToString();
     if (isQueue)
     {
         interlocked.NoCheckCompareSetSleep0(ref _cacheLock);
         try
         {
             if (_cache.Get(key, false)) return false;
             _cache.Set(key, true);
             if (_cache.Count > _maxCacheCount) _cache.Pop();
         }
         finally { _cacheLock = 0; }
         return true;
     }
     if (key.Equals(_lastCache)) return false;
     _lastCache = key;
     return true;
 }