Exemplo n.º 1
0
        TransferPkg send_and_recv(TransferPkg pkg, bool is_pull_req = false, bool need_recv = true)
        {
            lock (lockobj)
            {
                try
                {
                    //write into stream
                    Serializer.SerializeWithLengthPrefix(stream, pkg, PrefixStyle.Base128);
                    if (need_recv)
                    {
                        //get response
                        return(Serializer.DeserializeWithLengthPrefix <TransferPkg>(stream, PrefixStyle.Base128));
                    }
                    else
                    {
                        return(new TransferPkg {
                            cmdId = -3
                        });
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Error sending and receiving message: " + ex.Message);
                    reconnect();
                }

                return(new TransferPkg {
                    cmdId = -2
                });
            }
        }
Exemplo n.º 2
0
        public void commit_offsets(CommitDTO commit_dto)
        {
            if (!isConnected || commit_dto == null || commit_dto.commits.Count == 0)
            {
                return;
            }

            _logger.Info("committing offset...");

            foreach (var commit in commit_dto.commits)
            {
                _logger.Trace(string.Format("committing topic: {0}, partition: {1}, offset: {2}  ", commit.topic, commit.partition, commit.offset));
            }

            TransferPkg pkg = build_TransferPkg((int)CmdId.CommitReq, ProtoBufEncoder.SerializeToBytes(commit_dto));

            TransferPkg commit_res = send_and_recv(pkg, false, false);

            if (commit_res == null)
            {
                _logger.Error("Commit failed, received unexpected response, needed CommitRsp, received nothing.");
                reconnect();
                return;
            }
            if (commit_res.cmdId != -3)
            {
                _logger.Error("Error occured commiting offset, reconnecting...");
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        TransferPkg build_TransferPkg(int message_map, byte[] data)
        {
            TransferPkg transfer_pkg = new TransferPkg();

            transfer_pkg.seqId = 0;
            transfer_pkg.cmdId = message_map;
            transfer_pkg.data  = data;
            transfer_pkg.zip   = false;
            transfer_pkg.ver   = 0;

            return(transfer_pkg);
        }
Exemplo n.º 5
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();
            }
        }
Exemplo n.º 6
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);
            }
        }