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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servletClientHandlerWebRTC.h"
  2. #include "servletWebRTC.h"
  3. #include "../../components/base/basicFunctionality.h"
  4. #include "../../alg/generic/base64.h"
  5. #include <openssl/sha.h>
  6. #include <websocketpp/http/request.hpp>
  7. #include <websocketpp/http/response.hpp>
  8. #include <websocketpp/config/core.hpp>
  9. #include <websocketpp/processors/hybi13.hpp>
  10. #define LOG( X ) m_pLog->Log( "WRTC", this, X )
  11. #define LOG_ERROR( X ) m_pLog->Log( "WRTC", this, _LOG_ERROR, X )
  12. typedef websocketpp::config::core ws_config;
  13. typedef websocketpp::processor::hybi13<ws_config> ws_parser;
  14. typedef ws_config::request_type ws_request;
  15. typedef ws_config::response_type ws_response;
  16. typedef ws_config::message_type ws_message;
  17. typedef ws_config::con_msg_manager_type ws_msg_manager;
  18. typedef ws_config::rng_type ws_rng;
  19. servletClientHandlerWebRTC::servletClientHandlerWebRTC( servletWebRTC *pPtr, int Socket ):basicThread(){
  20. m_pLog = basicLogCollection::getInstance();
  21. m_pParent = NULL;
  22. if( pPtr == NULL ) return;
  23. m_iSocketFID = accept( Socket, NULL, NULL);
  24. if ( m_iSocketFID < 0) {
  25. char tBuf[CTEXT];
  26. sprintf(tBuf, "accept() error: %s", strerror(errno));
  27. LOG_ERROR(tBuf);
  28. return;
  29. };
  30. m_pParent = pPtr;
  31. basicThread::init();
  32. };
  33. servletClientHandlerWebRTC::~servletClientHandlerWebRTC(){
  34. basicThread::waitThreadEnd( true );
  35. LOG("Client Handler Deleted!");
  36. };
  37. void servletClientHandlerWebRTC::m_fThreadProc( void ){
  38. char *ws_head = "HTTP/1.1 101 Switching Protocols\r\n";
  39. char mesg[1024];
  40. uint8_t buffer[1024];
  41. bool bSwitched = false;
  42. size_t mRecvSize = 0;
  43. size_t nPackShift = 0;
  44. size_t mParserConsumed = 0;
  45. struct pollfd pfd[1];
  46. #ifdef DEBUG
  47. pid_t pid = gettid();
  48. char pidBuf[16];
  49. sprintf(pidBuf,"pid:%d", pid);
  50. LOG(pidBuf);
  51. #endif
  52. pfd->fd = m_iSocketFID;
  53. pfd->events = POLLIN;
  54. ws_parser * pParser = NULL;
  55. ws_request * pRequest = NULL;
  56. ws_response * pResponse = NULL;
  57. websocketpp::lib::error_code mErrorCode;
  58. std::vector< std::string > vSubprotocolList;
  59. ws_msg_manager M;
  60. std::shared_ptr<ws_msg_manager> pManagerPtr( &M );
  61. ws_rng A;
  62. pParser = new ws_parser( false, true, pManagerPtr, A ); //No Secure, Server
  63. if( pParser == NULL ) {
  64. m_bThreadStatus = TH_ENDED;
  65. return;
  66. };
  67. m_bThreadStatus = TH_WORKING;
  68. while( m_bThreadStatus < TH_ENDED ) {
  69. int RetVal = poll( pfd, 1, 1000 );
  70. if( RetVal <= 0 ){
  71. if (EINTR == errno) continue;
  72. LOG_ERROR("Unrecoverable pool error");
  73. m_bThreadStatus = TH_ENDED;
  74. continue;
  75. };
  76. if(!bSwitched) {
  77. if( nPackShift >= sizeof(mesg) ) {
  78. LOG_ERROR("Package is too lond - drop connection!");
  79. m_bThreadStatus = TH_ENDED;
  80. continue;
  81. };
  82. mRecvSize = recv( m_iSocketFID, mesg + nPackShift, sizeof(mesg) - nPackShift, 0 );
  83. if ( mRecvSize <= 0 ) {
  84. sprintf( mesg, "recv() error: %s[%d]", strerror(errno), errno );
  85. LOG_ERROR(mesg);
  86. m_bThreadStatus = TH_ENDED;
  87. continue;
  88. };
  89. if( pRequest == NULL ) {
  90. pRequest = new ws_request();
  91. };
  92. if( pRequest == NULL ) {
  93. LOG_ERROR("Request Constructor error - drop connection!");
  94. m_bThreadStatus = TH_ENDED;
  95. continue;
  96. };
  97. mParserConsumed = pRequest->consume( mesg, mRecvSize );
  98. if( pRequest->ready() ) {
  99. nPackShift = 0;
  100. LOG( "MSG parsed!" );
  101. if( websocketpp::processor::is_websocket_handshake( *pRequest ) ){
  102. int nVersion = 0;
  103. LOG( "MSG is handshake" );
  104. nVersion = websocketpp::processor::get_websocket_version( *pRequest );
  105. mErrorCode = pParser->extract_subprotocols( *pRequest, vSubprotocolList );
  106. if( vSubprotocolList.size() ) {
  107. LOG( "Subprotocols:" );
  108. for(int index = 0; index < vSubprotocolList.size(); index++ ) {
  109. LOG( vSubprotocolList.at(index).c_str() );
  110. };
  111. } else {
  112. LOG("No Subprotocols");
  113. };
  114. if( pResponse == NULL ) pResponse = new ws_response();
  115. if( pResponse == NULL ) {
  116. delete( pRequest );
  117. LOG_ERROR("Response Constructor error - drop connection!");
  118. m_bThreadStatus = TH_ENDED;
  119. continue;
  120. };
  121. pResponse->consume(ws_head, strlen(ws_head));
  122. mErrorCode = pParser->process_handshake( *pRequest, "", *pResponse );
  123. send( m_iSocketFID, pResponse->raw().c_str(), pResponse->raw().size(), 0);
  124. delete( pResponse );
  125. pResponse = NULL;
  126. bSwitched = true;
  127. };
  128. delete( pRequest );
  129. pRequest = NULL;
  130. } else {
  131. nPackShift += mRecvSize;
  132. };
  133. } else {
  134. if( nPackShift >= sizeof(mesg) ) {
  135. LOG_ERROR("Package is too lond - drop connection!");
  136. m_bThreadStatus = TH_ENDED;
  137. continue;
  138. };
  139. mRecvSize = recv( m_iSocketFID, buffer + nPackShift, sizeof(buffer) - nPackShift, 0 );
  140. if ( mRecvSize <= 0 ) {
  141. sprintf( mesg, "recv() error: %s[%d]", strerror(errno), errno );
  142. LOG_ERROR(mesg);
  143. m_bThreadStatus = TH_ENDED;
  144. continue;
  145. };
  146. mParserConsumed = pParser->consume( buffer, mRecvSize, mErrorCode );
  147. if( pParser->ready() ) {
  148. std::shared_ptr<ws_message> msg;
  149. //ws_message * body_out_msg;
  150. LOG("In MESSAGE!");
  151. mErrorCode = pParser->finalize_message();
  152. in_msg = pParser->get_message();
  153. LOG(in_msg->get_payload().c_str());
  154. body_out_msg = new ws_message( pManagerPtr );
  155. if( body_out_msg != NULL ){
  156. LOG("OUT allocated");
  157. {
  158. std::shared_ptr<ws_message> out_msg(body_out_msg);
  159. out_msg->set_payload( "KOKOBABA" );
  160. LOG("SET PAYLOAD");
  161. mErrorCode = pParser->prepare_data_frame( out_msg, out_msg );
  162. send( m_iSocketFID, out_msg->get_header().c_str(), out_msg->get_header().size(), 0);
  163. if(out_msg->get_extension_data().size()) {
  164. send( m_iSocketFID, out_msg->get_extension_data().c_str(), out_msg->get_extension_data().size(), 0);
  165. };
  166. send( m_iSocketFID, out_msg->get_payload().c_str(), out_msg->get_payload().size(), 0);
  167. out_msg->recycle();
  168. }
  169. //delete( body_out_msg );
  170. LOG("OUT deleted!");
  171. };
  172. };
  173. };
  174. };
  175. LOG("Client Handler Ended!");
  176. shutdown(m_iSocketFID, SHUT_RDWR);
  177. close(m_iSocketFID);
  178. };
  179. #undef LOG
  180. #undef LOG_ERROR