void CImConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE) m_in_buf.Extend(READ_BUF_SIZE); int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret == 0) { log("close on netlib_recv=0"); OnClose(); return; } else if (ret < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { log("close on error=%d", errno); OnClose(); return; } } m_recv_bytes += ret; m_in_buf.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } CImPdu* pPdu = NULL; try { while ( ( pPdu = CImPdu::ReadPdu(m_in_buf.GetBuffer(), m_in_buf.GetWriteOffset()) ) ) { uint32_t pdu_len = pPdu->GetLength(); HandlePdu(pPdu); m_in_buf.Read(NULL, pdu_len); delete pPdu; pPdu = NULL; // ++g_recv_pkt_cnt; } } catch (CPduException& ex) { log("!!!catch exception, sid=%u, cid=%u, err_code=%u, err_msg=%s, close the connection ", ex.GetServiceId(), ex.GetCommandId(), ex.GetErrorCode(), ex.GetErrorMsg()); if (pPdu) { delete pPdu; pPdu = NULL; } OnClose(); } }
void CImConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE) m_in_buf.Extend(READ_BUF_SIZE); int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret <= 0) break; m_recv_bytes += ret; m_in_buf.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } if (m_policy_conn) { return; } // no received data is read by ReadPdu(), check if this is a flash security policy request if (m_recv_bytes == m_in_buf.GetWriteOffset()) { if ( (m_in_buf.GetBuffer()[0] == '<') && (g_policy_content != NULL) ) { log("policy request, handle=%d\n", m_handle); m_policy_conn = true; Send(g_policy_content, g_policy_len); return; } } try { CImPdu* pPdu = NULL; while ( ( pPdu = CImPdu::ReadPdu(m_in_buf.GetBuffer(), m_in_buf.GetWriteOffset()) ) ) { uint32_t pdu_len = pPdu->GetLength(); HandlePdu(pPdu); m_in_buf.Read(NULL, pdu_len); delete pPdu; ++g_recv_pkt_cnt; } } catch (CPduException& ex) { log("!!!catch exception, sid=%u, cid=%u, err_code=%u, err_msg=%s, close the connection\n", ex.GetModuleId(), ex.GetCommandId(), ex.GetErrorCode(), ex.GetErrorMsg()); OnClose(); } }
void CHttpConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE + 1) m_in_buf.Extend(READ_BUF_SIZE + 1); int ret = netlib_recv(m_sock_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret == 0) { log("close on netlib_recv=0"); OnClose(); return; } else if (ret < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { log("close on error=%d", errno); OnClose(); return; } } m_in_buf.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } // 每次请求对应一个HTTP连接,所以读完数据后,不用在同一个连接里面准备读取下个请求 char* in_buf = (char*)m_in_buf.GetBuffer(); uint32_t buf_len = m_in_buf.GetWriteOffset(); in_buf[buf_len] = '\0'; //log("OnRead, buf_len=%u, conn_handle=%u\n", buf_len, m_conn_handle); // for debug m_HttpParser.ParseHttpContent(in_buf, buf_len); if (m_HttpParser.IsReadAll()) { string url = m_HttpParser.GetUrl(); if (strncmp(url.c_str(), "/query/", 7) == 0) { string content = m_HttpParser.GetBodyContent(); CHttpQuery* pQueryInstance = CHttpQuery::GetInstance(); pQueryInstance->DispatchQuery(url, content, this); } else { log("url unknown, url=%s ", url.c_str()); Close(); } } }
void CImConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE) m_in_buf.Extend(READ_BUF_SIZE); int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret <= 0) break; m_in_buf.IncWriteOffset(ret); while (m_in_buf.GetWriteOffset() >= imcore::HEADER_LENGTH) { uint32_t len = m_in_buf.GetWriteOffset(); uint32_t length = CByteStream::ReadUint32(m_in_buf.GetBuffer()); if (length > len) break; try { imcore::TTPBHeader pbHeader; pbHeader.unSerialize((byte*)m_in_buf.GetBuffer(), imcore::HEADER_LENGTH); LOG__(NET, _T("OnRead moduleId:0x%x,commandId:0x%x"), pbHeader.getModuleId(), pbHeader.getCommandId()); if (m_pTcpSocketCB) m_pTcpSocketCB->onReceiveData((const char*)m_in_buf.GetBuffer(), length); LOGBIN_F__(SOCK, "OnRead", m_in_buf.GetBuffer(), length); } catch (std::exception& ex) { assert(FALSE); LOGA__(NET, "std::exception,info:%s", ex.what()); if (m_pTcpSocketCB) m_pTcpSocketCB->onReceiveError(); } catch (...) { assert(FALSE); LOG__(NET, _T("unknown exception")); if (m_pTcpSocketCB) m_pTcpSocketCB->onReceiveError(); } m_in_buf.Read(NULL, length); } } }
void CHttpConn::OnRead() { for (;;){ uint32_t free_buf_len = m_inBuf.GetAllocSize() - m_inBuf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE + 1){ m_inBuf.Extend(READ_BUF_SIZE + 1); } int ret = netlib_recv(m_sockHandle, m_inBuf.GetBuffer() + m_inBuf.GetWriteOffset(), READ_BUF_SIZE); if (ret <= 0) break; m_inBuf.IncWriteOffset(ret); m_lastRecvTick = get_tick_count(); } // 每次请求对应一个HTTP连接,所以读完数据后,不用在同一个连接里面准备读取下个请求 char* in_buf = (char*)m_inBuf.GetBuffer(); uint32_t buf_len = m_inBuf.GetWriteOffset(); in_buf[buf_len] = '\0'; Logger.Log(INFO, "OnRead, buf_len=%u, uuid=%u content:%s\n", buf_len, m_uuid,in_buf); m_HttpParser->ParseHttpContent(in_buf, buf_len); if (m_HttpParser->IsReadAll()) { string url = m_HttpParser->GetUrl(); //参数安全性校验 // if(!ParamVerify(url)){ // Logger.Log(ERROR, "url param is not safe! url=%s", url.c_str()); // } ActionHandlerT actHandler = g_routeConfig->GetAction(url); if (actHandler) { CTask* pTask = new CHttpTask(m_uuid, actHandler, m_HttpParser); g_threadPool.AddTask(pTask); } else { Logger.Log(ERROR, "no handler for request: %s", url.c_str()); string strResponse = "服务端未找到对应的控制器"; Send((void*)strResponse.c_str(), strResponse.length()); } } }
void CImConn::OnRead() { while (true) { uint32_t free_buf_len = m_in_buff.GetAllocSize() - m_in_buff.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE) { m_in_buff.Extend(READ_BUF_SIZE); } int ret = netlib_recv(m_handle, m_in_buff.GetBuffer() + m_in_buff.GetWriteOffset(), READ_BUF_SIZE); if (ret <= 0) { break; } m_recv_bytes += ret; m_in_buff.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } if (m_policy_conn) { return; } if (m_recv_bytes == m_in_buff.GetWriteOffset()) { if ((m_in_buff.GetBuffer()[0] == '<') && (g_policy_content != NULL)) { m_policy_conn = true; Send(g_policy_content, g_policy_len); return; } } CImPdu *pPdu = NULL; while ((pPdu = CImPdu::ReadPdu(m_in_buff.GetBuffer(), m_in_buff.GetWriteOffset()))) { uint32_t pdu_len = pPdu->GetLength(); HandlePdu(pPdu); m_in_buff.Read(NULL, pdu_len); delete pPdu; ++g_recv_pkt_cnt; } }
void CHttpConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE + 1) m_in_buf.Extend(READ_BUF_SIZE + 1); int ret = netlib_recv(m_sock_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret <= 0) break; m_in_buf.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } // 每次请求对应一个HTTP连接,所以读完数据后,不用在同一个连接里面准备读取下个请求 char* in_buf = (char*)m_in_buf.GetBuffer(); uint32_t buf_len = m_in_buf.GetWriteOffset(); in_buf[buf_len] = '\0'; //log("OnRead, buf_len=%u, conn_handle=%u\n", buf_len, m_conn_handle); // for debug CHttpParserWrapper* pHttpParser = CHttpParserWrapper::GetInstance(); pHttpParser->ParseHttpContent(in_buf, buf_len); if (pHttpParser->IsReadAll()) { string url = pHttpParser->GetUrl(); if (strncmp(url.c_str(), "/query/", 7) == 0) { string content = pHttpParser->GetBodyContent(); CHttpQuery* pQueryInstance = CHttpQuery::GetInstance(); pQueryInstance->DispatchQuery(url, content, this); } else { log("url unknown, url=%s\n", url.c_str()); Close(); } } }
void CHttpConn::OnRead() { for (;;) { uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset(); if (free_buf_len < READ_BUF_SIZE + 1) m_in_buf.Extend(READ_BUF_SIZE + 1); int ret = netlib_recv(m_sock_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE); if (ret == 0) { log("close on netlib_recv=0"); OnClose(); return; } else if (ret < 0) { ret = 0; if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { log("close on error=%d", errno); OnClose(); return; } } m_in_buf.IncWriteOffset(ret); m_last_recv_tick = get_tick_count(); } // 每次请求对应一个HTTP连接,所以读完数据后,不用在同一个连接里面准备读取下个请求 char* in_buf = (char*) m_in_buf.GetBuffer(); uint32_t buf_len = m_in_buf.GetWriteOffset(); in_buf[buf_len] = '\0'; //log("OnRead, buf_len=%u, conn_handle=%u", buf_len, m_conn_handle); // for debug m_HttpParser.ParseHttpContent(in_buf, buf_len); if (m_HttpParser.IsReadAll()) { string strUrl = m_HttpParser.GetUrl(); log("IP:%s access:%s", m_peer_ip.c_str(), strUrl.c_str()); if (strUrl.find("..") != strUrl.npos) { Close(); return; } m_access_host = m_HttpParser.GetHost(); if (m_HttpParser.GetContentLen() > HTTP_UPLOAD_MAX) { // file is too big log("content is too big"); char url[128]; snprintf(url, sizeof(url), "{\"error_code\":1,\"error_msg\": \"上传文件过大\",\"url\":\"\"}"); log("%s",url); uint32_t content_length = strlen(url); char pContent[1024]; snprintf(pContent, sizeof(pContent), HTTP_RESPONSE_HTML, content_length,url); Send(pContent, strlen(pContent)); return; } int nContentLen = m_HttpParser.GetContentLen(); char* pContent = NULL; if(nContentLen != 0) { try { pContent =new char[nContentLen]; memcpy(pContent, m_HttpParser.GetBodyContent(), nContentLen); } catch(...) { log("not enough memory"); char szResponse[HTTP_RESPONSE_500_LEN + 1]; snprintf(szResponse, HTTP_RESPONSE_500_LEN, "%s", HTTP_RESPONSE_500); Send(szResponse, HTTP_RESPONSE_500_LEN); return; } } Request_t request; request.conn_handle = m_conn_handle; request.method = m_HttpParser.GetMethod();; request.nContentLen = nContentLen; request.pContent = pContent; request.strAccessHost = m_HttpParser.GetHost(); request.strContentType = m_HttpParser.GetContentType(); request.strUrl = m_HttpParser.GetUrl() + 1; CHttpTask* pTask = new CHttpTask(request); if(HTTP_GET == m_HttpParser.GetMethod()) { g_GetThreadPool.AddTask(pTask); } else { g_PostThreadPool.AddTask(pTask); } } }