add WebSocketServlet by "fuwangqin" <niukey@qq.com>

This commit is contained in:
root 2017-05-31 14:43:18 +08:00
parent e5c39cb5c8
commit 0707afa35f
15 changed files with 354 additions and 285 deletions

View File

@ -1,6 +1,9 @@
修改历史列表:
-----------------------------------------------------------------------
479) 2017.5.31
479.1) featur: add WebSocketServlet by "fuwangqin" <niukey@qq.com>
478) 2017.5.27
478.1) bugfix: websocket::make_frame_header 中当 payload_len 为 126 时会遇到
内存跨界问题 --- found by "lindawei" <672496008@qq.com>

View File

@ -100,7 +100,7 @@ public:
* @return {bool} false
*
*/
bool doRun();
bool doRun(void);
/**
* HttpServlet HTTP doXXX

View File

@ -1,14 +1,18 @@
#pragma once
#include "stdlib/acl_mymalloc.h"
#include "HttpServlet.hpp"
#include "websocket.hpp"
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/http/HttpServlet.hpp"
namespace acl
{
class websocket;
class session;
class WebSocketServlet: public HttpServlet
{
public:
WebSocketServlet();
WebSocketServlet(void);
/**
*
* @param stream {socket_stream*} acl_master
@ -27,17 +31,18 @@ namespace acl
* 便 acl_master
* @param memcache_addr {const char*}
*/
WebSocketServlet(socket_stream* stream, const char* memcache_addr = "127.0.0.1:11211");
WebSocketServlet(socket_stream* stream,
const char* memcache_addr = "127.0.0.1:11211");
/**
* HttpServlet HTTP doXXX
* start start /
* .
* @return {bool} false
*
* @return {bool} false
*
*/
virtual ~WebSocketServlet();
virtual ~WebSocketServlet(void);
/**
* HttpServlet HTTP doXXX
@ -65,7 +70,8 @@ namespace acl
return HttpServlet::doRun(memcached_addr, stream);
}
virtual bool doRun();
virtual bool doRun(void);
/**
* .
* @param rw_timeout {const char *}
@ -74,7 +80,6 @@ namespace acl
bool send_binary(const char *buf, int len);
/**
* .
* @param rw_timeout {const char *}
@ -83,7 +88,6 @@ namespace acl
bool send_text(const char *text);
/**
* pong .
* @param rw_timeout {const char *}
@ -98,7 +102,7 @@ namespace acl
*/
bool send_ping(const char *buffer = NULL);
unsigned long long get_max_msg_len()
unsigned long long get_max_msg_len(void) const
{
return max_msg_len_;
}
@ -111,13 +115,14 @@ namespace acl
{
max_msg_len_ = len;
}
protected:
protected:
/**
* websocket
* @return {void}
*/
virtual void on_close();
virtual void on_close(void);
/**
* websocket ping .
* @param {const char *} buf
@ -125,6 +130,7 @@ namespace acl
* @return {bool} false
*/
virtual bool on_ping(const char *buf, unsigned long long len) = 0;
/**
* websocket pong .
* @param {const char *} buf
@ -143,7 +149,9 @@ namespace acl
virtual bool on_message(char *data, unsigned long long len, bool text) = 0;
private:
// @override
bool doWebsocket(HttpServletRequest&, HttpServletResponse&);
private:
unsigned long long max_msg_len_;
@ -153,4 +161,5 @@ namespace acl
int write_pos_;
int opcode_;
};
}
} // namespace acl

View File

@ -89,6 +89,7 @@
#include "acl_cpp/http/http_request_pool.hpp"
#include "acl_cpp/http/http_request_manager.hpp"
#include "acl_cpp/http/websocket.hpp"
#include "acl_cpp/http/WebSocketServlet.hpp"
#include "acl_cpp/db/query.hpp"
#include "acl_cpp/db/mysql_conf.hpp"

View File

@ -672,6 +672,9 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<File
RelativePath=".\src\http\websocket.cpp">
</File>
<File
RelativePath=".\src\http\WebSocketServlet.cpp">
</File>
</Filter>
<Filter
Name="hsocket"
@ -1265,6 +1268,9 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<File
RelativePath=".\include\acl_cpp\http\websocket.hpp">
</File>
<File
RelativePath=".\include\acl_cpp\http\WebSocketServlet.hpp">
</File>
</Filter>
<Filter
Name="hsocket"

View File

@ -585,6 +585,10 @@
RelativePath=".\src\http\websocket.cpp"
>
</File>
<File
RelativePath=".\src\http\WebSocketServlet.cpp"
>
</File>
</Filter>
<Filter
Name="ipc"
@ -1431,6 +1435,10 @@
RelativePath=".\include\acl_cpp\http\websocket.hpp"
>
</File>
<File
RelativePath=".\include\acl_cpp\http\WebSocketServlet.hpp"
>
</File>
</Filter>
<Filter
Name="ipc"

View File

@ -255,6 +255,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClCompile Include="src\http\http_service.cpp" />
<ClCompile Include="src\http\http_utils.cpp" />
<ClCompile Include="src\http\websocket.cpp" />
<ClCompile Include="src\http\WebSocketServlet.cpp" />
<ClCompile Include="src\ipc\ipc_client.cpp" />
<ClCompile Include="src\ipc\ipc_server.cpp" />
<ClCompile Include="src\ipc\ipc_service.cpp" />
@ -434,6 +435,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\http\http_type.hpp" />
<ClInclude Include="include\acl_cpp\http\http_utils.hpp" />
<ClInclude Include="include\acl_cpp\http\websocket.hpp" />
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_client.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_server.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_service.hpp" />

View File

@ -286,6 +286,9 @@
<ClCompile Include="src\http\websocket.cpp">
<Filter>src\http</Filter>
</ClCompile>
<ClCompile Include="src\http\WebSocketServlet.cpp">
<Filter>src\http</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\sha1.cpp">
<Filter>src\stdlib</Filter>
</ClCompile>
@ -651,6 +654,9 @@
<ClInclude Include="include\acl_cpp\http\websocket.hpp">
<Filter>include\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp">
<Filter>include\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\HttpServlet.hpp">
<Filter>include\http</Filter>
</ClInclude>

View File

@ -449,6 +449,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\http\http_service.cpp" />
<ClCompile Include="src\http\http_utils.cpp" />
<ClCompile Include="src\http\websocket.cpp" />
<ClCompile Include="src\http\WebSocketServlet.cpp" />
<ClCompile Include="src\ipc\ipc_client.cpp" />
<ClCompile Include="src\ipc\ipc_server.cpp" />
<ClCompile Include="src\ipc\ipc_service.cpp" />
@ -628,6 +629,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\http\http_type.hpp" />
<ClInclude Include="include\acl_cpp\http\http_utils.hpp" />
<ClInclude Include="include\acl_cpp\http\websocket.hpp" />
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_client.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_server.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_service.hpp" />

View File

@ -283,6 +283,9 @@
<ClCompile Include="src\http\websocket.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\http\WebSocketServlet.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\sha1.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
@ -648,6 +651,9 @@
<ClInclude Include="include\acl_cpp\http\websocket.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\HttpServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>

View File

@ -449,6 +449,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\http\http_service.cpp" />
<ClCompile Include="src\http\http_utils.cpp" />
<ClCompile Include="src\http\websocket.cpp" />
<ClCompile Include="src\http\WebSocketServlet.cpp" />
<ClCompile Include="src\ipc\ipc_client.cpp" />
<ClCompile Include="src\ipc\ipc_server.cpp" />
<ClCompile Include="src\ipc\ipc_service.cpp" />
@ -628,6 +629,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\http\http_type.hpp" />
<ClInclude Include="include\acl_cpp\http\http_utils.hpp" />
<ClInclude Include="include\acl_cpp\http\websocket.hpp" />
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_client.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_server.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_service.hpp" />

View File

@ -283,6 +283,9 @@
<ClCompile Include="src\http\websocket.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\http\WebSocketServlet.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\sha1.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
@ -648,6 +651,9 @@
<ClInclude Include="include\acl_cpp\http\websocket.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\HttpServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>

View File

@ -449,6 +449,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\http\http_service.cpp" />
<ClCompile Include="src\http\http_utils.cpp" />
<ClCompile Include="src\http\websocket.cpp" />
<ClCompile Include="src\http\WebSocketServlet.cpp" />
<ClCompile Include="src\ipc\ipc_client.cpp" />
<ClCompile Include="src\ipc\ipc_server.cpp" />
<ClCompile Include="src\ipc\ipc_service.cpp" />
@ -628,6 +629,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\http\http_type.hpp" />
<ClInclude Include="include\acl_cpp\http\http_utils.hpp" />
<ClInclude Include="include\acl_cpp\http\websocket.hpp" />
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_client.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_server.hpp" />
<ClInclude Include="include\acl_cpp\ipc\ipc_service.hpp" />

View File

@ -283,6 +283,9 @@
<ClCompile Include="src\http\websocket.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\http\WebSocketServlet.cpp">
<Filter>Source Files\http</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\sha1.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
@ -648,6 +651,9 @@
<ClInclude Include="include\acl_cpp\http\websocket.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\WebSocketServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\http\HttpServlet.hpp">
<Filter>Header Files\http</Filter>
</ClInclude>

View File

@ -1,45 +1,59 @@
/**
* Copyright (C) 2015-2018
* All rights reserved.
*
* AUTHOR(S)
* Fu wangqin
* E-mail: "fuwangqin" <niukey@qq.com>
*
* VERSION
* Wed 31 May 2017 02:37:14 PM CST
*/
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/stream/socket_stream.hpp"
#include "acl_cpp/session/session.hpp"
#include "acl_cpp/http/websocket.hpp"
#include "acl_cpp/http/WebSocketServlet.hpp"
#endif
namespace acl
{
WebSocketServlet::WebSocketServlet(socket_stream* stream, session* session)
:HttpServlet(stream, session),
max_msg_len_(100 * 1024*1024),
ws_(NULL)
: HttpServlet(stream, session)
, max_msg_len_(100 * 1024*1024)
, ws_(NULL)
{
}
WebSocketServlet::WebSocketServlet(
socket_stream* stream,
WebSocketServlet::WebSocketServlet(socket_stream* stream,
const char* memcache_addr)
:HttpServlet(stream, memcache_addr),
max_msg_len_(100 * 1024 * 1024),
ws_(NULL)
:HttpServlet(stream, memcache_addr)
, max_msg_len_(100 * 1024 * 1024)
, ws_(NULL)
{
}
WebSocketServlet::WebSocketServlet()
WebSocketServlet::WebSocketServlet(void)
:max_msg_len_(100 * 1024 * 1024),
ws_(NULL)
{
}
WebSocketServlet::~WebSocketServlet()
WebSocketServlet::~WebSocketServlet(void)
{
if (ws_)
delete ws_;
ws_ = NULL;
}
bool WebSocketServlet::doRun()
bool WebSocketServlet::doRun(void)
{
if (!ws_)
{
@ -65,14 +79,11 @@ namespace acl
if (len > 0)
{
if (!recv_buffer_)
{
recv_buffer_ = (char*) acl_mymalloc((size_t)len + 1);
}
else
{
//frame not finish.
recv_buffer_ = (char*)acl_myrealloc(recv_buffer_, write_pos_ + (size_t)len + 1);
}
recv_buffer_ = (char*) acl_myrealloc(recv_buffer_,
write_pos_ + (size_t)len + 1);
if (ws_->read_frame_data(recv_buffer_+ write_pos_, len) < 0)
{
@ -84,6 +95,7 @@ namespace acl
write_pos_ += len;
recv_buffer_[write_pos_] = '\0';
}
int opcode = ws_->get_frame_opcode();
bool ret = false;
@ -97,9 +109,7 @@ namespace acl
//frame is finish callback frame.
if (opcode == FRAME_CONTINUATION)
{
opcode = opcode_;
}
switch (opcode)
{
@ -114,15 +124,15 @@ namespace acl
break;
case FRAME_TEXT:
case FRAME_BINARY:
{
ret = on_message(recv_buffer_, write_pos_,
ws_->get_frame_opcode() == FRAME_TEXT);
break;
}
default:
logger_error("unknown websocket Frame opcode error: %d",
ws_->get_frame_opcode());
break;
}
if (recv_buffer_)
{
acl_myfree(recv_buffer_);
@ -133,21 +143,20 @@ namespace acl
return ret;
}
bool WebSocketServlet::send_binary(const char *recv_buffer_, int len)
bool WebSocketServlet::send_binary(const char *buffer, int len)
{
ws_->set_frame_opcode(FRAME_BINARY);
ws_->set_frame_fin(true);
ws_->set_frame_payload_len(len);
return ws_->send_frame_data(recv_buffer_, len);
return ws_->send_frame_data(buffer, len);
}
bool WebSocketServlet::send_text(const char *recv_buffer_fer)
bool WebSocketServlet::send_text(const char *buffer)
{
ws_->set_frame_opcode(FRAME_TEXT);
ws_->set_frame_fin(true);
ws_->set_frame_payload_len(strlen(recv_buffer_fer));
return ws_->send_frame_data(recv_buffer_fer, strlen(recv_buffer_fer));
ws_->set_frame_payload_len(strlen(buffer));
return ws_->send_frame_data(buffer, strlen(buffer));
}
bool WebSocketServlet::send_pong(const char *text)
@ -175,7 +184,8 @@ namespace acl
return true;
}
void WebSocketServlet::on_close()
void WebSocketServlet::on_close(void)
{
}
}
} // namespace acl