feature:support for conneting horizon
This commit is contained in:
@@ -34,18 +34,18 @@ namespace livox {
|
||||
class CommandCallback {
|
||||
public:
|
||||
virtual ~CommandCallback() {}
|
||||
virtual void operator()(uint8_t handle, void *data) = 0;
|
||||
virtual void operator()(livox_status status,uint8_t handle, void *data) = 0;
|
||||
};
|
||||
|
||||
template <class T, class ResponseType>
|
||||
class MemberFunctionCallback : public CommandCallback {
|
||||
public:
|
||||
typedef void (T::*MemFn)(uint8_t status, uint8_t handle, ResponseType *data);
|
||||
typedef void (T::*MemFn)(livox_status status, uint8_t handle, ResponseType *data);
|
||||
|
||||
MemberFunctionCallback(T *cls, MemFn func) : this_(cls), func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (this_) {
|
||||
(this_->*func_)((data == NULL) ? kStatusTimeout : kStatusSuccess, handle, static_cast<ResponseType *>(data));
|
||||
(this_->*func_)(status, handle, static_cast<ResponseType *>(data));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,14 +57,16 @@ class MemberFunctionCallback : public CommandCallback {
|
||||
template <class T>
|
||||
class MemberFunctionCallback<T, uint8_t> : public CommandCallback {
|
||||
public:
|
||||
typedef void (T::*MemFn)(uint8_t status, uint8_t handle, uint8_t response);
|
||||
typedef void (T::*MemFn)(livox_status status, uint8_t handle, uint8_t response);
|
||||
|
||||
MemberFunctionCallback(T *cls, MemFn func) : this_(cls), func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (this_) {
|
||||
(this_->*func_)((data == NULL) ? kStatusTimeout : kStatusSuccess,
|
||||
handle,
|
||||
static_cast<uint8_t>(reinterpret_cast<uintptr_t>(data)));
|
||||
if (data == NULL) {
|
||||
(this_->*func_)(status, handle, 0);
|
||||
} else {
|
||||
(this_->*func_)(status, handle, static_cast<uint8_t>(reinterpret_cast<uintptr_t>(data)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,13 +85,13 @@ boost::shared_ptr<CommandCallback> MakeCommandCallback(T *cls,
|
||||
template <class T>
|
||||
class FunctionStatusCallback : public CommandCallback {
|
||||
public:
|
||||
typedef void (*Fn)(uint8_t status, uint8_t handle, T *data, void *client_data);
|
||||
typedef void (*Fn)(livox_status status, uint8_t handle, T *data, void *client_data);
|
||||
|
||||
public:
|
||||
FunctionStatusCallback(Fn func, void *client_data) : func_(func), client_data_(client_data) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
(*func_)((data == NULL) ? kStatusTimeout : kStatusSuccess, handle, static_cast<T *>(data), client_data_);
|
||||
(*func_)(status, handle, static_cast<T *>(data), client_data_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,16 +103,16 @@ class FunctionStatusCallback : public CommandCallback {
|
||||
template <>
|
||||
class FunctionStatusCallback<uint8_t> : public CommandCallback {
|
||||
public:
|
||||
typedef void (*Fn)(uint8_t status, uint8_t handle, uint8_t data, void *client_data);
|
||||
typedef void (*Fn)(livox_status status, uint8_t handle, uint8_t data, void *client_data);
|
||||
|
||||
public:
|
||||
FunctionStatusCallback(Fn func, void *client_data) : func_(func), client_data_(client_data) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
if (data == NULL) {
|
||||
(*func_)(kStatusTimeout, handle, 0, client_data_);
|
||||
(*func_)(status, handle, 0, client_data_);
|
||||
} else {
|
||||
(*func_)(kStatusSuccess, handle, *(uint8_t *)data, client_data_);
|
||||
(*func_)(status, handle, *(uint8_t *)data, client_data_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,13 +131,13 @@ boost::shared_ptr<CommandCallback> MakeCommandCallback(typename FunctionStatusCa
|
||||
template <class T>
|
||||
class MessageCallback : public CommandCallback {
|
||||
public:
|
||||
typedef void (*Fn)(uint8_t handle, T *data);
|
||||
typedef void (*Fn)(livox_status status, uint8_t handle, T *data);
|
||||
|
||||
public:
|
||||
MessageCallback(const Fn &func) : func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
(*func_)(handle, static_cast<T *>(data));
|
||||
(*func_)(status, handle, static_cast<T *>(data));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,13 +148,13 @@ class MessageCallback : public CommandCallback {
|
||||
template <class ResponseType>
|
||||
class BoostFunctionMessageCallback : public CommandCallback {
|
||||
public:
|
||||
typedef boost::function<void(uint8_t, ResponseType *)> Fn;
|
||||
typedef boost::function<void(livox_status, uint8_t, ResponseType *)> Fn;
|
||||
|
||||
public:
|
||||
BoostFunctionMessageCallback(const Fn &func) : func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status, uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
func_(handle, static_cast<ResponseType *>(data));
|
||||
func_(status, handle, static_cast<ResponseType *>(data));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,12 +177,12 @@ boost::shared_ptr<CommandCallback> MakeMessageCallback(typename BoostFunctionMes
|
||||
template <class T, class ResponseType>
|
||||
class MemberMessageCallback : public CommandCallback {
|
||||
public:
|
||||
typedef void (T::*MemFn)(uint8_t handle, ResponseType *data);
|
||||
typedef void (T::*MemFn)(livox_status status, uint8_t handle, ResponseType *data);
|
||||
|
||||
MemberMessageCallback(T *cls, MemFn func) : this_(cls), func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status,uint8_t handle, void *data) {
|
||||
if (this_) {
|
||||
(this_->*func_)(handle, (ResponseType *)data);
|
||||
(this_->*func_)(status, handle, (ResponseType *)data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,12 +204,12 @@ class BoostFunctionCallback : public CommandCallback {
|
||||
public:
|
||||
typedef boost::function<void(uint8_t, uint8_t, ResponseType *)> Fn;
|
||||
BoostFunctionCallback(const Fn &func) : func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status,uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
if (data == NULL) {
|
||||
func_(kStatusTimeout, handle, NULL);
|
||||
func_(status, handle, NULL);
|
||||
} else {
|
||||
func_(kStatusSuccess, handle, (ResponseType *)data);
|
||||
func_(status, handle, (ResponseType *)data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,12 +223,12 @@ class BoostFunctionCallback<uint8_t> : public CommandCallback {
|
||||
public:
|
||||
typedef boost::function<void(uint8_t, uint8_t, uint8_t)> Fn;
|
||||
BoostFunctionCallback(const Fn &func) : func_(func) {}
|
||||
void operator()(uint8_t handle, void *data) {
|
||||
void operator()(livox_status status,uint8_t handle, void *data) {
|
||||
if (func_) {
|
||||
if (data == NULL) {
|
||||
func_(kStatusTimeout, handle, 0);
|
||||
func_(status, handle, 0);
|
||||
} else {
|
||||
func_(kStatusSuccess, handle, static_cast<uint8_t>(reinterpret_cast<uintptr_t>(data)));
|
||||
func_(status, handle, static_cast<uint8_t>(reinterpret_cast<uintptr_t>(data)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ bool IOLoop::Init() {
|
||||
if (mem_pool_ == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
apr_status_t rv =
|
||||
apr_pollset_create(&pollset_, kMaxPollCount, mem_pool_, APR_POLLSET_WAKEABLE);
|
||||
@@ -80,6 +81,12 @@ void IOLoop::RemoveDelegate(apr_socket_t *sock, IOLoopDelegate *) {
|
||||
PostTask(boost::bind(&IOLoop::RemoveDelegateAsync, this, sock));
|
||||
}
|
||||
|
||||
void IOLoop::RemoveDelegateSync(apr_socket_t *sock) {
|
||||
apr_os_thread_t thread_id = apr_os_thread_current();
|
||||
assert(apr_os_thread_equal(this->thread_id_, thread_id));
|
||||
RemoveDelegateAsync(sock);
|
||||
}
|
||||
|
||||
void IOLoop::Loop() {
|
||||
apr_int32_t num = 0;
|
||||
const apr_pollfd_t *ret_pfd = NULL;
|
||||
@@ -88,7 +95,6 @@ void IOLoop::Loop() {
|
||||
if (rv == APR_SUCCESS) {
|
||||
for (int i = 0; i < num; i++) {
|
||||
ClientData *data = static_cast<ClientData *>(ret_pfd[i].client_data);
|
||||
;
|
||||
if (data) {
|
||||
IOLoopDelegate *delegate = data->first;
|
||||
if (delegate) {
|
||||
|
||||
@@ -60,9 +60,12 @@ class IOLoop : public noncopyable {
|
||||
void Uninit();
|
||||
void AddDelegate(apr_socket_t *sock, IOLoopDelegate *delegate, void *data = NULL);
|
||||
void RemoveDelegate(apr_socket_t *sock, IOLoopDelegate *delegate);
|
||||
void RemoveDelegateSync(apr_socket_t *sock);
|
||||
void Loop();
|
||||
bool Wakeup();
|
||||
void PostTask(const IOLoopTask &task);
|
||||
void SetThreadId (apr_os_thread_t thread_id) { thread_id_ = thread_id; }
|
||||
apr_os_thread_t GetThreadId () { return thread_id_; }
|
||||
|
||||
private:
|
||||
void AddDelegateAsync(apr_socket_t *sock, IOLoopDelegate *delegate, void *data);
|
||||
@@ -80,6 +83,7 @@ class IOLoop : public noncopyable {
|
||||
bool enable_timer_;
|
||||
bool enable_wake_;
|
||||
std::vector<IOLoopTask> pending_tasks_;
|
||||
apr_os_thread_t thread_id_;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
@@ -35,6 +35,9 @@ void IOThread::ThreadFunc() {
|
||||
if (loop_ == NULL) {
|
||||
return;
|
||||
}
|
||||
apr_os_thread_t thread_id = apr_os_thread_current();
|
||||
loop_->SetThreadId(thread_id);
|
||||
|
||||
while (!IsQuit()) {
|
||||
loop_->Loop();
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
#ifdef WIN32
|
||||
#include <boost/scoped_array.hpp>
|
||||
#include <Winsock2.h>
|
||||
#include <Iphlpapi.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <string>
|
||||
#pragma comment(lib,"Iphlpapi.lib")
|
||||
#pragma comment(lib,"iphlpapi.lib")
|
||||
#pragma comment(lib,"ws2_32.lib")
|
||||
#else
|
||||
#include <ifaddrs.h>
|
||||
@@ -36,11 +36,13 @@
|
||||
|
||||
namespace livox {
|
||||
namespace util {
|
||||
apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool nonblock) {
|
||||
apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool reuse_port, bool nonblock) {
|
||||
apr_sockaddr_t *sa = NULL;
|
||||
apr_socket_t *s = NULL;
|
||||
apr_status_t rv = APR_SUCCESS;
|
||||
|
||||
|
||||
|
||||
rv = apr_sockaddr_info_get(&sa, NULL, APR_INET, port, 0, mem_pool);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return NULL;
|
||||
@@ -51,6 +53,15 @@ apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool nonbloc
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (reuse_port) {
|
||||
rv = apr_socket_opt_set(s, APR_SO_REUSEADDR, 1);
|
||||
if (rv != APR_SUCCESS) {
|
||||
apr_socket_close(s);
|
||||
s = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
rv = apr_socket_bind(s, sa);
|
||||
if (rv != APR_SUCCESS) {
|
||||
apr_socket_close(s);
|
||||
@@ -87,7 +98,7 @@ bool GetAdapterState(const IP_ADAPTER_INFO *pAdapter)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
bool FindLocalIp(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
bool found = false;
|
||||
ULONG ulOutbufLen = sizeof(IP_ADAPTER_INFO);
|
||||
boost::scoped_array<uint8_t> pAdapterInfo(new uint8_t[ulOutbufLen]);
|
||||
@@ -99,29 +110,25 @@ bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
|
||||
IP_ADAPTER_INFO *pAdapter = reinterpret_cast<IP_ADAPTER_INFO *>(pAdapterInfo.get());
|
||||
if (NO_ERROR == dlRetVal && pAdapter != NULL) {
|
||||
while (pAdapter != NULL) {
|
||||
if (GetAdapterState(pAdapter)) {
|
||||
std::string str_ip = pAdapter->IpAddressList.IpAddress.String;
|
||||
std::string str_mask = pAdapter->IpAddressList.IpMask.String;
|
||||
ULONG host_ip = inet_addr(const_cast<char *>(str_ip.c_str()));
|
||||
ULONG host_mask = inet_addr(const_cast<char *>(str_mask.c_str()));
|
||||
while (GetAdapterState(pAdapter)) {
|
||||
std::string str_ip = pAdapter->IpAddressList.IpAddress.String;
|
||||
std::string str_mask = pAdapter->IpAddressList.IpMask.String;
|
||||
ULONG host_ip = inet_addr(const_cast<char *>(str_ip.c_str()));
|
||||
ULONG host_mask = inet_addr(const_cast<char *>(str_mask.c_str()));
|
||||
|
||||
if ((host_ip & host_mask) ==
|
||||
(client_addr.sin_addr.S_un.S_addr & host_mask)) {
|
||||
local_ip = host_ip;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if ((host_ip & host_mask) ==
|
||||
(client_addr.sin_addr.S_un.S_addr & host_mask)) {
|
||||
local_ip = host_ip;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
pAdapter = pAdapter->Next;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
#else
|
||||
bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
bool FindLocalIp(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
struct ifaddrs *if_addrs = NULL, *addrs = NULL;
|
||||
if (getifaddrs(&if_addrs) == -1) {
|
||||
return false;
|
||||
@@ -153,4 +160,4 @@ bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
}
|
||||
#endif
|
||||
} // namespace util
|
||||
} // namespace livox
|
||||
} // namespace livox
|
||||
@@ -34,8 +34,8 @@
|
||||
namespace livox {
|
||||
namespace util {
|
||||
|
||||
apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool nonblock = true);
|
||||
bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip);
|
||||
apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool reuse_port = false, bool nonblock = true);
|
||||
bool FindLocalIp(const struct sockaddr_in &client_addr, uint32_t &local_ip);
|
||||
|
||||
} // namespace util
|
||||
} // namespace livox
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#define LIVOX_THREAD_BASE_H_
|
||||
#include <apr_general.h>
|
||||
#include <apr_thread_proc.h>
|
||||
#include <apr_portable.h>
|
||||
#include <boost/atomic/atomic.hpp>
|
||||
#include "noncopyable.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user