I use TCPServer (POCO C ++ Libraries), which creates a stream for 1 client.

It is necessary to transfer to the client (stream), for example, data on the movement of characters in the game world.

How to implement it?

  • As I understand it, you need to use events. One stream lays, another reads. - Tarakan

1 answer 1

For transfer between threads in POCO, there is a class NotificationQueue . I added the output of informational messages to the example from here and translated the comments:

#include <iostream> #include <string> #include "Poco/Notification.h" #include "Poco/NotificationQueue.h" #include "Poco/ThreadPool.h" #include "Poco/Runnable.h" #include "Poco/AutoPtr.h" using Poco::Notification; using Poco::NotificationQueue; using Poco::ThreadPool; using Poco::Runnable; using Poco::AutoPtr; class WorkNotification: public Notification { public: WorkNotification(int data): _data(data) {} int data() const { return _data; } private: int _data; }; class Worker: public Runnable { public: Worker(NotificationQueue& queue, const std::string& str): _queue(queue),name(str) {} void run() { AutoPtr<Notification> pNf(_queue.waitDequeueNotification()); while (pNf) { WorkNotification* pWorkNf = dynamic_cast<WorkNotification*>(pNf.get()); if (pWorkNf) { std::cout<<name<<" get data : "<<pWorkNf->data()<<"\n"; } pNf = _queue.waitDequeueNotification(); } } private: NotificationQueue& _queue; std::string name; }; int main() { NotificationQueue queue; //создаем рабочие процессы Worker worker1(queue,"first"); Worker worker2(queue,"second"); // запускаем ThreadPool::defaultPool().start(worker1); ThreadPool::defaultPool().start(worker2); // добавляем уведомления for (int i = 0; i < 10; ++i) queue.enqueueNotification(new WorkNotification(i)); while (!queue.empty()) // ждем пока работы выполняются Poco::Thread::sleep(100); queue.wakeUpAll(); // говорим рабочим о выполнении ThreadPool::defaultPool().joinAll(); return 0; }