SocketConnection.h [plain text]
#pragma once
#include <wtf/Function.h>
#include <wtf/HashMap.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GSocketMonitor.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/text/CString.h>
typedef struct _GSocketConnection GSocketConnection;
namespace WTF {
class SocketConnection : public RefCounted<SocketConnection> {
public:
typedef void (* MessageCallback)(SocketConnection&, GVariant*, gpointer);
using MessageHandlers = HashMap<CString, std::pair<CString, MessageCallback>>;
static Ref<SocketConnection> create(GRefPtr<GSocketConnection>&& connection, const MessageHandlers& messageHandlers, gpointer userData)
{
return adoptRef(*new SocketConnection(WTFMove(connection), messageHandlers, userData));
}
WTF_EXPORT_PRIVATE ~SocketConnection();
WTF_EXPORT_PRIVATE void sendMessage(const char*, GVariant*);
bool isClosed() const { return !m_connection; }
void close();
private:
WTF_EXPORT_PRIVATE SocketConnection(GRefPtr<GSocketConnection>&&, const MessageHandlers&, gpointer);
bool read();
bool readMessage();
void write();
void waitForSocketWritability();
void didClose();
GRefPtr<GSocketConnection> m_connection;
const MessageHandlers& m_messageHandlers;
gpointer m_userData;
Vector<char> m_readBuffer;
GSocketMonitor m_readMonitor;
Vector<char> m_writeBuffer;
GSocketMonitor m_writeMonitor;
};
}
using WTF::SocketConnection;