Exemplo n.º 1
0
        public void ExecuteCommand(FastSocket.SocketBase.IConnection connection, AsyncBinaryCommandInfo commandInfo)
        {
            if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0)
            {
                Console.WriteLine("BrokenPull参数为空");
                connection.BeginDisconnect();
                return;
            }

            var    topic        = SerializeMemoryHelper.DeserializeFromBinary(commandInfo.Buffer).ToString();
            string topicQueueId = null;
            Action after        = null;
            var    offset       = BrokerManager.GetConsumerQueueOffset(connection, topic, ref topicQueueId, ref after);
            var    entity       = BrokerManager.Pull(connection, topic, topicQueueId, offset);

            if (entity == null)
            {
                Console.WriteLine("服务端消息已被消费完!");
                commandInfo.Reply(connection, null);//返回到客户端..
            }
            else
            {
                if (after != null)
                {
                    after();
                }
                Console.WriteLine("向客户端返回消息对象:{0},IP:{1},Port:{2},connId:{3}", entity.ToString(), connection.RemoteEndPoint.Address.Address, connection.RemoteEndPoint.Port, connection.ConnectionID);
                commandInfo.Reply(connection, SerializeMemoryHelper.SerializeToBinary(entity));//返回到客户端..
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 返回当前消费者的消息偏移量
        /// </summary>
        /// <param name="connection">消费者连接</param>
        /// <param name="topic">主题</param>
        /// <param name="topicQueueId">当前队列</param>
        /// <param name="after">回调</param>
        /// <returns></returns>
        internal static int GetConsumerQueueOffset(
            FastSocket.SocketBase.IConnection connection,
            string topic,
            ref string topicQueueId,
            ref Action after)
        {
            int offset    = 0;
            var queueList = RedisManager.Instance.GetDatabase().SetMembers(GetRedisKey(topic));

            topicQueueId = queueList.OrderByNewId().FirstOrDefault().ToString();
            if (topicQueueId == null)
            {
                return(0);
            }

            //消费者标识

            string connectionId = GetRedisKey(topicQueueId + "_" + GetIpLong(connection));

            if (!RedisManager.Instance.GetDatabase().HashExists(QUEUEOFFSETKEY, connectionId))
            {
                RedisManager.Instance.GetDatabase().HashSet(QUEUEOFFSETKEY, connectionId, 1);
            }

            //业务层的回调,算出当前队列的偏移量
            after = () =>
            {
                //更新消费端的消费量
                RedisManager.Instance.GetDatabase().HashIncrement(QUEUEOFFSETKEY, connectionId, 1);
            };

            return(offset);
        }
Exemplo n.º 3
0
        public void ExecuteCommand(FastSocket.SocketBase.IConnection connection, AsyncBinaryCommandInfo commandInfo)
        {
            if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0)
            {
                Console.WriteLine("BrokenPush参数为空");
                connection.BeginDisconnect();
                return;
            }

            var message = SerializeMemoryHelper.DeserializeFromBinary(commandInfo.Buffer) as MessageBody;

            try
            {
                BrokerManager.Push(message);

                string result = string.Format("消息成功加入队列,Topic:{0},QueueId:{1},QueueCount:{2}",
                                              message.Topic,
                                              message.QueueId,
                                              message.QueueOffset);
                Console.WriteLine(result);
                commandInfo.Reply(connection, SerializeMemoryHelper.SerializeToBinary("OK"));//返回到客户端..
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 拉出来消息后,处理消息
        /// </summary>
        /// <param name="connection">当前连接</param>
        /// <param name="topic">主题</param>
        /// <param name="topicQueueId">指定队列</param>
        /// <param name="aciton">处理程序</param>
        /// <param name="offset">偏移量</param>
        internal static void Pull(FastSocket.SocketBase.IConnection connection, string topic, string topicQueueId, Action <MessageBody> aciton, int offset = 0)
        {
            var entity = Pull(connection, topic, topicQueueId, offset);

            if (entity != null)
            {
                aciton(entity);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 拉出消息
        /// </summary>
        /// <param name="connection">当前连接</param>
        /// <param name="topic">主题</param>
        /// <param name="topicQueueId">队列</param>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        internal static MessageBody Pull(FastSocket.SocketBase.IConnection connection, string topic, string topicQueueId, int offset = 0)
        {
            if (topicQueueId == null)
            {
                return(null);
            }

            //通过消费进度,拿指定进度的消息
            var entity = RedisManager.Instance.GetDatabase().SortedSetRangeByScore(GetRedisDataKey(topicQueueId), start: offset).FirstOrDefault();

            if (!entity.HasValue)
            {
                //当前客户端已经消费完成
                return(null);
            }
            //消费日志
            RedisManager.Instance.GetDatabase().SetAdd(GetRedisKey(topicQueueId + "_" + GetIpLong(connection)), offset);
            return(Utils.SerializeMemoryHelper.DeserializeFromJson <MessageBody>(entity));
        }
Exemplo n.º 6
0
 protected override void HandleUnKnowCommand(FastSocket.SocketBase.IConnection connection, AsyncBinaryCommandInfo commandInfo)
 {
     Console.WriteLine("命令没有被识别");
 }
Exemplo n.º 7
0
 public override void OnConnected(FastSocket.SocketBase.IConnection connection)
 {
     base.OnConnected(connection);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 返回IP地址的长整型格式
 /// </summary>
 /// <param name="connection"></param>
 /// <returns></returns>
 static long GetIpLong(FastSocket.SocketBase.IConnection connection)
 {
     return(System.BitConverter.ToInt64(connection.RemoteEndPoint.Address.GetAddressBytes(), 0));
 }