IXWebSocket/ixwebsocket/IXWebSocketMessageQueue.h

54 lines
1.2 KiB
C
Raw Normal View History

2019-05-08 21:02:56 +02:00
/*
2019-05-08 23:16:37 +02:00
* IXWebSocketMessageQueue.h
2019-05-08 21:02:56 +02:00
* Author: Korchynskyi Dmytro
2019-05-08 23:16:37 +02:00
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
2019-05-08 21:02:56 +02:00
*/
#pragma once
#include "IXWebSocket.h"
#include <thread>
#include <list>
#include <memory>
namespace ix
{
2019-05-08 23:16:37 +02:00
//
// A helper class to dispatch websocket message callbacks in your thread.
//
2019-05-08 23:09:51 +02:00
class WebSocketMessageQueue
2019-05-08 21:02:56 +02:00
{
public:
2019-05-08 23:09:51 +02:00
WebSocketMessageQueue(WebSocket* websocket = nullptr);
~WebSocketMessageQueue();
2019-05-08 21:02:56 +02:00
void bindWebsocket(WebSocket* websocket);
void setOnMessageCallback(const OnMessageCallback& callback);
2019-05-12 19:59:18 +02:00
void setOnMessageCallback(OnMessageCallback&& callback);
2019-05-08 21:02:56 +02:00
void poll(int count = 512);
protected:
struct Message
{
WebSocketMessageType type;
std::string str;
size_t wireSize;
WebSocketErrorInfo errorInfo;
WebSocketOpenInfo openInfo;
WebSocketCloseInfo closeInfo;
};
2019-05-09 00:05:47 +02:00
using MessagePtr = std::shared_ptr<Message>;
2019-05-08 21:02:56 +02:00
2019-05-09 00:05:47 +02:00
MessagePtr popMessage();
2019-05-08 21:02:56 +02:00
private:
WebSocket* _websocket = nullptr;
OnMessageCallback _onMessageUserCallback;
std::mutex _messagesMutex;
2019-05-09 00:05:47 +02:00
std::list<MessagePtr> _messages;
2019-05-08 21:02:56 +02:00
};
}