void PrintDebugError(const std::string sMessage) { std::wstring wsMessage(sMessage.begin(), sMessage.end()); OutputDebugString(L"Error!"); OutputDebugString(wsMessage.c_str()); }
std::string Utils::GetLastError() { LPVOID lpErrMsgBuf; LPVOID lpMessageBuf; DWORD dwErrorCode = ::GetLastError(); // Get the string corresponding to the error code FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpErrMsgBuf, 0, NULL); // Allocate a bigger buffer lpMessageBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpErrMsgBuf)+40)*sizeof(TCHAR)); // Format the message like so: "error 'code': 'message'" StringCchPrintf((LPTSTR)lpMessageBuf, LocalSize(lpMessageBuf) / sizeof(TCHAR), TEXT("error %d: %s"), dwErrorCode, lpErrMsgBuf); std::wstring wsMessage((LPCTSTR)lpMessageBuf); LocalFree(lpErrMsgBuf); LocalFree(lpMessageBuf); return narrowString(wsMessage); }
void HttpServer::processWebSocketFrame(pbuf *buf, HttpServerConnection& connection) { //TODO: process splitted payload uint8_t* data; size_t size; wsFrameType frameType = wsParseInputFrame((uint8_t*)buf->payload, buf->len, &data, &size); WebSocket* sock = getWebSocket(connection); if (frameType == WS_TEXT_FRAME) { String msg; msg.setString((char*)data, size); debugf("WS: %s", msg.c_str()); if (sock && wsMessage) wsMessage(*sock, msg); } if (frameType == WS_BINARY_FRAME) { if (sock && wsMessage) wsBinary(*sock, data, size); } else if (frameType == WS_CLOSING_FRAME) { connection.close(); // it will be processed automatically in onCloseWebSocket callback } else if (frameType == WS_INCOMPLETE_FRAME || frameType == WS_ERROR_FRAME) debugf("WS error reading frame: %X", frameType); else debugf("WS frame type: %X", frameType); }
ret_ CXMLLoaderActions::LoadAlert(CProgram &Program, const DOMElement *pElement) { #ifdef _DEBUG_ if (!pElement) return (PARAMETER_NULL | PARAMETER_2); #endif auto_xerces_str wsMessage("message"); auto_xerces_str sMessage(pElement->getAttribute(wsMessage)); COptAlert *pOpt = new COptAlert(sMessage); if (false_v == Program.AddOperator(pOpt)) return XML_LOADER_ERROR; return SUCCESS; }
void HttpServer::processWebSocketFrame(pbuf *buf, HttpServerConnection& connection) { //TODO: process splitted payload uint8_t* data; size_t size; wsFrameType frameType = (wsFrameType) 0x01; uint8_t payloadFieldExtraBytes = 0; size_t payloadLength = 0; size_t payloadShift = 0; do { payloadLength = getPayloadLength((uint8_t*)buf->payload + payloadShift, buf->len, &payloadFieldExtraBytes, &frameType); // debugf("payloadLength: %u, payLoadShift: %u, payloadFieldExtraBytes: %u\n", payloadLength, payloadShift, payloadFieldExtraBytes); wsFrameType frameType = wsParseInputFrame((uint8_t*)buf->payload + payloadShift, (payloadLength + 6 + payloadFieldExtraBytes), &data, &size); WebSocket* sock = getWebSocket(connection); if (frameType == WS_TEXT_FRAME) { String msg; msg.setString((char*)data, size); debugf("WS: %s", msg.c_str()); if (sock && wsMessage) wsMessage(*sock, msg); #if ENABLE_CMD_EXECUTOR if (sock && sock->commandExecutor) sock->commandExecutor->executorReceive(msg+"\r"); #endif } else if (frameType == WS_BINARY_FRAME) { if (sock && wsMessage) wsBinary(*sock, data, size); } else if (frameType == WS_CLOSING_FRAME) { connection.close(); // it will be processed automatically in onCloseWebSocket callback } else if (frameType == WS_INCOMPLETE_FRAME || frameType == WS_ERROR_FRAME) debugf("WS error reading frame: %X", frameType); else debugf("WS frame type: %X", frameType); payloadShift += payloadLength + 6 + payloadFieldExtraBytes; } while (buf->len > payloadShift); }
err_t WebsocketClient::onReceive(pbuf* buf) { if (buf == NULL) { // Disconnected, close it TcpClient::onReceive(buf); } else { uint16_t size = buf->tot_len; uint8_t* data = new uint8_t[size]; pbuf_copy_partial(buf, data, size, 0); switch (_mode) { case wsMode::Connecting: if (_verifyKey((char*)data, size) == true) { _mode = wsMode::Connected; // debugf("Key Verified. Websocket Handshake completed"); sendPing(); } else { _mode = wsMode::Disconnected; // Handshake was not proper. } if (wsConnect) { wsConnect(*this, _mode); } break; case wsMode::Connected: WebsocketFrameClass wsFrame; do { if (wsFrame.decodeFrame(data, size)) { switch (wsFrame._frameType) { case WSFrameType::text: { // debugf("Got text frame"); String msg; msg.setString((char*)wsFrame._payload, wsFrame._payloadLength); if (wsMessage) { wsMessage(*this, msg.c_str()); } break; } case WSFrameType::binary: { // debugf("Got binary frame"); if (wsBinary) { wsBinary(*this, wsFrame._payload, wsFrame._payloadLength); } break; } case WSFrameType::close: { debugf("Got Disconnect request from server.\n"); //RFC requires we return a close op code before closing the connection disconnect(); break; } case WSFrameType::ping: { debugf("Got ping ..."); sendPong(); //Need to send Pong in response to Ping break; } case WSFrameType::pong: { debugf("Got pong ..."); //A pong can contain app data, but shouldnt if we didnt send any... break; } case WSFrameType::error: { debugf("ERROR parsing frame!"); break; } case WSFrameType::incomplete: { debugf("INCOMPLETE websocket frame!"); break; } default: { debugf("Unknown frameType: %d", wsFrame._frameType); break; } } } } while (wsFrame._nextReadOffset > 0); break; } delete[] data; TcpClient::onReceive(buf); } }