git-svn-id: svn://db.shs.com.ru/pip@1009 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2020-04-10 20:14:23 +00:00
parent 4b34dbdf45
commit 510a26933b
2 changed files with 111 additions and 1 deletions

View File

@@ -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)

109
main.cpp
View File

@@ -1,4 +1,9 @@
#include "pip.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/select.h>
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<int> add_que, remove_que;
PIDeque<int> 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]);