85 lines
1.9 KiB
C++
85 lines
1.9 KiB
C++
/*
|
|
* (c) Copyright, Real-Time Innovations, 2024.
|
|
* All rights reserved.
|
|
*
|
|
* No duplications, whole or partial, manual or electronic, may be made
|
|
* without express written permission. Any such copies, or
|
|
* revisions thereof, must display this notice unaltered.
|
|
* This code contains trade secrets of Real-Time Innovations, Inc.
|
|
*/
|
|
|
|
#ifndef CIVETWEBSOCKET_CONNECTION_H
|
|
#define CIVETWEBSOCKET_CONNECTION_H
|
|
|
|
#include <civetweb.h>
|
|
|
|
#include "WebSocketConnection.hpp"
|
|
|
|
namespace rti { namespace apputils { namespace websocket { namespace civetweb {
|
|
|
|
class CivetWebSocketConnection : public WebSocketConnection {
|
|
public:
|
|
explicit CivetWebSocketConnection(struct mg_connection *conn) : conn_(conn)
|
|
{
|
|
}
|
|
|
|
void send(const StringBuffer& msg)
|
|
{
|
|
struct mg_context *context = mg_get_context(conn_);
|
|
|
|
mg_lock_context(context);
|
|
|
|
mg_websocket_write(
|
|
conn_,
|
|
MG_WEBSOCKET_OPCODE_TEXT,
|
|
msg.c_str(),
|
|
msg.length());
|
|
|
|
mg_unlock_context(context);
|
|
}
|
|
|
|
void send_pong(const StringBuffer& msg)
|
|
{
|
|
mg_context *context = mg_get_context(conn_);
|
|
|
|
mg_lock_context(context);
|
|
|
|
mg_websocket_write(
|
|
conn_,
|
|
MG_WEBSOCKET_OPCODE_PONG,
|
|
msg.c_str(),
|
|
msg.length());
|
|
|
|
mg_unlock_context(context);
|
|
}
|
|
|
|
|
|
bool operator==(WebSocketConnection* conn)
|
|
{
|
|
return conn_ == static_cast<CivetWebSocketConnection *>(conn)->conn();
|
|
}
|
|
|
|
const struct mg_connection* conn() const
|
|
{
|
|
return conn_;
|
|
}
|
|
|
|
void close()
|
|
{
|
|
struct mg_context *context = mg_get_context(conn_);
|
|
|
|
mg_lock_context(context);
|
|
|
|
mg_close_connection(conn_);
|
|
|
|
mg_unlock_context(context);
|
|
}
|
|
|
|
private:
|
|
mg_connection* conn_;
|
|
};
|
|
|
|
}}}} // namespace rti::apputils::websocket::civetweb
|
|
|
|
#endif // CIVETWEBSOCKET_CONNECTION_H
|