From 510a26933bfd8c74a38cefcdfd748e56f47fcf43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Fri, 10 Apr 2020 20:14:23 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@1009 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- cmake/DeployMacros.cmake | 3 ++ main.cpp | 109 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/cmake/DeployMacros.cmake b/cmake/DeployMacros.cmake index e9989141..9c891dc2 100644 --- a/cmake/DeployMacros.cmake +++ b/cmake/DeployMacros.cmake @@ -642,11 +642,14 @@ macro(deploy_target _T) set(_AT ${_T}_lib) get_target_property(_sources ${_T} SOURCES) get_target_property(_libs ${_T} LINK_LIBRARIES) + get_target_property(_incs ${_T} INCLUDE_DIRECTORIES) add_library(${_AT} SHARED ${_sources}) target_link_libraries(${_AT} ${_libs}) + target_include_directories(${_AT} PRIVATE ${_incs}) message("create new target ${_AT} ${_sources} ${_libs}") endif() #get_target_property(_LL ${_T} ) + add_custom_target(deploy_${_T}) set(_has_deploy 1) endif() if (_has_deploy) diff --git a/main.cpp b/main.cpp index 0888af43..6eb3f76e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,9 @@ #include "pip.h" +#include +#include +#include +#include +#include void print(PIConfig::Entry*e, PIString indent = "") { piCout << indent << e->name() << "=" << e->value(); @@ -6,7 +11,109 @@ void print(PIConfig::Entry*e, PIString indent = "") { e->children().forEach([=](PIConfig::Entry*e)->PIConfig::Entry*{print(e, indent); return e;}); } -int main() { +class AsyncIOWatcher: public PIThread { + PIOBJECT_SUBCLASS(AsyncIOWatcher, PIThread) +public: + AsyncIOWatcher() { + pipe_fd[0] = pipe_fd[1] = 0; + if (pipe(pipe_fd) != 0) { + piCoutObj << "Warning: can`t create pipe," << errorString(); + } else { + fd_list << pipe_fd[0]; + } + piCout << pipe_fd[0] << pipe_fd[1]; + fd_list_changed = false; + start(); + } + ~AsyncIOWatcher() { + stop(); + breakSelect(); + if (!waitForFinish(2000)) + terminate(); + if (pipe_fd[0]) ::close(pipe_fd[0]); + if (pipe_fd[1]) ::close(pipe_fd[1]); + } + + void add(int fd) { + que_mutex.lock(); + fd_list_changed = true; + if (!add_que.contains(fd)) + add_que.enqueue(fd); + que_mutex.unlock(); + breakSelect(); + } + void remove(int fd) { + que_mutex.lock(); + fd_list_changed = true; + if (!remove_que.contains(fd)) + remove_que.enqueue(fd); + que_mutex.unlock(); + breakSelect(); + } + +private: + virtual void run() { + que_mutex.lock(); + if (fd_list_changed) { + for (int i = 0; i < add_que.size_s(); ++i) { + if (!fd_list.contains(add_que[i])) + fd_list << add_que[i]; + } + for (int i = 0; i < remove_que.size_s(); ++i) { + fd_list.removeAll(remove_que[i]); + } + add_que.clear(); + remove_que.clear(); + } + fd_list_changed = false; + que_mutex.unlock(); + + s_tv.tv_sec = 1; + s_tv.tv_usec = 0; + FD_ZERO(&s_set); + int max_fd = 0; + piForeachC (int fd, fd_list) { + FD_SET(fd, &s_set); + if (max_fd < fd) + max_fd = fd; + } + int ret = select(max_fd + 1, &s_set, 0, 0, 0); + piCout << "select" << ret; + if (ret <= 0) return; + read_buff.resize(1024); + uint ibuff = 0; + piForeachC (int fd, fd_list) { + if (!FD_ISSET(fd, &s_set)) continue; + if (fd == pipe_fd[0]) { + read(fd, &ibuff, sizeof(ibuff)); + piCoutObj << "breaked"; + continue; + } + int readed = read(fd, read_buff.data(), read_buff.size_s()); + piCout << "readed" << fd << readed; + } + } + void breakSelect() { + if (pipe_fd[1]) + ::write(pipe_fd[1], "\0", 1); + } + + PIQueue add_que, remove_que; + PIDeque fd_list; + PIByteArray read_buff; + PIMutex que_mutex; + bool fd_list_changed; + int pipe_fd[2]; + + fd_set s_set; + struct timeval s_tv; + +}; + + +PIKbdListener kbd; + +int main(int argc, char * argv[]) { PIStringList dl = PISerial::availableDevices(); piCout << dl; PISerial ser(dl[0]);