IXWebSocket/ixredis/ixredis/IXRedisServer.h

66 lines
2.2 KiB
C
Raw Normal View History

/*
* IXRedisServer.h
* Author: Benjamin Sergeant
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketServer.h>
#include <functional>
2020-04-20 22:59:20 -07:00
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <thread>
#include <utility> // pair
namespace ix
{
class RedisServer final : public SocketServer
{
public:
RedisServer(int port = SocketServer::kDefaultPort,
const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections,
int addressFamily = SocketServer::kDefaultAddressFamily);
virtual ~RedisServer();
virtual void stop() final;
private:
// Member variables
std::atomic<int> _connectedClientsCount;
// Subscribers
// We could store connection states in there, to add better debugging
// since a connection state has a readable ID
2020-03-24 12:40:58 -07:00
std::map<std::string, std::set<Socket*>> _subscribers;
std::mutex _mutex;
std::atomic<bool> _stopHandlingConnections;
// Methods
2020-03-24 12:40:58 -07:00
virtual void handleConnection(std::unique_ptr<Socket>,
std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) final;
virtual size_t getConnectedClientsCount() final;
bool startsWith(const std::string& str, const std::string& start);
std::string writeString(const std::string& str);
2020-04-20 22:59:20 -07:00
bool parseRequest(std::unique_ptr<Socket>& socket, std::vector<std::string>& tokens);
2020-04-20 22:59:20 -07:00
bool handlePublish(std::unique_ptr<Socket>& socket, const std::vector<std::string>& tokens);
2020-03-24 12:40:58 -07:00
bool handleSubscribe(std::unique_ptr<Socket>& socket,
const std::vector<std::string>& tokens);
2020-04-20 22:59:20 -07:00
bool handleCommand(std::unique_ptr<Socket>& socket, const std::vector<std::string>& tokens);
2020-03-24 12:40:58 -07:00
void cleanupSubscribers(std::unique_ptr<Socket>& socket);
};
} // namespace ix