remove dependency on APR library
This commit is contained in:
+48
-106
@@ -26,6 +26,7 @@
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include "logging.h"
|
||||
|
||||
using std::lock_guard;
|
||||
@@ -33,101 +34,37 @@ using std::mutex;
|
||||
using std::vector;
|
||||
using std::chrono::steady_clock;
|
||||
|
||||
|
||||
namespace livox {
|
||||
|
||||
bool IOLoop::Init() {
|
||||
if (mem_pool_ == NULL) {
|
||||
auto multiple_io = MultipleIOFactory::CreateMultipleIO();
|
||||
if (!multiple_io) {
|
||||
LOG_ERROR("Creat Multiple IO Failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
apr_status_t rv =
|
||||
apr_pollset_create(&pollset_, kMaxPollCount, mem_pool_, APR_POLLSET_WAKEABLE);
|
||||
|
||||
if (rv != APR_SUCCESS) {
|
||||
LOG_ERROR(PrintAPRStatus(rv));
|
||||
multiple_io_base_ = std::move(multiple_io);
|
||||
if (!multiple_io_base_->PollCreate(OPEN_MAX_POLL)) {
|
||||
LOG_ERROR("Poll Create Failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IOLoop::Uninit() {
|
||||
if (pollset_) {
|
||||
apr_pollset_destroy(pollset_);
|
||||
pollset_ = NULL;
|
||||
}
|
||||
|
||||
for (DelegatesType::const_iterator ite = delegates_.begin(); ite != delegates_.end(); ++ite) {
|
||||
ClientData *data = ite->second;
|
||||
if (data) {
|
||||
delete data;
|
||||
}
|
||||
}
|
||||
|
||||
delegates_.clear();
|
||||
mem_pool_ = NULL;
|
||||
multiple_io_base_->PollDestroy();
|
||||
}
|
||||
|
||||
void IOLoop::AddDelegate(apr_socket_t *sock, IOLoop::IOLoopDelegate *delegate, void *data) {
|
||||
void IOLoop::AddDelegate(socket_t sock, IOLoop::IOLoopDelegate *delegate, void *data) {
|
||||
PostTask(std::bind(&IOLoop::AddDelegateAsync, this, sock, delegate, data));
|
||||
}
|
||||
|
||||
void IOLoop::RemoveDelegate(apr_socket_t *sock, IOLoopDelegate *) {
|
||||
void IOLoop::RemoveDelegate(socket_t sock, IOLoopDelegate *) {
|
||||
PostTask(std::bind(&IOLoop::RemoveDelegateAsync, this, sock));
|
||||
}
|
||||
|
||||
void IOLoop::RemoveDelegateSync(apr_socket_t *sock) {
|
||||
assert(apr_os_thread_equal(this->thread_id_, apr_os_thread_current()));
|
||||
RemoveDelegateAsync(sock);
|
||||
}
|
||||
|
||||
void IOLoop::Loop() {
|
||||
apr_int32_t num = 0;
|
||||
const apr_pollfd_t *ret_pfd = NULL;
|
||||
apr_status_t rv = apr_pollset_poll(pollset_, apr_time_from_msec(50), &num, &ret_pfd);
|
||||
|
||||
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) {
|
||||
delegate->OnData(ret_pfd[i].desc.s, data->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enable_wake_ && APR_STATUS_IS_EINTR(rv)) {
|
||||
DelegatesType delegates = delegates_;
|
||||
for (DelegatesType::const_iterator ite = delegates.begin(); ite != delegates.end(); ++ite) {
|
||||
ClientData *data = ite->second;
|
||||
if (data) {
|
||||
IOLoopDelegate *delegate = data->first;
|
||||
if (delegate) {
|
||||
delegate->OnWake();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enable_timer_) {
|
||||
TimePoint t = steady_clock::now();;
|
||||
if (last_timeout_ == TimePoint() || t - last_timeout_ > std::chrono::milliseconds(50)) {
|
||||
last_timeout_ = t;
|
||||
// copy delegates_ in case delegate is removed in callback.
|
||||
DelegatesType delegates = delegates_;
|
||||
for (DelegatesType::const_iterator ite = delegates.begin(); ite != delegates.end(); ++ite) {
|
||||
ClientData *data = ite->second;
|
||||
if (data) {
|
||||
IOLoopDelegate *delegate = data->first;
|
||||
if (delegate) {
|
||||
delegate->OnTimer(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
multiple_io_base_->Poll(POLL_TIMEOUT);
|
||||
|
||||
vector<IOLoopTask> tasks;
|
||||
{
|
||||
@@ -135,24 +72,17 @@ void IOLoop::Loop() {
|
||||
tasks.swap(pending_tasks_);
|
||||
}
|
||||
|
||||
for (vector<IOLoopTask>::iterator ite = tasks.begin(); ite != tasks.end(); ++ite) {
|
||||
IOLoopTask &task = *ite;
|
||||
for (auto &task : tasks) {
|
||||
task();
|
||||
}
|
||||
|
||||
if (rv != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(rv) && !APR_STATUS_IS_EINTR(rv)) {
|
||||
LOG_ERROR(PrintAPRStatus(rv));
|
||||
}
|
||||
}
|
||||
|
||||
bool IOLoop::Wakeup() {
|
||||
apr_status_t rv = apr_pollset_wakeup(pollset_);
|
||||
if (rv != APR_SUCCESS) {
|
||||
LOG_ERROR(PrintAPRStatus(rv));
|
||||
return false;
|
||||
bool IOLoop::Wakeup() {
|
||||
if (multiple_io_base_) {
|
||||
multiple_io_base_->PollWakeUp();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void IOLoop::PostTask(const IOLoopTask &task) {
|
||||
{
|
||||
@@ -162,27 +92,39 @@ void IOLoop::PostTask(const IOLoopTask &task) {
|
||||
Wakeup();
|
||||
}
|
||||
|
||||
void IOLoop::AddDelegateAsync(apr_socket_t *sock, IOLoop::IOLoopDelegate *delegate, void *data) {
|
||||
ClientData *p = new ClientData(delegate, data);
|
||||
apr_pollfd_t pfd = {mem_pool_, APR_POLL_SOCKET, APR_POLLIN, 0, {NULL}, p};
|
||||
pfd.desc.s = sock;
|
||||
apr_pollset_add(pollset_, &pfd);
|
||||
delegates_[sock] = p;
|
||||
void IOLoop::AddDelegateAsync(socket_t sock, IOLoop::IOLoopDelegate *delegate, void *data) {
|
||||
PollFd pollfd = {};
|
||||
pollfd.fd = sock;
|
||||
pollfd.event = READBLE_EVENT;
|
||||
pollfd.event_callback = [=](FdEvent event) {
|
||||
if (event & READBLE_EVENT) {
|
||||
if (delegate) {
|
||||
delegate->OnData(sock, data);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (enable_timer_) {
|
||||
pollfd.timer_callback = [=](TimePoint t) {
|
||||
if (delegate) {
|
||||
delegate->OnTimer(t);
|
||||
}
|
||||
};
|
||||
}
|
||||
if (enable_wake_) {
|
||||
pollfd.wake_callback = [=]() {
|
||||
if (delegate) {
|
||||
delegate->OnWake();
|
||||
}
|
||||
};
|
||||
}
|
||||
multiple_io_base_->PollSetAdd(pollfd);
|
||||
}
|
||||
|
||||
void IOLoop::RemoveDelegateAsync(apr_socket_t *sock) {
|
||||
apr_pollfd_t pfd = {mem_pool_, APR_POLL_SOCKET, 0, 0, {NULL}, NULL};
|
||||
pfd.desc.s = sock;
|
||||
apr_pollset_remove(pollset_, &pfd);
|
||||
|
||||
DelegatesType::iterator ite = delegates_.find(sock);
|
||||
if (ite != delegates_.end()) {
|
||||
ClientData *p = ite->second;
|
||||
if (p) {
|
||||
delete p;
|
||||
}
|
||||
delegates_.erase(ite);
|
||||
}
|
||||
void IOLoop::RemoveDelegateAsync(socket_t sock) {
|
||||
PollFd pollfd = {};
|
||||
pollfd.fd = sock;
|
||||
pollfd.event = READBLE_EVENT;
|
||||
multiple_io_base_->PollSetRemove(pollfd);
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
+19
-27
@@ -30,61 +30,53 @@
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "apr_general.h"
|
||||
#include "apr_network_io.h"
|
||||
#include "apr_poll.h"
|
||||
#include "apr_thread_proc.h"
|
||||
#include <algorithm>
|
||||
#include "command_callback.h"
|
||||
#include "noncopyable.h"
|
||||
#include "thread_base.h"
|
||||
#include "util.h"
|
||||
#include "multiple_io/multiple_io_base.h"
|
||||
#include "multiple_io/multiple_io_factory.h"
|
||||
|
||||
namespace livox {
|
||||
|
||||
#define OPEN_MAX_POLL 48
|
||||
#define POLL_TIMEOUT 50 //ms
|
||||
typedef int socket_t;
|
||||
|
||||
class IOLoop : public noncopyable {
|
||||
public:
|
||||
typedef std::chrono::steady_clock::time_point TimePoint;
|
||||
typedef std::function<void(void)> IOLoopTask;
|
||||
|
||||
class IOLoopDelegate {
|
||||
public:
|
||||
virtual void OnData(apr_socket_t *, void *) {}
|
||||
virtual void OnTimer(TimePoint) {}
|
||||
virtual void OnData(socket_t, void *) {}
|
||||
virtual void OnTimer(std::chrono::steady_clock::time_point) {}
|
||||
virtual void OnWake() {}
|
||||
};
|
||||
|
||||
typedef std::function<void(void)> IOLoopTask;
|
||||
|
||||
public:
|
||||
explicit IOLoop(apr_pool_t *mem_pool, bool enable_timer = true, bool enable_wake = true)
|
||||
: pollset_(NULL), mem_pool_(mem_pool), last_timeout_(), enable_timer_(enable_timer), enable_wake_(enable_wake){};
|
||||
explicit IOLoop(bool enable_timer = true, bool enable_wake = true)
|
||||
: enable_timer_(enable_timer), enable_wake_(enable_wake){};
|
||||
|
||||
|
||||
bool Init();
|
||||
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 AddDelegate(socket_t sock, IOLoopDelegate *delegate, void *data = NULL);
|
||||
void RemoveDelegate(socket_t sock, IOLoopDelegate *delegate);
|
||||
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);
|
||||
void RemoveDelegateAsync(apr_socket_t *sock);
|
||||
void AddDelegateAsync(socket_t sock, IOLoopDelegate *delegate, void *data);
|
||||
void RemoveDelegateAsync(socket_t sock);
|
||||
|
||||
private:
|
||||
static const apr_uint32_t kMaxPollCount = 48;
|
||||
typedef std::pair<IOLoopDelegate *, void *> ClientData;
|
||||
typedef std::unordered_map<apr_socket_t *, ClientData *> DelegatesType;
|
||||
DelegatesType delegates_;
|
||||
apr_pollset_t *pollset_;
|
||||
apr_pool_t *mem_pool_;
|
||||
TimePoint last_timeout_;
|
||||
std::mutex mutex_;
|
||||
bool enable_timer_;
|
||||
bool enable_wake_;
|
||||
std::vector<IOLoopTask> pending_tasks_;
|
||||
apr_os_thread_t thread_id_;
|
||||
std::unique_ptr<MultipleIOBase> multiple_io_base_;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
//
|
||||
|
||||
#include "io_thread.h"
|
||||
#include "apr_pools.h"
|
||||
|
||||
namespace livox {
|
||||
|
||||
@@ -32,11 +31,9 @@ void IOThread::Join() {
|
||||
}
|
||||
|
||||
void IOThread::ThreadFunc() {
|
||||
if (loop_ == NULL) {
|
||||
if (!loop_) {
|
||||
return;
|
||||
}
|
||||
apr_os_thread_t thread_id = apr_os_thread_current();
|
||||
loop_->SetThreadId(thread_id);
|
||||
|
||||
while (!IsQuit()) {
|
||||
loop_->Loop();
|
||||
@@ -48,14 +45,13 @@ bool IOThread::Init(bool enable_timer, bool enable_wake) {
|
||||
return false;
|
||||
}
|
||||
|
||||
loop_.reset(new IOLoop(pool_, enable_timer, enable_wake));
|
||||
loop_ = std::make_shared<IOLoop>(enable_timer, enable_wake);
|
||||
return loop_->Init();
|
||||
}
|
||||
|
||||
void IOThread::Uninit() {
|
||||
if (loop_) {
|
||||
loop_->Uninit();
|
||||
loop_.reset(NULL);
|
||||
}
|
||||
ThreadBase::Uninit();
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@ class IOThread : public ThreadBase {
|
||||
void Join();
|
||||
void Uninit();
|
||||
|
||||
IOLoop *loop() { return loop_.get(); }
|
||||
std::weak_ptr<IOLoop> loop() { return loop_; }
|
||||
void ThreadFunc();
|
||||
|
||||
private:
|
||||
std::unique_ptr<IOLoop> loop_;
|
||||
std::shared_ptr<IOLoop> loop_;
|
||||
};
|
||||
} // namespace livox
|
||||
#endif // LIVOX_IO_THREAD_H_
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
|
||||
namespace livox {
|
||||
|
||||
void MultipleIOBase::CheckTimer() {
|
||||
TimePoint t = std::chrono::steady_clock::now();
|
||||
if (t - last_timeout_ > std::chrono::milliseconds(50)) {
|
||||
last_timeout_ = t;
|
||||
for (auto & descriptor : descriptors_) {
|
||||
PollFd pollfd = descriptor.second;
|
||||
if (pollfd.timer_callback) {
|
||||
pollfd.timer_callback(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MultipleIOBase::WakeUpInit() {
|
||||
//Initialize wake up pipe
|
||||
wake_up_pipe_.reset(new WakeUpPipe());
|
||||
wake_up_pipe_->PipeCreate();
|
||||
//register wake_fd to multiple io
|
||||
PollFd wake_fd = {};
|
||||
wake_fd.fd = wake_up_pipe_->GetPipeOut();
|
||||
wake_fd.event = READBLE_EVENT;
|
||||
wake_fd.event_callback = [this](FdEvent event) {
|
||||
if (event & READBLE_EVENT) {
|
||||
if (wake_up_pipe_) {
|
||||
wake_up_pipe_->Drain();
|
||||
}
|
||||
for (auto & descriptor : descriptors_) {
|
||||
PollFd pollfd = descriptor.second;
|
||||
if (pollfd.wake_callback) {
|
||||
pollfd.wake_callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
PollSetAdd(wake_fd);
|
||||
}
|
||||
|
||||
void MultipleIOBase::WakeUpUninit() {
|
||||
PollFd wake_fd = {};
|
||||
wake_fd.fd = wake_up_pipe_->GetPipeOut();
|
||||
wake_fd.event = READBLE_EVENT | WRITABLE_EVENT;
|
||||
PollSetRemove(wake_fd);
|
||||
if (wake_up_pipe_) {
|
||||
wake_up_pipe_->PipeDestroy();
|
||||
wake_up_pipe_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void MultipleIOBase::PollWakeUp() {
|
||||
if (wake_up_pipe_) {
|
||||
wake_up_pipe_->WakeUp();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef MULTIPLE_IO_BASE_H_
|
||||
#define MULTIPLE_IO_BASE_H_
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include "base/wake_up/wake_up_pipe.h"
|
||||
|
||||
namespace livox {
|
||||
|
||||
#define NONE_EVENT 0 /* No events registered. */
|
||||
#define READBLE_EVENT 1 /* when descriptor is readable. */
|
||||
#define WRITABLE_EVENT 2 /* when descriptor is writeable. */
|
||||
|
||||
typedef std::chrono::steady_clock::time_point TimePoint;
|
||||
typedef int FdEvent;
|
||||
|
||||
typedef struct {
|
||||
int fd; /* File descriptor. */
|
||||
FdEvent event; /* Read | Write Event to listen. */
|
||||
std::function<void(FdEvent)> event_callback; /* Read or Write Event Callback. */
|
||||
std::function<void(TimePoint)> timer_callback; /* Timer Event Callback. */
|
||||
std::function<void()> wake_callback; /* WakeUp Event Callback. */
|
||||
} PollFd;
|
||||
|
||||
class MultipleIOBase {
|
||||
public:
|
||||
MultipleIOBase() = default;
|
||||
virtual ~MultipleIOBase() = default;
|
||||
virtual bool PollCreate(int size) = 0;
|
||||
virtual void PollDestroy() = 0;
|
||||
virtual bool PollSetAdd(PollFd poll_fd) = 0;
|
||||
virtual bool PollSetRemove(PollFd poll_fd) = 0;
|
||||
virtual void Poll(int timeout) = 0;
|
||||
virtual void PollWakeUp();
|
||||
protected:
|
||||
virtual void CheckTimer();
|
||||
std::map<int, PollFd> descriptors_;
|
||||
TimePoint last_timeout_ = TimePoint();
|
||||
|
||||
virtual void WakeUpInit();
|
||||
virtual void WakeUpUninit();
|
||||
std::unique_ptr<WakeUpPipe> wake_up_pipe_;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // MULTIPLE_IO_BASE_H_
|
||||
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "multiple_io_epoll.h"
|
||||
#ifdef HAVE_EPOLL
|
||||
namespace livox {
|
||||
|
||||
int GetEvent (FdEvent event) {
|
||||
FdEvent rv = 0;
|
||||
if (event & READBLE_EVENT)
|
||||
rv |= EPOLLIN;
|
||||
if (event & WRITABLE_EVENT)
|
||||
rv |= EPOLLOUT;
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool MultipleIOEpoll::PollCreate(int size) {
|
||||
max_poll_size_ = size + 1;
|
||||
epoll_fd_ = epoll_create(max_poll_size_);
|
||||
if (epoll_fd_ < 0) {
|
||||
return false;
|
||||
}
|
||||
pollset_.reset(new struct epoll_event[size]);
|
||||
WakeUpInit();
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOEpoll::PollDestroy() {
|
||||
WakeUpUninit();
|
||||
if (epoll_fd_ > 0) {
|
||||
close(epoll_fd_);
|
||||
epoll_fd_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool MultipleIOEpoll::PollSetAdd(PollFd poll_fd) {
|
||||
if (max_poll_size_ <= (int)descriptors_.size()) {
|
||||
return false;
|
||||
}
|
||||
struct epoll_event ee = {0};
|
||||
ee.events = GetEvent(poll_fd.event);
|
||||
ee.data.fd = poll_fd.fd;
|
||||
if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, poll_fd.fd, &ee) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int fd = poll_fd.fd;
|
||||
descriptors_[fd] = poll_fd;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleIOEpoll::PollSetRemove(PollFd poll_fd) {
|
||||
int fd = poll_fd.fd;
|
||||
struct epoll_event ee = {0};
|
||||
epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &ee);
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
descriptors_.erase(fd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOEpoll::Poll(int time_out) {
|
||||
int ret = epoll_wait(epoll_fd_, pollset_.get(), (int)descriptors_.size(),
|
||||
time_out);
|
||||
if (ret > 0) {
|
||||
for (int i =0; i< ret; i++) {
|
||||
FdEvent fd_event = NONE_EVENT;
|
||||
if (pollset_[i].events & EPOLLIN) {
|
||||
fd_event |= READBLE_EVENT;
|
||||
}
|
||||
if (pollset_[i].events & EPOLLOUT) {
|
||||
fd_event |= WRITABLE_EVENT;
|
||||
}
|
||||
int fd = pollset_[i].data.fd;
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
PollFd pollfd = descriptors_[fd];
|
||||
pollfd.event_callback(fd_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
CheckTimer();
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_EPOLL
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef MULTIPLE_IO_EPOLL_H_
|
||||
#define MULTIPLE_IO_EPOLL_H_
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
#include "config.h"
|
||||
#include <memory>
|
||||
|
||||
#ifdef HAVE_EPOLL
|
||||
|
||||
namespace livox {
|
||||
|
||||
class MultipleIOEpoll : public MultipleIOBase {
|
||||
public:
|
||||
bool PollCreate(int size);
|
||||
bool PollSetAdd(PollFd poll_fd);
|
||||
bool PollSetRemove(PollFd poll_fd);
|
||||
void Poll(int timeout);
|
||||
void PollDestroy();
|
||||
private:
|
||||
int epoll_fd_ = -1;
|
||||
std::unique_ptr<struct epoll_event[]> pollset_;
|
||||
int max_poll_size_ = 0;
|
||||
};
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_EPOLL
|
||||
#endif // MULTIPLE_IO_EPOLL_H_
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef MULTIPLE_IO_FACTORY_H_
|
||||
#define MULTIPLE_IO_FACTORY_H_
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
#include "multiple_io_epoll.h"
|
||||
#include "multiple_io_kqueue.h"
|
||||
#include "multiple_io_select.h"
|
||||
#include "multiple_io_poll.h"
|
||||
#include <memory>
|
||||
|
||||
namespace livox {
|
||||
|
||||
class MultipleIOFactory {
|
||||
public:
|
||||
static std::unique_ptr<MultipleIOBase> CreateMultipleIO() {
|
||||
#if defined(HAVE_EPOLL)
|
||||
return std::unique_ptr<MultipleIOBase>(new MultipleIOEpoll());
|
||||
#elif defined(HAVE_KQUEUE)
|
||||
return std::unique_ptr<MultipleIOBase>(new MultipleIOKqueue());
|
||||
#elif defined(HAVE_SELECT)
|
||||
return std::unique_ptr<MultipleIOBase>(new MultipleIOSelect());
|
||||
#elif defined(HAVE_POLL)
|
||||
return std::unique_ptr<MultipleIOBase>(new MultipleIOPoll());
|
||||
#else
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // MULTIPLE_IO_FACTORY_H_
|
||||
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "multiple_io_kqueue.h"
|
||||
#ifdef HAVE_KQUEUE
|
||||
|
||||
namespace livox {
|
||||
|
||||
bool MultipleIOKqueue::PollCreate(int size) {
|
||||
kqueue_fd_ = kqueue();
|
||||
if (kqueue_fd_ == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int flags = 0;
|
||||
if ((flags = fcntl(kqueue_fd_, F_GETFD)) == -1) {
|
||||
close(kqueue_fd_);
|
||||
return false;
|
||||
}
|
||||
flags |= FD_CLOEXEC;
|
||||
if (fcntl(kqueue_fd_, F_SETFD, flags) == -1) {
|
||||
close(kqueue_fd_);
|
||||
return false;
|
||||
}
|
||||
max_poll_size_ = size + 1;
|
||||
set_size_ = 2 * max_poll_size_;
|
||||
kevent_set_.reset(new struct kevent[set_size_]);
|
||||
WakeUpInit();
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOKqueue::PollDestroy() {
|
||||
WakeUpUninit();
|
||||
if (kqueue_fd_ > 0) {
|
||||
close(kqueue_fd_);
|
||||
kqueue_fd_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool MultipleIOKqueue::PollSetAdd(PollFd poll_fd) {
|
||||
if (max_poll_size_ <= (int)descriptors_.size()) {
|
||||
return false;
|
||||
}
|
||||
int fd = poll_fd.fd;
|
||||
descriptors_[fd] = poll_fd;
|
||||
if (poll_fd.event & READBLE_EVENT) {
|
||||
EV_SET(&kevent_, fd, EVFILT_READ, EV_ADD, 0, 0, &descriptors_[fd].fd);
|
||||
if (kevent(kqueue_fd_, &kevent_, 1, nullptr, 0, nullptr) == -1) {
|
||||
descriptors_.erase(fd);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (poll_fd.event & WRITABLE_EVENT) {
|
||||
EV_SET(&kevent_, fd, EVFILT_WRITE, EV_ADD, 0, 0, &descriptors_[fd].fd);
|
||||
if (kevent(kqueue_fd_, &kevent_, 1, nullptr, 0, nullptr) == -1) {
|
||||
descriptors_.erase(fd);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleIOKqueue::PollSetRemove(PollFd poll_fd) {
|
||||
int fd = poll_fd.fd;
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
if (descriptors_[fd].event & READBLE_EVENT) {
|
||||
EV_SET(&kevent_, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
||||
if (kevent(kqueue_fd_, &kevent_, 1, nullptr, 0, nullptr) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (descriptors_[fd].event & WRITABLE_EVENT) {
|
||||
EV_SET(&kevent_, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
||||
if (kevent(kqueue_fd_, &kevent_, 1, nullptr, 0, nullptr) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
descriptors_.erase(fd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOKqueue::Poll(int time_out) {
|
||||
struct timespec tv, *tvptr;
|
||||
|
||||
if (time_out < 0) {
|
||||
tvptr = NULL;
|
||||
} else {
|
||||
tv.tv_sec = (long) time_out / 1000;
|
||||
tv.tv_nsec = (long) (time_out % 1000) * 1000000;
|
||||
tvptr = &tv;
|
||||
}
|
||||
|
||||
int rv = kevent(kqueue_fd_, NULL, 0, kevent_set_.get(), set_size_, tvptr);
|
||||
if (rv > 0) {
|
||||
for (int i = 0; i < rv; i++) {
|
||||
int fd = *(int *)(kevent_set_[i].udata);
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
PollFd pollfd = descriptors_[fd];
|
||||
if (kevent_set_[i].filter == EVFILT_READ) {
|
||||
pollfd.event_callback(READBLE_EVENT);
|
||||
}
|
||||
if (kevent_set_[i].filter == EVFILT_WRITE) {
|
||||
pollfd.event_callback(WRITABLE_EVENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CheckTimer();
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_KQUEUE
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef MULTIPLE_IO_KQUEUE_H_
|
||||
#define MULTIPLE_IO_KQUEUE_H_
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
#include "config.h"
|
||||
#ifdef HAVE_KQUEUE
|
||||
|
||||
namespace livox {
|
||||
|
||||
class MultipleIOKqueue : public MultipleIOBase {
|
||||
public:
|
||||
bool PollCreate(int size);
|
||||
bool PollSetAdd(PollFd poll_fd);
|
||||
bool PollSetRemove(PollFd poll_fd);
|
||||
void Poll(int timeout);
|
||||
void PollDestroy();
|
||||
private:
|
||||
int kqueue_fd_ = -1;
|
||||
struct kevent kevent_ = {};
|
||||
std::unique_ptr<struct kevent[]> kevent_set_;
|
||||
int max_poll_size_ = 0;
|
||||
int set_size_ = 0;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_KQUEUE
|
||||
#endif // MULTIPLE_IO_KQUEUE_H_
|
||||
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "multiple_io_poll.h"
|
||||
|
||||
#ifdef HAVE_POLL
|
||||
|
||||
namespace livox {
|
||||
|
||||
int GetEvent (FdEvent event) {
|
||||
int rv = 0;
|
||||
if (event & READBLE_EVENT)
|
||||
rv |= POLLIN;
|
||||
if (event & WRITABLE_EVENT)
|
||||
rv |= POLLOUT;
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool MultipleIOPoll:: PollCreate(int size) {
|
||||
max_poll_size_ = size + 1;
|
||||
pollset_.reset(new struct pollfd[max_poll_size_]);
|
||||
WakeUpInit();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleIOPoll:: PollSetAdd(PollFd poll_fd) {
|
||||
if (max_poll_size_ <= (int)descriptors_.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int fd = poll_fd.fd;
|
||||
descriptors_[fd] = poll_fd;
|
||||
|
||||
struct pollfd fds;
|
||||
fds.fd = fd;
|
||||
fds.events = GetEvent(poll_fd.event);
|
||||
pollset_[pollset_num_] = fds;
|
||||
pollset_num_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOPoll::PollDestroy() {
|
||||
WakeUpUninit();
|
||||
return;
|
||||
}
|
||||
|
||||
bool MultipleIOPoll:: PollSetRemove(PollFd poll_fd) {
|
||||
int fd = poll_fd.fd;
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
descriptors_.erase(fd);
|
||||
}
|
||||
|
||||
for (int i = 0; i< pollset_num_; i++) {
|
||||
if (pollset_[i].fd == fd) {
|
||||
int dst = i;
|
||||
for (i++; i < pollset_num_ - 1; i++) {
|
||||
if (pollset_[i].fd == fd) {
|
||||
pollset_num_--;
|
||||
} else {
|
||||
pollset_[dst] = pollset_[i];
|
||||
dst++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOPoll:: Poll(int time_out) {
|
||||
int rv = poll(pollset_.get(), pollset_num_, time_out);
|
||||
if (rv > 0) {
|
||||
for (int i = 0; i < pollset_num_; i++) {
|
||||
FdEvent fd_event = NONE_EVENT;
|
||||
if (pollset_[i].revents & POLLIN) {
|
||||
fd_event = | READBLE_EVENT;
|
||||
}
|
||||
if (pollset_[i].revents & POLLOUT) {
|
||||
fd_event = | WRITABLE_EVENT;
|
||||
}
|
||||
int fd = pollset_[i].fd;
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
PollFd pollfd = descriptors_[fd];
|
||||
pollfd.event_callback(fd_event);
|
||||
}
|
||||
pollset_[i].revents = NONE_EVENT;
|
||||
}
|
||||
}
|
||||
CheckTimer();
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_POLL
|
||||
@@ -22,31 +22,32 @@
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
using std::string;
|
||||
#ifndef MULTIPLE_IO_POLL_H_
|
||||
#define MULTIPLE_IO_POLL_H_
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
#include "config.h"
|
||||
#include <memory>
|
||||
|
||||
|
||||
#ifdef HAVE_POLL
|
||||
|
||||
namespace livox {
|
||||
|
||||
string PrintAPRStatus(apr_status_t s) {
|
||||
char buf[128];
|
||||
apr_strerror(s, buf, sizeof(buf));
|
||||
return buf;
|
||||
}
|
||||
string PrintAPRTime(apr_time_t t) {
|
||||
char cbuf[45 + 1];
|
||||
apr_size_t sz = 45;
|
||||
apr_time_exp_t xt;
|
||||
apr_time_exp_gmt(&xt, t);
|
||||
int milli = t % 1000;
|
||||
apr_strftime(cbuf, &sz, 45, " %T.", &xt);
|
||||
string s(cbuf);
|
||||
|
||||
char buffer[33];
|
||||
sprintf(buffer, "%d", milli);
|
||||
|
||||
s += buffer;
|
||||
return s;
|
||||
}
|
||||
class MultipleIOPoll : public MultipleIOBase {
|
||||
public:
|
||||
bool PollCreate(int size);
|
||||
bool PollSetAdd(PollFd poll_fd);
|
||||
bool PollSetRemove(PollFd poll_fd);
|
||||
void Poll(int timeout);
|
||||
void PollDestroy();
|
||||
private:
|
||||
std::unique_ptr<struct pollfd[]> pollset_;
|
||||
int pollset_num_ = 0;
|
||||
int max_poll_size_ = 0;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_POLL
|
||||
#endif // MULTIPLE_IO_POLL_H_
|
||||
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "multiple_io_select.h"
|
||||
#include <thread>
|
||||
|
||||
#ifdef HAVE_SELECT
|
||||
namespace livox {
|
||||
|
||||
bool MultipleIOSelect:: PollCreate(int size) {
|
||||
FD_ZERO(&rfds_);
|
||||
FD_ZERO(&wfds_);
|
||||
//wake up fd + 1
|
||||
max_poll_size_ = size + 1;
|
||||
WakeUpInit();
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOSelect::PollDestroy() {
|
||||
WakeUpUninit();
|
||||
FD_ZERO(&rfds_);
|
||||
FD_ZERO(&wfds_);
|
||||
}
|
||||
|
||||
bool MultipleIOSelect:: PollSetAdd(PollFd poll_fd) {
|
||||
if (max_poll_size_ <= (int)descriptors_.size()) {
|
||||
return false;
|
||||
}
|
||||
int fd = poll_fd.fd;
|
||||
descriptors_[fd] = poll_fd;
|
||||
if (max_fd_ < fd) {
|
||||
max_fd_ = fd;
|
||||
}
|
||||
if (poll_fd.event & READBLE_EVENT) {
|
||||
FD_SET(fd, &rfds_);
|
||||
}
|
||||
if (poll_fd.event & WRITABLE_EVENT) {
|
||||
FD_SET(fd, &wfds_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleIOSelect:: PollSetRemove(PollFd poll_fd) {
|
||||
int fd = poll_fd.fd;
|
||||
if (descriptors_.find(fd) != descriptors_.end()) {
|
||||
descriptors_.erase(fd);
|
||||
}
|
||||
FD_CLR(fd, &rfds_);
|
||||
FD_CLR(fd, &wfds_);
|
||||
if (max_fd_ <= fd) {
|
||||
max_fd_--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipleIOSelect:: Poll(int time_out) {
|
||||
fd_set readset, writeset;
|
||||
struct timeval tv, *tvptr;
|
||||
if (descriptors_.size() == 0) {
|
||||
if (time_out > 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(time_out));
|
||||
return;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
if (time_out < 0) {
|
||||
tvptr = nullptr;
|
||||
} else {
|
||||
tv.tv_sec = (long)time_out / 1000;
|
||||
tv.tv_usec = (long)time_out % 1000;
|
||||
tvptr = &tv;
|
||||
}
|
||||
|
||||
memcpy(&readset, &rfds_, sizeof(fd_set));
|
||||
memcpy(&writeset, &wfds_, sizeof(fd_set));
|
||||
|
||||
int rv = select(max_fd_ + 1, &readset, &writeset, nullptr, tvptr);
|
||||
if (rv > 0) {
|
||||
for (auto& descriptor : descriptors_) {
|
||||
int fd = descriptor.first;
|
||||
FdEvent fd_event = NONE_EVENT;
|
||||
if (FD_ISSET(fd, &readset)) {
|
||||
fd_event |= READBLE_EVENT;
|
||||
}
|
||||
if (FD_ISSET(fd, &writeset)) {
|
||||
fd_event |= WRITABLE_EVENT;
|
||||
}
|
||||
if (fd_event != NONE_EVENT) {
|
||||
PollFd pollfd = descriptor.second;
|
||||
pollfd.event_callback(fd_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
CheckTimer();
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_SELECT
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef MULTIPLE_IO_SELECT_H_
|
||||
#define MULTIPLE_IO_SELECT_H_
|
||||
|
||||
#include "multiple_io_base.h"
|
||||
#include "config.h"
|
||||
|
||||
#ifdef HAVE_SELECT
|
||||
|
||||
namespace livox {
|
||||
|
||||
class MultipleIOSelect : public MultipleIOBase {
|
||||
public:
|
||||
bool PollCreate(int size);
|
||||
bool PollSetAdd(PollFd poll_fd);
|
||||
bool PollSetRemove(PollFd poll_fd);
|
||||
void Poll(int timeout);
|
||||
void PollDestroy();
|
||||
private:
|
||||
int max_fd_ = -1;
|
||||
fd_set rfds_;
|
||||
fd_set wfds_;
|
||||
int max_poll_size_ = 0;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // HAVE_SELECT
|
||||
#endif // MULTIPLE_IO_SELECT_H_
|
||||
@@ -24,19 +24,35 @@
|
||||
|
||||
#ifndef LIVOX_NETWORK_UTIL_H_
|
||||
#define LIVOX_NETWORK_UTIL_H_
|
||||
#include <apr_general.h>
|
||||
#include <apr_network_io.h>
|
||||
#ifdef WIN32
|
||||
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif // WIN32
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
namespace livox {
|
||||
namespace util {
|
||||
|
||||
apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool reuse_port = false, bool nonblock = true);
|
||||
typedef int socket_t;
|
||||
|
||||
socket_t CreateSocket(uint16_t port, bool nonblock = true, bool reuse_port = true);
|
||||
|
||||
void CloseSock(socket_t sock);
|
||||
|
||||
bool FindLocalIp(const struct sockaddr_in &client_addr, uint32_t &local_ip);
|
||||
|
||||
size_t RecvFrom(socket_t &sock, void *buff, size_t buf_size, int flag, struct sockaddr *addr, int* addrlen);
|
||||
|
||||
} // namespace util
|
||||
} // namespace livox
|
||||
|
||||
#endif // LIVOX_NETWORK_UTIL_H_
|
||||
@@ -0,0 +1,124 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#ifndef WIN32
|
||||
#include "base/network/network_util.h"
|
||||
#include <ifaddrs.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
namespace livox {
|
||||
namespace util {
|
||||
|
||||
socket_t CreateSocket(uint16_t port, bool nonblock, bool reuse_port) {
|
||||
int status = -1;
|
||||
int on = -1;
|
||||
int sock = -1;
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nonblock) {
|
||||
status = ioctl(sock, FIONBIO, (char*)&on);
|
||||
if (status != 0) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (reuse_port) {
|
||||
status = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
||||
(char *) &on, sizeof (on));
|
||||
if (status != 0) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
|
||||
// Filling server information
|
||||
servaddr.sin_family = AF_INET; // IPv4
|
||||
servaddr.sin_addr.s_addr = INADDR_ANY;
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
status = bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr));
|
||||
if (status != 0) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
void CloseSock(int sock) {
|
||||
if (sock > 0) {
|
||||
close(sock);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
addrs = if_addrs;
|
||||
bool found = false;
|
||||
while (if_addrs != NULL) {
|
||||
if ((if_addrs->ifa_addr != NULL) && (if_addrs->ifa_addr->sa_family == AF_INET)) // check it is IP4
|
||||
{
|
||||
// is a connected IP4 Address
|
||||
struct sockaddr_in *ifu_localaddr = (struct sockaddr_in *)if_addrs->ifa_addr;
|
||||
struct sockaddr_in *ifu_netmask = (struct sockaddr_in *)if_addrs->ifa_netmask;
|
||||
if (ifu_localaddr->sin_addr.s_addr != htonl(INADDR_ANY)) {
|
||||
if ((ifu_localaddr->sin_addr.s_addr & ifu_netmask->sin_addr.s_addr) ==
|
||||
(client_addr.sin_addr.s_addr & ifu_netmask->sin_addr.s_addr)) {
|
||||
local_ip = ifu_localaddr->sin_addr.s_addr;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if_addrs = if_addrs->ifa_next;
|
||||
}
|
||||
|
||||
if (addrs) {
|
||||
freeifaddrs(addrs);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
size_t RecvFrom(socket_t &sock, void *buff, size_t buf_size, int flag, struct sockaddr *addr, int *addrlen) {
|
||||
return recvfrom(sock, buff, buf_size, 0, addr, (socklen_t *)addrlen);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace livox
|
||||
|
||||
#endif // WIN32
|
||||
+46
-72
@@ -22,65 +22,64 @@
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "network_util.h"
|
||||
#ifdef WIN32
|
||||
#include "base/network/network_util.h"
|
||||
#include <memory>
|
||||
#include <Winsock2.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <string>
|
||||
#pragma comment(lib,"iphlpapi.lib")
|
||||
#pragma comment(lib,"ws2_32.lib")
|
||||
#else
|
||||
#include <ifaddrs.h>
|
||||
#endif
|
||||
|
||||
namespace livox {
|
||||
namespace util {
|
||||
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;
|
||||
|
||||
void CloseSock(socket_t sock) {
|
||||
closesocket(sock);
|
||||
}
|
||||
|
||||
|
||||
rv = apr_sockaddr_info_get(&sa, NULL, APR_INET, port, 0, mem_pool);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rv = apr_socket_create(&s, sa->family, SOCK_DGRAM, APR_PROTO_UDP, mem_pool);
|
||||
if (rv != APR_SUCCESS) {
|
||||
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);
|
||||
s = NULL;
|
||||
return NULL;
|
||||
socket_t CreateSocket(uint16_t port, bool nonblock, bool reuse_port) {
|
||||
int status = -1;
|
||||
int on = -1;
|
||||
int sock = -1;
|
||||
struct sockaddr_in servaddr;
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nonblock) {
|
||||
apr_socket_opt_set(s, APR_SO_NONBLOCK, 1);
|
||||
apr_socket_timeout_set(s, 0);
|
||||
status = ioctlsocket(sock, FIONBIO, (u_long *)&on);
|
||||
if (status != NO_ERROR) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
|
||||
if (reuse_port) {
|
||||
status = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
||||
(char *) &on, sizeof (on));
|
||||
if (status != 0) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
// Filling server information
|
||||
servaddr.sin_family = AF_INET; // IPv4
|
||||
servaddr.sin_addr.s_addr = INADDR_ANY;
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
status = bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr));
|
||||
if (status != 0) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
bool GetAdapterState(const IP_ADAPTER_INFO *pAdapter)
|
||||
{
|
||||
bool GetAdapterState(const IP_ADAPTER_INFO *pAdapter) {
|
||||
if(pAdapter == NULL) {
|
||||
return false;
|
||||
}
|
||||
@@ -129,37 +128,12 @@ bool FindLocalIp(const struct sockaddr_in &client_addr, uint32_t &local_ip) {
|
||||
}
|
||||
return found;
|
||||
}
|
||||
#else
|
||||
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;
|
||||
}
|
||||
addrs = if_addrs;
|
||||
bool found = false;
|
||||
while (if_addrs != NULL) {
|
||||
if ((if_addrs->ifa_addr != NULL) && (if_addrs->ifa_addr->sa_family == AF_INET)) // check it is IP4
|
||||
{
|
||||
// is a connected IP4 Address
|
||||
struct sockaddr_in *ifu_localaddr = (struct sockaddr_in *)if_addrs->ifa_addr;
|
||||
struct sockaddr_in *ifu_netmask = (struct sockaddr_in *)if_addrs->ifa_netmask;
|
||||
if (ifu_localaddr->sin_addr.s_addr != htonl(INADDR_ANY)) {
|
||||
if ((ifu_localaddr->sin_addr.s_addr & ifu_netmask->sin_addr.s_addr) ==
|
||||
(client_addr.sin_addr.s_addr & ifu_netmask->sin_addr.s_addr)) {
|
||||
local_ip = ifu_localaddr->sin_addr.s_addr;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if_addrs = if_addrs->ifa_next;
|
||||
}
|
||||
|
||||
if (addrs) {
|
||||
freeifaddrs(addrs);
|
||||
}
|
||||
return found;
|
||||
size_t RecvFrom(socket_t &sock, void *buff, size_t buf_size, int flag, struct sockaddr *addr, int* addrlen) {
|
||||
return recvfrom(sock, (char *)buff, buf_size, 0, addr, addrlen);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace util
|
||||
} // namespace livox
|
||||
|
||||
#endif // WIN32
|
||||
@@ -23,49 +23,30 @@
|
||||
//
|
||||
|
||||
#include "thread_base.h"
|
||||
#include <thread>
|
||||
|
||||
namespace livox {
|
||||
static void *APR_THREAD_FUNC ClassThreadHelperFunc(apr_thread_t *thd, void *data) {
|
||||
ThreadBase *caller = static_cast<ThreadBase *>(data);
|
||||
if (caller == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
caller->ThreadFunc();
|
||||
apr_thread_exit(thd, APR_SUCCESS);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ThreadBase::ThreadBase() : thread_(NULL), quit_(false), pool_(NULL) {}
|
||||
ThreadBase::ThreadBase() : quit_(false), is_thread_valid_(false) {}
|
||||
|
||||
bool ThreadBase::Start() {
|
||||
quit_ = false;
|
||||
apr_threadattr_t *thread_attr = NULL;
|
||||
apr_status_t rv = APR_SUCCESS;
|
||||
rv = apr_threadattr_create(&thread_attr, pool_);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
rv = apr_thread_create(&thread_, thread_attr, ClassThreadHelperFunc, this, pool_);
|
||||
return rv == APR_SUCCESS;
|
||||
thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this);
|
||||
is_thread_valid_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ThreadBase::Join() {
|
||||
apr_status_t rv = APR_SUCCESS;
|
||||
if (thread_) {
|
||||
apr_thread_join(&rv, thread_);
|
||||
thread_ = NULL;
|
||||
if (is_thread_valid_) {
|
||||
thread_->join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ThreadBase::Init() {
|
||||
return apr_pool_create(&pool_, NULL) == APR_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ThreadBase::Uninit() {
|
||||
if (pool_) {
|
||||
apr_pool_destroy(pool_);
|
||||
pool_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
@@ -24,10 +24,8 @@
|
||||
|
||||
#ifndef LIVOX_THREAD_BASE_H_
|
||||
#define LIVOX_THREAD_BASE_H_
|
||||
#include <apr_general.h>
|
||||
#include <apr_thread_proc.h>
|
||||
#include <apr_portable.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include "noncopyable.h"
|
||||
|
||||
namespace livox {
|
||||
@@ -45,9 +43,11 @@ class ThreadBase : public noncopyable {
|
||||
bool IsQuit() { return quit_; }
|
||||
|
||||
protected:
|
||||
apr_thread_t *thread_;
|
||||
std::shared_ptr<std::thread> thread_;
|
||||
std::atomic_bool quit_;
|
||||
apr_pool_t *pool_;
|
||||
|
||||
private:
|
||||
std::atomic_bool is_thread_valid_;
|
||||
};
|
||||
|
||||
} // namespace livox
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef WIN32
|
||||
#include "base/wake_up/wake_up_pipe.h"
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace livox {
|
||||
|
||||
WakeUpPipe::~WakeUpPipe() {
|
||||
PipeDestroy();
|
||||
}
|
||||
|
||||
bool WakeUpPipe::WakeUp() {
|
||||
char ch = '1';
|
||||
ssize_t nbytes = sizeof(ch);
|
||||
if (pipe_in_ > 0) {
|
||||
if (nbytes != write(pipe_in_, &ch, nbytes)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::Drain() {
|
||||
char ch[512];
|
||||
size_t size = sizeof(ch);
|
||||
if (pipe_out_ > 0) {
|
||||
if (read(pipe_out_, ch, size) < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::PipeDestroy() {
|
||||
if (pipe_in_ > 0) {
|
||||
close(pipe_in_);
|
||||
}
|
||||
if (pipe_out_ > 0) {
|
||||
close(pipe_out_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::PipeCreate() {
|
||||
//in filedes[0]
|
||||
//out filedes[1]
|
||||
int filedes[2];
|
||||
if (pipe(filedes) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int flags = 0;
|
||||
if ((flags = fcntl(filedes[0], F_GETFL|O_NONBLOCK)) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
flags |= FD_CLOEXEC;
|
||||
if (fcntl(filedes[0], F_SETFL, flags) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
flags = 0;
|
||||
if ((flags = fcntl(filedes[1], F_GETFD)) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
flags |= FD_CLOEXEC;
|
||||
if (fcntl(filedes[1], F_SETFD, flags) == -1) {
|
||||
return false;
|
||||
}
|
||||
pipe_out_ = filedes[0];
|
||||
pipe_in_ = filedes[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // WIN32
|
||||
@@ -22,16 +22,24 @@
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef LIVOX_UTIL_H_
|
||||
#define LIVOX_UTIL_H_
|
||||
#include <string>
|
||||
#include "apr_general.h"
|
||||
#include "apr_time.h"
|
||||
#ifndef WAKE_UP_PIPE_H_
|
||||
#define WAKE_UP_PIPE_H_
|
||||
|
||||
namespace livox {
|
||||
class WakeUpPipe {
|
||||
public:
|
||||
WakeUpPipe(): pipe_in_(0), pipe_out_(0) {}
|
||||
virtual ~WakeUpPipe();
|
||||
bool PipeCreate();
|
||||
bool PipeDestroy();
|
||||
bool WakeUp();
|
||||
bool Drain();
|
||||
int GetPipeOut() { return pipe_out_; }
|
||||
protected:
|
||||
int pipe_in_;
|
||||
int pipe_out_;
|
||||
};
|
||||
|
||||
std::string PrintAPRStatus(apr_status_t s);
|
||||
std::string PrintAPRTime(apr_time_t t);
|
||||
} // namespace livox
|
||||
|
||||
} // namespace livox
|
||||
#endif // LIVOX_UTIL_H_
|
||||
#endif // WAKE_UP_PIPE_H_
|
||||
@@ -0,0 +1,132 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2019 Livox. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#ifdef WIN32
|
||||
#include "base/wake_up/wake_up_pipe.h"
|
||||
#include <fcntl.h>
|
||||
#include <winsock2.h>
|
||||
#include <stdio.h>
|
||||
#pragma comment(lib,"iphlpapi.lib")
|
||||
#pragma comment(lib,"ws2_32.lib")
|
||||
|
||||
namespace livox {
|
||||
|
||||
WakeUpPipe::~WakeUpPipe() {
|
||||
PipeDestroy();
|
||||
}
|
||||
|
||||
bool WakeUpPipe::WakeUp() {
|
||||
char ch = '1';
|
||||
size_t nbytes = sizeof(ch);
|
||||
if (pipe_in_ > 0) {
|
||||
if (nbytes != send(pipe_in_,&ch, nbytes, 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::Drain() {
|
||||
char ch[512];
|
||||
size_t size = sizeof(ch);
|
||||
if (pipe_out_ > 0) {
|
||||
recv(pipe_out_, ch, size, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::PipeDestroy() {
|
||||
if (pipe_in_ > 0) {
|
||||
closesocket(pipe_in_);
|
||||
}
|
||||
if (pipe_out_ > 0) {
|
||||
closesocket(pipe_out_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WakeUpPipe::PipeCreate() {
|
||||
int listen_sock = -1;
|
||||
unsigned long on = 1;
|
||||
bool status = false;
|
||||
if ((listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
int servaddr_len = sizeof(servaddr);
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = 0;
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
|
||||
do {
|
||||
if (bind(listen_sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {
|
||||
break;
|
||||
}
|
||||
if (getsockname(listen_sock, (struct sockaddr *)&servaddr, &servaddr_len) == SOCKET_ERROR) {
|
||||
break;
|
||||
}
|
||||
if (listen(listen_sock, 1) == SOCKET_ERROR) {
|
||||
break;
|
||||
}
|
||||
if ((pipe_in_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
if (connect(pipe_in_, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {
|
||||
break;
|
||||
}
|
||||
if (ioctlsocket(listen_sock, FIONBIO, &on) == SOCKET_ERROR) {
|
||||
break;
|
||||
}
|
||||
|
||||
struct sockaddr_in clientaddr;
|
||||
int clientaddr_len = sizeof(clientaddr);
|
||||
fd_set poll_set;
|
||||
FD_ZERO(&poll_set);
|
||||
FD_SET(listen_sock, &poll_set);
|
||||
// timeout 2s
|
||||
struct timeval timeout = {2, 0};
|
||||
int rv = select(0, &poll_set, nullptr, nullptr, &timeout);
|
||||
if (rv <= 0) {
|
||||
break;
|
||||
}
|
||||
if ((pipe_out_ = accept(listen_sock, (struct sockaddr *)&clientaddr, &clientaddr_len)) == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
status = true;
|
||||
} while(0);
|
||||
|
||||
closesocket(listen_sock);
|
||||
if (!status) {
|
||||
if (pipe_in_ > 0) {
|
||||
closesocket(pipe_in_);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace livox
|
||||
|
||||
#endif // WIN32
|
||||
Reference in New Issue
Block a user