add demo-chat

This commit is contained in:
mutouyun 2019-02-13 15:48:55 +08:00
parent bffa8ae6b8
commit e88a4cf865
2 changed files with 54 additions and 0 deletions

17
build/chat.pro Normal file
View File

@ -0,0 +1,17 @@
TEMPLATE = app
CONFIG += console
CONFIG += c++14 c++1z # may be useless
CONFIG -= app_bundle
CONFIG -= qt
DESTDIR = ../output
INCLUDEPATH += \
../include
SOURCES += \
../demo/chat/main.cpp
LIBS += \
-L$${DESTDIR} -lipc

37
demo/chat/main.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <thread>
#include "ipc.h"
namespace {
char name__[] = "ipc-chat";
char quit__[] = "q";
} // namespace
int main() {
std::string buf;
ipc::channel cc { name__ };
std::thread r {[] {
ipc::channel cc { name__ };
while (1) {
auto buf = cc.recv();
if (buf.empty()) continue;
std::string dat { static_cast<char const *>(buf.data()), buf.size() - 1 };
if (dat == quit__) return;
std::cout << dat << std::endl;
}
}};
while (1) {
std::cin >> buf;
cc.send(buf);
if (buf == quit__) break;
}
r.join();
return 0;
}