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
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* websocket <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
2018-10-20 20:36:08 +08:00
|
|
|
|
virtual void onClose(void) {}
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket ping <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
2018-12-06 15:50:13 +08:00
|
|
|
|
* @param payload_len {unsigned long long} <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param finish {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
|
2017-05-31 14:43:18 +08:00
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-12-06 15:50:13 +08:00
|
|
|
|
virtual bool onPing(unsigned long long payload_len, bool finish) = 0;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket pong <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
2018-12-06 15:50:13 +08:00
|
|
|
|
* @param payload_len {unsigned long long} <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param finish {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
|
2017-05-31 14:43:18 +08:00
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-12-06 15:50:13 +08:00
|
|
|
|
virtual bool onPong(unsigned long long payload_len, bool finish) = 0;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket ping <EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD>.
|
2018-12-06 15:50:13 +08:00
|
|
|
|
* @param payload_len {unsigned long long} <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><EFBFBD>ݡ<EFBFBD>
|
|
|
|
|
* @param finish {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
|
2017-05-31 14:43:18 +08:00
|
|
|
|
* @return {bool} false <EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|
|
|
|
*/
|
2018-12-06 15:50:13 +08:00
|
|
|
|
virtual bool onMessage(unsigned long long payload_len,
|
|
|
|
|
bool text, bool finish) = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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> <= 0 Ϊֹ
|
|
|
|
|
* @param buf {size_t*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param size {size_t} buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
|
|
|
|
* @return {int} <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>
|
|
|
|
|
* 0: <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* -1: <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* >0: <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>
|
|
|
|
|
*/
|
|
|
|
|
int readPayload(void* buf, size_t size);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> websocket <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> websocket <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {websocket*}
|
|
|
|
|
*/
|
|
|
|
|
websocket* get_websocket(void) const
|
|
|
|
|
{
|
|
|
|
|
return ws_;
|
|
|
|
|
}
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// @override
|
2018-12-06 15:50:13 +08:00
|
|
|
|
bool doWebSocket(HttpServletRequest&, HttpServletResponse&);
|
2017-05-31 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
websocket *ws_;
|
2018-10-20 00:30:02 +08:00
|
|
|
|
int opcode_;
|
2017-05-31 14:43:18 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|