You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
7.8 KiB
192 lines
7.8 KiB
#include "servletClientHandlerWebRTC.h"
|
|
#include "servletWebRTC.h"
|
|
#include "../../components/base/basicFunctionality.h"
|
|
#include "../../alg/generic/base64.h"
|
|
#include <openssl/sha.h>
|
|
#include <websocketpp/http/request.hpp>
|
|
#include <websocketpp/http/response.hpp>
|
|
#include <websocketpp/config/core.hpp>
|
|
#include <websocketpp/processors/hybi13.hpp>
|
|
|
|
#define LOG( X ) m_pLog->Log( "WRTC", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "WRTC", this, _LOG_ERROR, X )
|
|
|
|
typedef websocketpp::config::core ws_config;
|
|
typedef websocketpp::processor::hybi13<ws_config> ws_parser;
|
|
typedef ws_config::request_type ws_request;
|
|
typedef ws_config::response_type ws_response;
|
|
typedef ws_config::message_type ws_message;
|
|
typedef ws_config::con_msg_manager_type ws_msg_manager;
|
|
typedef ws_config::rng_type ws_rng;
|
|
|
|
|
|
servletClientHandlerWebRTC::servletClientHandlerWebRTC( servletWebRTC *pPtr, int Socket ):basicThread(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pParent = NULL;
|
|
if( pPtr == NULL ) return;
|
|
m_iSocketFID = accept( Socket, NULL, NULL);
|
|
if ( m_iSocketFID < 0) {
|
|
char tBuf[CTEXT];
|
|
sprintf(tBuf, "accept() error: %s", strerror(errno));
|
|
LOG_ERROR(tBuf);
|
|
return;
|
|
};
|
|
m_pParent = pPtr;
|
|
basicThread::init();
|
|
};
|
|
|
|
servletClientHandlerWebRTC::~servletClientHandlerWebRTC(){
|
|
basicThread::waitThreadEnd( true );
|
|
LOG("Client Handler Deleted!");
|
|
};
|
|
|
|
void servletClientHandlerWebRTC::m_fThreadProc( void ){
|
|
char *ws_head = "HTTP/1.1 101 Switching Protocols\r\n";
|
|
char mesg[1024];
|
|
uint8_t buffer[1024];
|
|
bool bSwitched = false;
|
|
size_t mRecvSize = 0;
|
|
size_t nPackShift = 0;
|
|
size_t mParserConsumed = 0;
|
|
struct pollfd pfd[1];
|
|
|
|
#ifdef DEBUG
|
|
pid_t pid = gettid();
|
|
char pidBuf[16];
|
|
sprintf(pidBuf,"pid:%d", pid);
|
|
LOG(pidBuf);
|
|
#endif
|
|
|
|
pfd->fd = m_iSocketFID;
|
|
pfd->events = POLLIN;
|
|
|
|
ws_parser * pParser = NULL;
|
|
ws_request * pRequest = NULL;
|
|
ws_response * pResponse = NULL;
|
|
websocketpp::lib::error_code mErrorCode;
|
|
std::vector< std::string > vSubprotocolList;
|
|
ws_msg_manager M;
|
|
std::shared_ptr<ws_msg_manager> pManagerPtr( &M );
|
|
ws_rng A;
|
|
|
|
pParser = new ws_parser( false, true, pManagerPtr, A ); //No Secure, Server
|
|
if( pParser == NULL ) {
|
|
m_bThreadStatus = TH_ENDED;
|
|
return;
|
|
};
|
|
m_bThreadStatus = TH_WORKING;
|
|
while( m_bThreadStatus < TH_ENDED ) {
|
|
int RetVal = poll( pfd, 1, 1000 );
|
|
if( RetVal <= 0 ){
|
|
if (EINTR == errno) continue;
|
|
LOG_ERROR("Unrecoverable pool error");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
if(!bSwitched) {
|
|
if( nPackShift >= sizeof(mesg) ) {
|
|
LOG_ERROR("Package is too lond - drop connection!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
mRecvSize = recv( m_iSocketFID, mesg + nPackShift, sizeof(mesg) - nPackShift, 0 );
|
|
if ( mRecvSize <= 0 ) {
|
|
sprintf( mesg, "recv() error: %s[%d]", strerror(errno), errno );
|
|
LOG_ERROR(mesg);
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
if( pRequest == NULL ) {
|
|
pRequest = new ws_request();
|
|
};
|
|
if( pRequest == NULL ) {
|
|
LOG_ERROR("Request Constructor error - drop connection!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
mParserConsumed = pRequest->consume( mesg, mRecvSize );
|
|
if( pRequest->ready() ) {
|
|
nPackShift = 0;
|
|
LOG( "MSG parsed!" );
|
|
if( websocketpp::processor::is_websocket_handshake( *pRequest ) ){
|
|
int nVersion = 0;
|
|
LOG( "MSG is handshake" );
|
|
nVersion = websocketpp::processor::get_websocket_version( *pRequest );
|
|
mErrorCode = pParser->extract_subprotocols( *pRequest, vSubprotocolList );
|
|
if( vSubprotocolList.size() ) {
|
|
LOG( "Subprotocols:" );
|
|
for(int index = 0; index < vSubprotocolList.size(); index++ ) {
|
|
LOG( vSubprotocolList.at(index).c_str() );
|
|
};
|
|
} else {
|
|
LOG("No Subprotocols");
|
|
};
|
|
if( pResponse == NULL ) pResponse = new ws_response();
|
|
if( pResponse == NULL ) {
|
|
delete( pRequest );
|
|
LOG_ERROR("Response Constructor error - drop connection!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
pResponse->consume(ws_head, strlen(ws_head));
|
|
mErrorCode = pParser->process_handshake( *pRequest, "", *pResponse );
|
|
send( m_iSocketFID, pResponse->raw().c_str(), pResponse->raw().size(), 0);
|
|
delete( pResponse );
|
|
pResponse = NULL;
|
|
bSwitched = true;
|
|
};
|
|
delete( pRequest );
|
|
pRequest = NULL;
|
|
} else {
|
|
nPackShift += mRecvSize;
|
|
};
|
|
} else {
|
|
if( nPackShift >= sizeof(mesg) ) {
|
|
LOG_ERROR("Package is too lond - drop connection!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
mRecvSize = recv( m_iSocketFID, buffer + nPackShift, sizeof(buffer) - nPackShift, 0 );
|
|
if ( mRecvSize <= 0 ) {
|
|
sprintf( mesg, "recv() error: %s[%d]", strerror(errno), errno );
|
|
LOG_ERROR(mesg);
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
mParserConsumed = pParser->consume( buffer, mRecvSize, mErrorCode );
|
|
if( pParser->ready() ) {
|
|
std::shared_ptr<ws_message> msg;
|
|
//ws_message * body_out_msg;
|
|
LOG("In MESSAGE!");
|
|
mErrorCode = pParser->finalize_message();
|
|
in_msg = pParser->get_message();
|
|
LOG(in_msg->get_payload().c_str());
|
|
body_out_msg = new ws_message( pManagerPtr );
|
|
if( body_out_msg != NULL ){
|
|
LOG("OUT allocated");
|
|
{
|
|
std::shared_ptr<ws_message> out_msg(body_out_msg);
|
|
out_msg->set_payload( "KOKOBABA" );
|
|
LOG("SET PAYLOAD");
|
|
mErrorCode = pParser->prepare_data_frame( out_msg, out_msg );
|
|
send( m_iSocketFID, out_msg->get_header().c_str(), out_msg->get_header().size(), 0);
|
|
if(out_msg->get_extension_data().size()) {
|
|
send( m_iSocketFID, out_msg->get_extension_data().c_str(), out_msg->get_extension_data().size(), 0);
|
|
};
|
|
send( m_iSocketFID, out_msg->get_payload().c_str(), out_msg->get_payload().size(), 0);
|
|
out_msg->recycle();
|
|
}
|
|
//delete( body_out_msg );
|
|
LOG("OUT deleted!");
|
|
};
|
|
};
|
|
};
|
|
};
|
|
LOG("Client Handler Ended!");
|
|
shutdown(m_iSocketFID, SHUT_RDWR);
|
|
close(m_iSocketFID);
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|