remove dependency on APR library
This commit is contained in:
@@ -26,10 +26,11 @@
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
#include "base/logging.h"
|
||||
#include "base/network_util.h"
|
||||
#include "base/network/network_util.h"
|
||||
#include "command_impl.h"
|
||||
#include "device_manager.h"
|
||||
#include "livox_def.h"
|
||||
#include <stdio.h>
|
||||
|
||||
using std::bind;
|
||||
using std::list;
|
||||
@@ -41,49 +42,46 @@ using std::chrono::steady_clock;
|
||||
|
||||
namespace livox {
|
||||
|
||||
CommandChannel::CommandChannel(apr_port_t port,
|
||||
CommandChannel::CommandChannel(uint16_t port,
|
||||
uint8_t handle,
|
||||
const string &remote_ip,
|
||||
CommandChannelDelegate *cb,
|
||||
apr_pool_t *pool)
|
||||
CommandChannelDelegate *cb)
|
||||
: handle_(handle),
|
||||
port_(port),
|
||||
sock_(NULL),
|
||||
mem_pool_(pool),
|
||||
loop_(NULL),
|
||||
loop_(),
|
||||
callback_(cb),
|
||||
comm_port_(new CommPort),
|
||||
heartbeat_time_(),
|
||||
remote_ip_(remote_ip),
|
||||
heartbeat_time_(),
|
||||
last_heartbeat_() {}
|
||||
|
||||
bool CommandChannel::Bind(IOLoop *loop) {
|
||||
if (loop == NULL) {
|
||||
|
||||
bool CommandChannel::Bind(std::weak_ptr<IOLoop> loop) {
|
||||
if (loop.expired()) {
|
||||
return false;
|
||||
}
|
||||
loop_ = loop;
|
||||
sock_ = util::CreateBindSocket(port_, mem_pool_);
|
||||
if (sock_ == NULL) {
|
||||
sock_ = util::CreateSocket(port_);
|
||||
if (sock_ == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
loop_->AddDelegate(sock_, this);
|
||||
loop_.lock()->AddDelegate(sock_, this);
|
||||
last_heartbeat_ = steady_clock::now();
|
||||
return true;
|
||||
}
|
||||
|
||||
void CommandChannel::OnData(apr_socket_t *, void *) {
|
||||
apr_sockaddr_t addr;
|
||||
void CommandChannel::OnData(socket_t , void *) {
|
||||
struct sockaddr addr;
|
||||
int addrlen = sizeof(addr);
|
||||
uint32_t buf_size = 0;
|
||||
uint8_t *cache_buf = comm_port_->FetchCacheFreeSpace(&buf_size);
|
||||
apr_size_t size = buf_size;
|
||||
apr_status_t rv = apr_socket_recvfrom(&addr, sock_, 0, reinterpret_cast<char *>(cache_buf), &size);
|
||||
comm_port_->UpdateCacheWrIdx(size);
|
||||
if (rv != APR_SUCCESS) {
|
||||
LOG_ERROR(PrintAPRStatus(rv));
|
||||
int size = buf_size;
|
||||
size = util::RecvFrom(sock_, reinterpret_cast<char *>(cache_buf), buf_size, 0, &addr, &addrlen);
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
comm_port_->UpdateCacheWrIdx(size);
|
||||
CommPacket packet;
|
||||
memset(&packet, 0, sizeof(packet));
|
||||
|
||||
@@ -115,8 +113,14 @@ void CommandChannel::OnData(apr_socket_t *, void *) {
|
||||
|
||||
void CommandChannel::SendAsync(const Command &command) {
|
||||
Command cmd = DeepCopy(command);
|
||||
if (loop_) {
|
||||
loop_->PostTask(bind(&CommandChannel::Send, this, cmd));
|
||||
if (!loop_.expired()) {
|
||||
auto w_ptr = WeakProtector(protector_);
|
||||
loop_.lock()->PostTask([this, w_ptr, cmd](){
|
||||
if(w_ptr.expired()) {
|
||||
return;
|
||||
}
|
||||
Send(cmd);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +138,7 @@ void CommandChannel::OnTimer(TimePoint now) {
|
||||
}
|
||||
|
||||
for (list<Command>::iterator ite = timeout_commands.begin(); ite != timeout_commands.end(); ++ite) {
|
||||
LOG_WARN("Command Timeout: Set {}, Id {}, Seq {}",
|
||||
LOG_WARN("Command Timeout: Set {}, Id {}, Seq {}",
|
||||
(uint16_t)ite->packet.cmd_set, ite->packet.cmd_code, ite->packet.seq_num);
|
||||
if (callback_) {
|
||||
ite->packet.packet_type = kCommandTypeAck;
|
||||
@@ -150,16 +154,12 @@ void CommandChannel::OnTimer(TimePoint now) {
|
||||
}
|
||||
|
||||
void CommandChannel::Uninit() {
|
||||
if (sock_) {
|
||||
apr_os_thread_t thread_id = apr_os_thread_current();
|
||||
if (apr_os_thread_equal(loop_->GetThreadId(), thread_id)) {
|
||||
loop_->RemoveDelegateSync(sock_);
|
||||
} else {
|
||||
loop_->RemoveDelegate(sock_, this);
|
||||
if (sock_ != -1) {
|
||||
if (!loop_.expired()) {
|
||||
loop_.lock()->RemoveDelegate(sock_, this);
|
||||
}
|
||||
loop_ = NULL;
|
||||
apr_socket_close(sock_);
|
||||
sock_ = NULL;
|
||||
util::CloseSock(sock_);
|
||||
sock_ = -1;
|
||||
}
|
||||
callback_ = NULL;
|
||||
if (comm_port_) {
|
||||
@@ -190,22 +190,22 @@ void CommandChannel::HeartBeat(TimePoint t) {
|
||||
}
|
||||
|
||||
void CommandChannel::SendInternal(const Command &command) {
|
||||
apr_pool_t *subpool = NULL;
|
||||
apr_pool_create(&subpool, mem_pool_);
|
||||
uint8_t *buf = (uint8_t *)apr_palloc(subpool, kMaxCommandBufferSize);
|
||||
uint32_t size = 0;
|
||||
comm_port_->Pack(buf, kMaxCommandBufferSize, &size, command.packet);
|
||||
apr_size_t apr_size = size;
|
||||
apr_status_t rv = APR_SUCCESS;
|
||||
apr_sockaddr_t *sa = NULL;
|
||||
rv = apr_sockaddr_info_get(&sa, remote_ip_.c_str(), APR_INET, 65000, 0, subpool);
|
||||
if (rv == APR_SUCCESS) {
|
||||
rv = apr_socket_sendto(sock_, sa, 0, (const char *)buf, &apr_size);
|
||||
std::vector<uint8_t> buf(kMaxCommandBufferSize + 1);
|
||||
int size = 0;
|
||||
comm_port_->Pack(buf.data(), kMaxCommandBufferSize, (uint32_t *)&size, command.packet);
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(65000);
|
||||
servaddr.sin_addr.s_addr = inet_addr(remote_ip_.c_str());
|
||||
|
||||
int byte_send = sendto(sock_, (const char*)buf.data(), size, 0, (const struct sockaddr *) &servaddr,
|
||||
sizeof(servaddr));
|
||||
if (byte_send < 0) {
|
||||
if (command.cb) {
|
||||
(*command.cb)(kStatusSendFailed, handle_, NULL);
|
||||
}
|
||||
}
|
||||
if (rv != APR_SUCCESS) {
|
||||
(*command.cb)(kStatusSendFailed, handle_, NULL);
|
||||
}
|
||||
apr_pool_destroy(subpool);
|
||||
}
|
||||
|
||||
uint16_t CommandChannel::GenerateSeq() {
|
||||
|
||||
@@ -28,8 +28,7 @@
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include "apr_network_io.h"
|
||||
#include <algorithm>
|
||||
#include "base/io_loop.h"
|
||||
#include "comm/comm_port.h"
|
||||
|
||||
@@ -67,18 +66,19 @@ class CommandChannelDelegate {
|
||||
virtual void OnHeartbeatStateUpdate(uint8_t handle, const HeartbeatResponse &state) = 0;
|
||||
};
|
||||
|
||||
class Protector {};
|
||||
|
||||
/**
|
||||
* CommandChannel implements the sending/receiving commands with a specific device.
|
||||
*/
|
||||
class CommandChannel : public IOLoop::IOLoopDelegate {
|
||||
public:
|
||||
typedef std::chrono::steady_clock::time_point TimePoint;
|
||||
|
||||
CommandChannel(apr_port_t port,
|
||||
|
||||
CommandChannel(uint16_t port,
|
||||
uint8_t handle,
|
||||
const std::string &remote_ip,
|
||||
CommandChannelDelegate *cb,
|
||||
apr_pool_t *pool);
|
||||
CommandChannelDelegate *cb);
|
||||
virtual ~CommandChannel() { Uninit(); }
|
||||
|
||||
/** Uninitialize CommandChannel. */
|
||||
@@ -89,7 +89,7 @@ class CommandChannel : public IOLoop::IOLoopDelegate {
|
||||
* @param loop the IOLoop to bind.
|
||||
* @return true on successfully.
|
||||
*/
|
||||
bool Bind(IOLoop *loop);
|
||||
bool Bind(std::weak_ptr<IOLoop> loop);
|
||||
|
||||
/**
|
||||
* Send a command asynchronously.
|
||||
@@ -97,7 +97,7 @@ class CommandChannel : public IOLoop::IOLoopDelegate {
|
||||
*/
|
||||
void SendAsync(const Command &command);
|
||||
|
||||
void OnData(apr_socket_t *, void *);
|
||||
void OnData(socket_t, void *);
|
||||
void OnTimer(TimePoint now);
|
||||
|
||||
static uint16_t GenerateSeq();
|
||||
@@ -113,16 +113,18 @@ class CommandChannel : public IOLoop::IOLoopDelegate {
|
||||
private:
|
||||
const std::chrono::milliseconds kHeartbeatTimer = std::chrono::milliseconds(800);
|
||||
uint8_t handle_;
|
||||
apr_port_t port_;
|
||||
apr_socket_t *sock_;
|
||||
apr_pool_t *mem_pool_;
|
||||
IOLoop *loop_;
|
||||
uint16_t port_;
|
||||
socket_t sock_ = -1;
|
||||
std::weak_ptr<IOLoop> loop_;
|
||||
CommandChannelDelegate *callback_;
|
||||
std::map<uint16_t, std::pair<Command, TimePoint> > commands_;
|
||||
std::unique_ptr<CommPort> comm_port_;
|
||||
TimePoint heartbeat_time_;
|
||||
std::string remote_ip_;
|
||||
TimePoint heartbeat_time_;
|
||||
TimePoint last_heartbeat_;
|
||||
using SharedProtecotr = std::shared_ptr<Protector>;
|
||||
using WeakProtector = std::weak_ptr<Protector>;
|
||||
SharedProtecotr protector_ = std::make_shared<Protector>();
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
@@ -90,9 +90,9 @@ bool CommandHandler::AddDevice(const DeviceInfo &info) {
|
||||
if (impl_ == NULL) {
|
||||
DeviceMode mode = static_cast<DeviceMode>(device_manager().device_mode());
|
||||
if (mode == kDeviceModeHub) {
|
||||
impl_.reset(new HubCommandHandlerImpl(this, mem_pool_, loop_));
|
||||
impl_.reset(new HubCommandHandlerImpl(this, loop_));
|
||||
} else if (mode == kDeviceModeLidar) {
|
||||
impl_.reset(new LidarCommandHandlerImpl(this, mem_pool_, loop_));
|
||||
impl_.reset(new LidarCommandHandlerImpl(this, loop_));
|
||||
}
|
||||
}
|
||||
if (impl_ == NULL) {
|
||||
@@ -102,26 +102,15 @@ bool CommandHandler::AddDevice(const DeviceInfo &info) {
|
||||
return impl_->AddDevice(info);
|
||||
}
|
||||
|
||||
bool CommandHandler::Init(IOLoop *loop) {
|
||||
apr_status_t rv = apr_pool_create(&mem_pool_, NULL);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CommandHandler::Init(std::weak_ptr<IOLoop> loop) {
|
||||
loop_ = loop;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CommandHandler::Uninit() {
|
||||
loop_ = NULL;
|
||||
if (impl_) {
|
||||
impl_.reset(NULL);
|
||||
}
|
||||
|
||||
if (mem_pool_) {
|
||||
apr_pool_destroy(mem_pool_);
|
||||
mem_pool_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void CommandHandler::OnCommand(uint8_t handle, const Command &command) {
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include "base/command_callback.h"
|
||||
#include "base/util.h"
|
||||
#include "command_channel.h"
|
||||
#include "device_discovery.h"
|
||||
#include "livox_sdk.h"
|
||||
@@ -39,9 +38,9 @@ class CommandHandlerImpl;
|
||||
|
||||
class CommandHandler : public noncopyable {
|
||||
public:
|
||||
explicit CommandHandler() : mem_pool_(NULL), loop_(NULL) {}
|
||||
explicit CommandHandler() {}
|
||||
|
||||
bool Init(IOLoop *loop);
|
||||
bool Init(std::weak_ptr<IOLoop> loop);
|
||||
void Uninit();
|
||||
|
||||
bool AddDevice(const DeviceInfo &info);
|
||||
@@ -71,10 +70,9 @@ class CommandHandler : public noncopyable {
|
||||
|
||||
private:
|
||||
std::multimap<uint16_t, Command> message_registers_;
|
||||
apr_pool_t *mem_pool_;
|
||||
std::unique_ptr<CommandHandlerImpl> impl_;
|
||||
std::mutex mutex_;
|
||||
IOLoop *loop_;
|
||||
std::weak_ptr<IOLoop> loop_;
|
||||
};
|
||||
|
||||
class CommandHandlerImpl : public CommandChannelDelegate {
|
||||
|
||||
@@ -262,7 +262,7 @@ livox_status HubStopSampling(CommonCommandCallback cb, void *client_data) {
|
||||
return DeviceSampleControl(kHubDefaultHandle, false, cb, client_data);
|
||||
}
|
||||
|
||||
livox_status HubGetLidarHandle(uint8_t slot, uint8_t id) {
|
||||
uint8_t HubGetLidarHandle(uint8_t slot, uint8_t id) {
|
||||
return (slot - 1) * 3 + id - 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
//
|
||||
|
||||
#include "hub_command_handler.h"
|
||||
#include "base/network_util.h"
|
||||
#include "base/network/network_util.h"
|
||||
|
||||
namespace livox {
|
||||
|
||||
@@ -41,7 +41,7 @@ bool HubCommandHandlerImpl::AddDevice(const DeviceInfo &info) {
|
||||
is_valid_ = true;
|
||||
hub_info_ = info;
|
||||
|
||||
channel_.reset(new CommandChannel(info.cmd_port, info.handle, info.ip, this, mem_pool_));
|
||||
channel_.reset(new CommandChannel(info.cmd_port, info.handle, info.ip, this));
|
||||
channel_->Bind(loop_);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -31,16 +31,15 @@
|
||||
namespace livox {
|
||||
class HubCommandHandlerImpl : public CommandHandlerImpl {
|
||||
public:
|
||||
HubCommandHandlerImpl(CommandHandler *handler, apr_pool_t *pool, IOLoop *loop)
|
||||
: CommandHandlerImpl(handler), mem_pool_(pool), loop_(loop), is_valid_(false) {}
|
||||
HubCommandHandlerImpl(CommandHandler *handler, std::weak_ptr<IOLoop> loop)
|
||||
: CommandHandlerImpl(handler), loop_(loop), is_valid_(false) {}
|
||||
void Uninit();
|
||||
bool AddDevice(const DeviceInfo &info);
|
||||
bool RemoveDevice(uint8_t handle);
|
||||
livox_status SendCommand(uint8_t handle, const Command &command);
|
||||
|
||||
private:
|
||||
apr_pool_t *mem_pool_;
|
||||
IOLoop *loop_;
|
||||
std::weak_ptr<IOLoop> loop_;
|
||||
bool is_valid_;
|
||||
DeviceInfo hub_info_;
|
||||
std::unique_ptr<CommandChannel> channel_;
|
||||
|
||||
@@ -34,7 +34,7 @@ void LidarCommandHandlerImpl::Uninit() {
|
||||
|
||||
bool LidarCommandHandlerImpl::AddDevice(const DeviceInfo &info) {
|
||||
std::shared_ptr<CommandChannel> channel =
|
||||
std::make_shared<CommandChannel>(info.cmd_port, info.handle, info.ip, this, mem_pool_);
|
||||
std::make_shared<CommandChannel>(info.cmd_port, info.handle, info.ip, this);
|
||||
channel->Bind(loop_);
|
||||
|
||||
DeviceItem item = {channel, info};
|
||||
|
||||
@@ -31,8 +31,8 @@ namespace livox {
|
||||
|
||||
class LidarCommandHandlerImpl : public CommandHandlerImpl {
|
||||
public:
|
||||
LidarCommandHandlerImpl(CommandHandler *handler, apr_pool_t *pool, IOLoop *loop)
|
||||
: CommandHandlerImpl(handler), mem_pool_(pool), loop_(loop) {}
|
||||
LidarCommandHandlerImpl(CommandHandler *handler, std::weak_ptr<IOLoop> loop)
|
||||
: CommandHandlerImpl(handler), loop_(loop) {}
|
||||
|
||||
void Uninit();
|
||||
|
||||
@@ -45,9 +45,8 @@ class LidarCommandHandlerImpl : public CommandHandlerImpl {
|
||||
std::shared_ptr<CommandChannel> channel;
|
||||
DeviceInfo info;
|
||||
} DeviceItem;
|
||||
apr_pool_t *mem_pool_;
|
||||
std::list<DeviceItem> devices_;
|
||||
IOLoop *loop_;
|
||||
std::weak_ptr<IOLoop> loop_;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
Reference in New Issue
Block a user