acl/lib_acl_cpp/samples/http/webSocketServlet/WebsocketServlet_impl.cpp

134 lines
2.8 KiB
C++
Raw Normal View History

2017-05-20 11:37:15 +08:00
#include "stdafx.h"
#include "WebsocketServlet_impl.h"
WebsocketServlet_impl::WebsocketServlet_impl(acl::redis_client_cluster& cluster, size_t max_conns)
{
// <20><><EFBFBD><EFBFBD> session <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>
session_ = new acl::redis_session(cluster, max_conns);
step_ = 0;
}
WebsocketServlet_impl::~WebsocketServlet_impl(void)
{
delete session_;
}
bool WebsocketServlet_impl::doUnknown(acl::HttpServletRequest&,
acl::HttpServletResponse& res)
{
res.setStatus(400);
res.setContentType("text/html; charset=");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
if (res.sendHeader() == false)
return false;
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
acl::string buf("<root error='unkown request method' />\r\n");
(void) res.getOutputStream().write(buf);
return false;
}
bool WebsocketServlet_impl::doGet(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
return doPost(req, res);
}
bool WebsocketServlet_impl::doPost(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
res.setContentType("text/html; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setContentEncoding(false) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>ʽ
const char* ip = req.getLocalAddr();
if (ip == NULL || *ip == 0)
{
logger_error("getLocalAddr error");
return false;
}
unsigned short port = req.getLocalPort();
if (port == 0)
{
logger_error("getLocalPort error");
return false;
}
acl::string local_addr;
local_addr << ip << ":" << port;
printf("getLocalAddr: %s\r\n", local_addr.c_str());
acl::string html_file;
html_file << "www/upload.html";
acl::string buf;
if (acl::ifstream::load(html_file, &buf) == false)
{
logger_error("load %s error %s",
html_file.c_str(), acl::last_serror());
return doError(req, res);
}
buf << "<script>g_url='ws://" << local_addr << "/'</script>";
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><EFBFBD><E5A3AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> chunk <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
// res.write <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 <20>Ա<EFBFBD>ʾ chunk <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
return res.write(buf) && res.write(NULL, 0);
}
2017-05-31 14:57:05 +08:00
bool WebsocketServlet_impl::on_ping(const char *, unsigned long long)
2017-05-20 11:37:15 +08:00
{
return send_pong();
}
2017-05-31 14:57:05 +08:00
bool WebsocketServlet_impl::on_pong(const char *, unsigned long long)
2017-05-20 11:37:15 +08:00
{
return send_ping();
}
2017-05-31 14:57:05 +08:00
bool WebsocketServlet_impl::on_message(char *data, unsigned long long len, bool)
2017-05-20 11:37:15 +08:00
{
switch (step_)
{
case 0:
{
printf("FileName:%s\n",data);
filename_.append(data, len);
step_++;
}
break;
case 1:
{
printf("FileSize:%s\n", data);
2017-05-31 14:57:05 +08:00
filesize_ = strtol(data, 0, 10);
2017-05-20 11:37:15 +08:00
step_++;
}
break;
case 2:
{
if (!file_)
{
file_ = new acl::ofstream();
file_->open_trunc(filename_);
}
file_->write(data, len);
current_filesize_ += len;
if (current_filesize_ == filesize_)
{
printf("upload done\n");
file_->close();
delete file_;
file_ = NULL;
filename_.clear();
filesize_ = 0;
current_filesize_ = 0;
step_ = 0;
}
}
break;
default:
break;
}
return true;
}