Esempio n. 1
0
        /// <summary>
        /// Send a message to a specific client
        /// </summary>
        /// <param name="clientPublicToken"></param>
        /// <param name="name"></param>
        /// <param name="contents"></param>
        public void SendMessage(string clientPublicToken, string tip, object content)
        {
            if (string.IsNullOrEmpty(clientPublicToken))
            {
                throw new ArgumentNullException("clientPublicToken");
            }
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            lock (state)
            {
                if (!publicClients.ContainsKey(clientPublicToken))
                {
                    throw CometException.CometClientDoesNotExistException();
                }

                InProcCometClient cometClient = publicClients[clientPublicToken];

                CometMessage message = new CometMessage();

                message.Content   = content;
                message.Tip       = tip;
                message.MessageId = cometClient.NextMessageId;

                cometClient.NextMessageId++;
                cometClient.Messages.Add(message.MessageId, message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get the client from the state provider
        /// </summary>
        /// <param name="clientPrivateToken"></param>
        /// <returns></returns>
        public CometClient GetCometClient(string clientPrivateToken)
        {
            //  Debug.WriteLine("目前有几个客户端连接"+this.publicClients.Count);

            if (!this.privateClients.ContainsKey(clientPrivateToken))
            {
                throw CometException.CometClientDoesNotExistException();
            }

            //  return the client private token
            return(this.privateClients[clientPrivateToken].CometClient);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="callback"></param>
        /// <param name="extraData"></param>
        /// <returns></returns>
        public IAsyncResult BeginSubscribe(HttpContext context, AsyncCallback callback, object extraData)
        {
            try
            {
                long   lastMessageId;
                string privateToken = string.Empty;

                if (!long.TryParse(context.Request["lastMessageId"] ?? "-1", out lastMessageId))
                {
                    throw CometException.CometHandlerParametersAreInvalidException();
                }

                privateToken = context.Request["privateToken"];
                if (string.IsNullOrEmpty(privateToken))
                {
                    throw CometException.CometHandlerParametersAreInvalidException();
                }


                this.DebugWriteThreadInfo("BeginSubscribe");

                lock (state)
                {
                    Debug.WriteLine("目前有几个客户端连接" + this.GetCometClients());

                    CometClient cometClient = this.GetCometClient(privateToken);
                    this.FireClientSubscribed(cometClient);
                    // 从所有线程的请求队列中找到该CometClient,将其排除
                    for (int i = 0; i < this.workerThreadCount; i++)
                    {
                        this.workerThreads[i].DequeueCometWaitRequest(privateToken);
                    }

                    CometWaitRequest request = new CometWaitRequest(privateToken, lastMessageId, context, callback, extraData);

                    this.workerThreads[this.currentThread].QueueCometWaitRequest(request);
                    this.currentThread++;

                    if (this.currentThread >= this.workerThreadCount)
                    {
                        this.currentThread = 0;
                    }

                    return(request.Result);
                }
            }
            catch (Exception ex)
            {
                this.WriteErrorToResponse(context, ex.Message);
                return(null);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Remove an idle client from the memory
        /// </summary>
        /// <param name="clientPrivateToken"></param>
        public void KillIdleCometClient(string clientPrivateToken)
        {
            if (!this.privateClients.ContainsKey(clientPrivateToken))
            {
                throw CometException.CometClientDoesNotExistException();
            }

            //  get the client
            InProcCometClient ipCometClient = this.privateClients[clientPrivateToken];

            //  and remove the dictionarys
            this.privateClients.Remove(ipCometClient.CometClient.PrivateToken);
            this.publicClients.Remove(ipCometClient.CometClient.PublicToken);
            Debug.WriteLine(ipCometClient.CometClient.PublicToken + " has  been removed  from  memmory.");
        }
Esempio n. 5
0
        public CometMessage[] GetMessages(string clientPrivateToken, long lastMessageId)
        {
            if (string.IsNullOrEmpty(clientPrivateToken))
            {
                throw new ArgumentNullException("clientPrivateToken");
            }

            lock (state)
            {
                if (!privateClients.ContainsKey(clientPrivateToken))
                {
                    throw CometException.CometClientDoesNotExistException();
                }

                InProcCometClient cometClient = privateClients[clientPrivateToken];

                List <long> toDelete = new List <long>();
                List <long> toReturn = new List <long>();

                // 根据lastMessageId 机制可以得到离线消息
                foreach (long key in cometClient.Messages.Keys)
                {
                    if (key <= lastMessageId)
                    {
                        toDelete.Add(key);
                    }
                    else
                    {
                        toReturn.Add(key);
                    }
                }

                foreach (long key in toDelete)
                {
                    cometClient.Messages.Remove(key);
                }

                List <CometMessage> cometMessages = new List <CometMessage>();
                foreach (long key in toReturn)
                {
                    cometMessages.Add(cometClient.Messages[key]);
                }

                return(cometMessages.ToArray());
            }
        }
Esempio n. 6
0
        public void InitializeClient(CometClient cometClient)
        {
            if (cometClient == null)
            {
                throw new ArgumentNullException(nameof(cometClient));
            }

            lock (state)
            {
                if (publicClients.ContainsKey(cometClient.PublicToken) || privateClients.ContainsKey(cometClient.PrivateToken))
                {
                    throw CometException.CometClientAlreadyExistsException();
                }

                var inProcCometClient = new InProcCometClient()
                {
                    CometClient = cometClient
                };
                publicClients.Add(cometClient.PublicToken, inProcCometClient);
                privateClients.Add(cometClient.PrivateToken, inProcCometClient);
            }
        }