acl/lib_acl_cpp/include/acl_cpp/http/WebSocketServlet.hpp

153 lines
4.5 KiB
C++
Raw Normal View History

#pragma once
2018-12-12 11:16:12 +08:00
#include "../acl_cpp_define.hpp"
#include "HttpServlet.hpp"
#ifndef ACL_CLIENT_ONLY
2018-12-12 11:16:12 +08:00
namespace acl
{
class websocket;
class session;
class HttpServletRequest;
class HttpServletResponse;
class ACL_CPP_API WebSocketServlet : public HttpServlet
{
public:
WebSocketServlet(void);
/**
*
* @param stream {socket_stream*} acl_master
* apache CGI
* NULL
* 便 acl_master
* @param session {session*} HttpServlet session
2018-12-12 11:16:12 +08:00
*/
WebSocketServlet(socket_stream* stream, session* session);
/**
*
* @param stream {socket_stream*} acl_master
* apache CGI
* NULL
* 便 acl_master
2018-12-12 11:16:12 +08:00
* @param memcache_addr {const char*}
*/
WebSocketServlet(socket_stream* stream,
const char* memcache_addr = "127.0.0.1:11211");
/**
* HttpServlet HTTP doXXX
* start start /
* .
* @return {bool} false
*
2018-12-12 11:16:12 +08:00
*/
virtual ~WebSocketServlet(void);
// @override
bool doRun(void);
// @override
bool doRun(session& session, socket_stream* stream = NULL);
// @override
bool doRun(const char* memcached_addr, socket_stream* stream);
/**
* .
* @param rw_timeout {const char *}
* @return {bool} false. true
2018-12-12 11:16:12 +08:00
*/
bool sendBinary(const char *buf, int len);
/**
* .
* @param rw_timeout {const char *}
* @return {bool} false. true
2018-12-12 11:16:12 +08:00
*/
bool sendText(const char *text);
/**
* pong .
* @param rw_timeout {const char *}
* @return {bool} false. true
2018-12-12 11:16:12 +08:00
*/
bool sendPong(const char *buffer = NULL);
/**
* pong .
* @param rw_timeout {const char *}
* @return {bool} false. true
2018-12-12 11:16:12 +08:00
*/
bool sendPing(const char *buffer = NULL);
protected:
/**
* websocket
2018-12-12 11:16:12 +08:00
* @return {void}
*/
virtual void onClose(void) {}
/**
* websocket ping .
* @param payload_len {unsigned long long}
* @param finish {bool}
* @return {bool} false
2018-12-12 11:16:12 +08:00
*/
virtual bool onPing(unsigned long long payload_len, bool finish) = 0;
/**
* websocket pong .
* @param payload_len {unsigned long long}
* @param finish {bool}
* @return {bool} false
2018-12-12 11:16:12 +08:00
*/
virtual bool onPong(unsigned long long payload_len, bool finish) = 0;
/**
* websocket ping .
* @param payload_len {unsigned long long}
* @param text {bool } true ,
* @param finish {bool}
* @return {bool} false
2018-12-12 11:16:12 +08:00
*/
virtual bool onMessage(unsigned long long payload_len,
bool text, bool finish) = 0;
/**
* <= 0
* @param buf {size_t*}
* @param size {size_t} buf
* @return {int}
* 0:
* -1:
* >0: 便
2018-12-12 11:16:12 +08:00
*/
int readPayload(void* buf, size_t size);
/**
* websocket NULL websocket
2018-12-12 11:16:12 +08:00
* @return {websocket*}
*/
websocket* get_websocket(void) const
{
return ws_;
}
private:
// @override
bool doWebSocket(HttpServletRequest&, HttpServletResponse&);
private:
websocket *ws_;
int opcode_;
};
} // namespace acl
#endif // ACL_CLIENT_ONLY