Ejemplo n.º 1
0
        public void pull_once()
        {
            if (!isConnected)
            {
                return;
            }

            PullReq pull_req = new PullReq();

            if (pull_id == int.MaxValue)
            {
                pull_id = 1;
            }
            else
            {
                pull_id++;
            }

            pull_req.id = pull_id;

            _logger.Trace("Pulling data: " + pull_id);

            TransferPkg pkg = build_TransferPkg((int)CmdId.PullReq, ProtoBufEncoder.SerializeToBytes(pull_req));

            TransferPkg pull_res = send_and_recv(pkg, true);

            if (pull_res == null)
            {
                _logger.Error("Pull failed, received unexpected response, needed PullRsp, received nothing.");
                reconnect();
                return;
            }
            if (pull_res.cmdId == (int)CmdId.PullRsp)
            {
                next_ping_deadline = DateTime.Now.AddSeconds(ping_interval_in_sec);
                PullRsp rsp = ProtoBufDecoder.DeserializeToObj <PullRsp>(pull_res.data);

                int message_count = rsp.msgDTO.messages.Count;
                if (message_count > 0)
                {
                    _logger.Info("Got " + message_count.ToString() + " message(s).");
                }
                foreach (var message in rsp.msgDTO.messages)
                {
                    //if (message_count < 5)
                    //{
                    //    _logger.Info(string.Format("Got message, key: {0}, partition: {1}, offset: {2}, topic: {3}, value: {4}", message.key, message.partition, message.offset, message.topic, message.value));
                    //}
                    //else
                    //{
                    //    _logger.Trace(string.Format("Got message, key: {0}, partition: {1}, offset: {2}, topic: {3}, value: {4}", message.key, message.partition, message.offset, message.topic, message.value));
                    //}
                    queue.Add(message);
                }
            }
            else
            {
                _logger.Error("Pull failed, received unexpected response, needed PullRsp, received " + pull_res.cmdId);
            }
        }
Ejemplo n.º 2
0
        void sub()
        {
            SubReq sub_req = new SubReq();

            sub_req.category      = subType;
            sub_req.clientId      = ((IPEndPoint)(clientSocket.Client.LocalEndPoint)).Address.ToString();
            sub_req.subId         = sub_id;
            sub_req.accessKey     = accessKey;
            sub_req.consumerGroup = consumer_group;

            TransferPkg pkg = build_TransferPkg((int)CmdId.SubReq, ProtoBufEncoder.SerializeToBytes(sub_req));

            TransferPkg sub_res = send_and_recv(pkg);

            //check if subscription is successful
            if (sub_res == null)
            {
                _logger.Error("Subscription failed, receive unexpected response, needed SubRsp, received nothing.");
                reconnect();
                return;
            }
            if (sub_res.cmdId == (int)CmdId.SubRsp)
            {
                SubRsp rsp = ProtoBufDecoder.DeserializeToObj <SubRsp>(sub_res.data);
                if (rsp.ack == 0)
                {
                    isConnected = true;
                    //new thread to ping server to keep it alive
                    next_ping_deadline = DateTime.Now.AddSeconds(ping_interval_in_sec);
                    LifeKeeper lk          = new LifeKeeper();
                    Thread     life_keeper = new Thread(() => lk.Run(this));
                    life_keeper.Start();
                    //new thread to run fetching
                    SubFetcher fetcher     = new SubFetcher();
                    Thread     fetchthread = new Thread(() => fetcher.Run(this));
                    fetchthread.Start();
                }
                else
                {
                    _logger.Error("Subscription failed, sub info:" + sub_req.ToString());
                    reconnect();
                }
            }
            else
            {
                _logger.Error("Subscription failed, receive unexpected response, needed SubRsp, received:" + sub_res.cmdId);
                reconnect();
            }
        }
Ejemplo n.º 3
0
        void auth()
        {
            AuthReq auth_req = new AuthReq();

            auth_req.accessKey = accessKey;
            auth_req.subId     = sub_id;
            auth_req.sign      = Encryptor.GetHashSha256(accessKey, sub_id, accessSecret);
            auth_req.subType   = subType;

            TransferPkg pkg = build_TransferPkg((int)CmdId.AuthReq, ProtoBufEncoder.SerializeToBytes(auth_req));

            TransferPkg auth_res = send_and_recv(pkg);

            if (auth_res == null)
            {
                _logger.Error("Authentication failed, received unexpected response, needed AuthRsp, received nothing.");
                reconnect();
                return;
            }
            //check if auth is successful
            if (auth_res.cmdId == (int)CmdId.AuthRsp)
            {
                AuthRsp rsp = ProtoBufDecoder.DeserializeToObj <AuthRsp>(auth_res.data);
                if (rsp.ack == 0)
                {
                    _logger.Info("Authenticated successfully.");
                    //subscribe
                    sub();
                }
                else
                {
                    throw new Exception("Authentication failed, auth info:" + auth_req.ToString());
                }
            }
            else
            {
                _logger.Error("Authentication failed, received unexpected response, needed AuthRsp, received:" + auth_res.cmdId);
            }
        }