CNetCacheAPI::EReadResult CNetCacheAPI::GetData( const string& key, CSimpleBuffer& buffer) { size_t x_blob_size = 0; auto_ptr<IReader> reader(GetData(key, &x_blob_size, eCaching_Disable)); if (reader.get() == 0) return eNotFound; buffer.resize_mem(x_blob_size); return m_Impl->ReadBuffer(*reader, (char*) buffer.data(), x_blob_size, NULL, x_blob_size); }
void Decode<Uint4, float>(const CSimpleBuffer& data, CScoreVector<Uint4, float>& vec) { CRawScoreVector<Uint4, float> raw; Decode(&data[0], data.size(), raw); vec = raw; }
int32_t CWGDConn::ParseBuffer(CSimpleBuffer& simpleBuffer) { uint32_t uintHave = simpleBuffer.GetWriteOffset(); int32_t nHeadSize = sizeof(WGDHEAD); if (uintHave < nHeadSize) { return -1; } uchar_t* pBuffer = simpleBuffer.GetBuffer(); WGDHEAD* pHead = (WGDHEAD*)pBuffer; int32_t nTotalSize = nHeadSize + pHead->nDataLen; if (uintHave < nTotalSize) { return -1; } return DealBuffer(simpleBuffer); }
/** @fn int32_t CTCPClientAsync::SendBufferAsync() * @brief 将待发送队列中的数据发送出去 * @return */ int32_t CTCPClientAsync::SendBufferAsync() { int32_t nErrorCode = 0; m_sendqueuemutex.Lock(); if (m_sendqueue.size() == 0) { //待发送队列中为空,则删除写事件的注册,改成读事件 m_pio->Remove_WriteEvent(this); m_sendqueuemutex.Unlock(); if (_GetWaitForCloseStatus() == TRUE) { //待发送内容发送完毕,则关闭链接 _Close(); } return nErrorCode; } CSimpleBuffer* pBufferLoop = m_sendqueue.front(); m_sendqueuemutex.Unlock(); int32_t nRet = S_Send(GetSocket(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { #if (defined(_WIN32) || defined(_WIN64)) int32_t nError = ::GetLastError(); if (WSAEWOULDBLOCK == nError) #else int32_t nError = errno; if (EAGAIN == nError) #endif { SOCKET_IO_DEBUG("send tcp data, buffer is blocking.") } else { _ClearSendBuffer(); SOCKET_IO_ERROR("send tcp data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_TCP_SEND_FAILED); } }
int32_t CWGDConn::DealBuffer(CSimpleBuffer & simpleBuffer) { uchar_t* pBuffer = simpleBuffer.GetBuffer(); WGDHEAD* pHead = (WGDHEAD*)pBuffer; int32_t nTotalSize = sizeof(WGDHEAD) + pHead->nDataLen; //m_pBaseSocket->m_pBaseServer->OnStatisticEnd(); m_pServer->OnReceivedNotify(m_pBaseSocket->GetSocket(), pBuffer, nTotalSize); simpleBuffer.Read(NULL, nTotalSize); return 0; #if 0 uchar_t* pBuffer = simpleBuffer.GetBuffer(); WGDHEAD* pHead = (WGDHEAD*)pBuffer; char* name = pHead->szFileName; switch (pHead->type) { case eNet_Upload: { std::string strFilePath = RECEIVE_DIR + std::string(pHead->szFileName); int nHeadLen = pHead->nDataLen; m_in_buf.Read(NULL, sizeof(WGDHEAD)); int32_t nWrite = WriteFile(strFilePath, m_in_buf.GetBuffer(), nHeadLen); if (nWrite > 0 && nWrite == nHeadLen) { m_in_buf.Read(NULL, nWrite); } return 0; } break; case eNet_Download: { std::string strFilePath = RECEIVE_DIR + std::string(name); m_in_buf.Read(NULL, sizeof(WGDHEAD)); CSimpleBuffer tmpBuffer; WGDHEAD head; head.type = eNet_Download; //strncpy(head.szFileName, pHead->szFileName, sizeof(head.szFileName)); tmpBuffer.Write((void*)&head, sizeof(WGDHEAD)); int32_t nRead = ReadFile(strFilePath, tmpBuffer); WGDHEAD* pHead = (WGDHEAD*)tmpBuffer.GetBuffer(); pHead->nDataLen = nRead; Send(tmpBuffer.GetBuffer(), tmpBuffer.GetWriteOffset()); } break; case eNet_GetDirectory: { } break; default: break; } return int32_t(0); #endif }
int32_t CSSLClientAsync::SendBufferAsync() { int32_t nErrorCode = SOCKET_IO_RESULT_OK; m_sendqueuemutex.Lock(); if (m_sendqueue.size() == 0) { SOCKET_IO_DEBUG("ssl send queue is empty."); //待发送队列中为空,则删除写事件的注册,改成读事件 m_pio->Remove_WriteEvent(this); m_sendqueuemutex.Unlock(); if (_GetWaitForCloseStatus() == TRUE) { //待发送内容发送完毕,则关闭链接 _Close(); } return nErrorCode; } CSimpleBuffer* pBufferLoop = m_sendqueue.front(); m_sendqueuemutex.Unlock(); int32_t nRet = SSL_write(GetSSL(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_WANT_WRITE == nError || SSL_ERROR_WANT_READ == nError) { SOCKET_IO_INFO("send ssl data, buffer is blocking, errno: %d.", nError); } else { _ClearSendBuffer(); SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } } else if (nRet == 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_ZERO_RETURN == nError) { SOCKET_IO_WARN("send ssl data error, peer closed."); } else { SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); } _ClearSendBuffer(); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } else if (nRet != pBufferLoop->GetWriteOffset()) { //将未成功的数据重新放置buffer loop中,待下次发送 //对于ssl来说,应该不会出现此种情况 int32_t nSize = 0; pBufferLoop->Read(NULL, nRet); SOCKET_IO_WARN("send ssl data, send size: %d, less than %d.", nRet, pBufferLoop->GetWriteOffset()); } else { SOCKET_IO_INFO("send ssl data from buffer successed."); m_sendqueuemutex.Lock(); delete pBufferLoop; pBufferLoop = NULL; m_sendqueue.pop(); m_sendqueuemutex.Unlock(); } return nErrorCode; }
int32_t CSSLClientAsync::SendMsgAsync(const char *szBuf, int32_t nBufSize) { CSimpleBuffer* pBufferLoop = new CSimpleBuffer(); pBufferLoop->Write(szBuf, nBufSize); m_sendqueuemutex.Lock(); if (m_sendqueue.size() != 0) { if (_GetWaitForCloseStatus() == TRUE) { SOCKET_IO_DEBUG("send ssl data error, socket will be closed."); delete pBufferLoop; pBufferLoop = NULL; } else { if (m_sendqueue.size() >= MAX_SEND_QUEUE_SIZE) { SOCKET_IO_WARN("send ssl data error, buffer is overload."); delete pBufferLoop; pBufferLoop = NULL; } else { SOCKET_IO_INFO("send ssl data, push data to buffer."); m_sendqueue.push(pBufferLoop); //m_pio->Add_WriteEvent(this); } } m_sendqueuemutex.Unlock(); return SOCKET_IO_RESULT_OK; } m_sendqueuemutex.Unlock(); int32_t nRet = SSL_write(GetSSL(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_WANT_WRITE == nError || SSL_ERROR_WANT_READ == nError) { m_sendqueuemutex.Lock(); m_sendqueue.push(pBufferLoop); m_sendqueuemutex.Unlock(); //有数据放入待发送队列,则注册为写事件 m_pio->Add_WriteEvent(this); SOCKET_IO_INFO("send ssl data, buffer is blocking, errno: %d.", nError); } else { delete pBufferLoop; pBufferLoop = NULL; SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } } else if (nRet == 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_ZERO_RETURN == nError) { SOCKET_IO_WARN("send ssl data error, peer closed."); } else { SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); } delete pBufferLoop; pBufferLoop = NULL; DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } else if (nRet != nBufSize) { int32_t nRest = nBufSize - nRet; pBufferLoop->Read(NULL, nRet); m_sendqueuemutex.Lock(); m_sendqueue.push(pBufferLoop); m_sendqueuemutex.Unlock(); //有数据放入待发送队列,则注册为写事件 //对于ssl来说,应该不会出现此种情况 m_pio->Add_WriteEvent(this); SOCKET_IO_WARN("send ssl data, send size: %d, less than %d.", nRet, nBufSize); } else if (nRet == nBufSize) { delete pBufferLoop; pBufferLoop = NULL; SOCKET_IO_DEBUG("send ssl data successed."); } else { delete pBufferLoop; pBufferLoop = NULL; } return SOCKET_IO_RESULT_OK; }
void Decode<Uint4, float>(const CSimpleBuffer& data, CRawScoreVector<Uint4, float>& vec) { Decode(&data[0], data.size(), vec); }