2017-05-20 11:39:14 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
|
#include "HttpServlet.hpp"
|
2017-05-20 11:39:14 +08:00
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
class websocket;
|
|
|
|
|
class session;
|
2018-10-20 00:30:02 +08:00
|
|
|
|
class HttpServletRequest;
|
|
|
|
|
class HttpServletResponse;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
class WebSocketServlet: public HttpServlet
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
WebSocketServlet(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|
|
|
|
* @param stream {socket_stream*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl_master <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿգ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> apache <EFBFBD><EFBFBD><EFBFBD><EFBFBD> CGI <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD>Ϊ NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>Ӧ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD>Ĺر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl_master <EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param session {session*} ÿһ<EFBFBD><EFBFBD> HttpServlet <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> session <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
WebSocketServlet(socket_stream* stream, session* session);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|
|
|
|
* @param stream {socket_stream*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl_master <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿգ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> apache <EFBFBD><EFBFBD><EFBFBD><EFBFBD> CGI <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD>Ϊ NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>Ӧ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD>Ĺر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl_master <EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param memcache_addr {const char*}
|
|
|
|
|
*/
|
|
|
|
|
WebSocketServlet(socket_stream* stream,
|
|
|
|
|
const char* memcache_addr = "127.0.0.1:11211");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HttpServlet <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HTTP <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> doXXX <EFBFBD>麯<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|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
|
|
|
|
|
|
|
virtual ~WebSocketServlet(void);
|
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
// @override
|
|
|
|
|
bool doRun(void);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
// @override
|
|
|
|
|
bool doRun(session& session, socket_stream* stream = NULL);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
// @override
|
|
|
|
|
bool doRun(const char* memcached_addr, socket_stream* stream);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
* @param rw_timeout {const char *} <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false.<EFBFBD><EFBFBD><EFBFBD><EFBFBD> true
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
bool sendBinary(const char *buf, int len);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
* @param rw_timeout {const char *} <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false.<EFBFBD><EFBFBD><EFBFBD><EFBFBD> true
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
bool sendText(const char *text);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>pong <EFBFBD><EFBFBD>Ϣ.
|
|
|
|
|
* @param rw_timeout {const char *} <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false.<EFBFBD><EFBFBD><EFBFBD><EFBFBD> true
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
bool sendPong(const char *buffer = NULL);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>pong <EFBFBD><EFBFBD>Ϣ.
|
|
|
|
|
* @param rw_timeout {const char *} <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false.<EFBFBD><EFBFBD><EFBFBD><EFBFBD> true
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
bool sendPing(const char *buffer = NULL);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
unsigned long long getMaxMsgLen(void) const
|
2017-05-31 14:43:18 +08:00
|
|
|
|
{
|
|
|
|
|
return max_msg_len_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD>websocket <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD>websocket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
* @param unsigned long long{len} <EFBFBD>µij<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
void setMaxMsgLen(unsigned long long len)
|
2017-05-20 11:39:14 +08:00
|
|
|
|
{
|
2017-05-31 14:43:18 +08:00
|
|
|
|
max_msg_len_ = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* websocket <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
virtual void onClose(void);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket ping <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
|
|
|
|
* @param {const char *} buf <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param {int} len <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
virtual bool onPing(const char *buf, unsigned long long len) = 0;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket pong <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
|
|
|
|
* @param {const char *} buf <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param {int} len <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
virtual bool onPong(const char *buf, unsigned long long len) = 0;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket ping <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
|
|
|
|
* @param data{char *} <EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param len{unsigned long long}<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
|
|
|
|
|
* @param text{bool } true Ϊ<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-10-20 00:30:02 +08:00
|
|
|
|
virtual bool onMessage(char *data, unsigned long long len, bool text) = 0;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// @override
|
|
|
|
|
bool doWebsocket(HttpServletRequest&, HttpServletResponse&);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
unsigned long long max_msg_len_;
|
|
|
|
|
websocket *ws_;
|
|
|
|
|
|
2018-10-20 00:30:02 +08:00
|
|
|
|
char *buffer_;
|
|
|
|
|
int wpos_;
|
|
|
|
|
int opcode_;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|