static void ReadCallback(void* data, int32_t result) { if (result == PP_OK) { /* We're done reading the file. */ DecodeAndPlayOggFile(); } else if (result > 0) { /* 'result' bytes were read into memory. */ ogg_file_size += (size_t)result; ReadSome(data); } else assert(0); }
void AddressBookSubscription::Request () { // must be run in separate thread LogPrint (eLogInfo, "Downloading hosts from ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); bool success = false; i2p::util::http::url u (m_Link); i2p::data::IdentHash ident; if (m_Book.GetIdentHash (u.host_, ident)) { std::condition_variable newDataReceived; std::mutex newDataReceivedMutex; auto leaseSet = m_Book.getSharedLocalDestination()->FindLeaseSet (ident); if (!leaseSet) { std::unique_lock<std::mutex> l(newDataReceivedMutex); m_Book.getSharedLocalDestination()->RequestDestination (ident, [&newDataReceived, &leaseSet](std::shared_ptr<i2p::data::LeaseSet> ls) { leaseSet = ls; newDataReceived.notify_all (); }); if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout) LogPrint (eLogError, "Subscription LeseseSet request timeout expired"); } if (leaseSet) { std::stringstream request, response; // standard header request << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_ << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n"; if (m_Etag.length () > 0) // etag request << i2p::util::http::IF_NONE_MATCH << ": \"" << m_Etag << "\"\r\n"; if (m_LastModified.length () > 0) // if-modfief-since request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; request << "\r\n"; // end of header auto stream = m_Book.getSharedLocalDestination()->CreateStream (leaseSet, u.port_); stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ()); uint8_t buf[4096]; bool end = false; while (!end) { stream->AsyncReceive (boost::asio::buffer (buf, 4096), [&](const boost::system::error_code& ecode, std::size_t bytes_transferred) { if (bytes_transferred) response.write ((char *)buf, bytes_transferred); if (ecode == boost::asio::error::timed_out || !stream->IsOpen ()) end = true; newDataReceived.notify_all (); }, 30); // wait for 30 seconds std::unique_lock<std::mutex> l(newDataReceivedMutex); if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout) LogPrint (eLogError, "Subscription timeout expired"); } // process remaining buffer while (size_t len = stream->ReadSome (buf, 4096)) response.write ((char *)buf, len); // parse response std::string version; response >> version; // HTTP version int status = 0; response >> status; // status if (status == 200) // OK { bool isChunked = false; std::string header, statusMessage; std::getline (response, statusMessage); // read until new line meaning end of header while (!response.eof () && header != "\r") { std::getline (response, header); auto colon = header.find (':'); if (colon != std::string::npos) { std::string field = header.substr (0, colon); header.resize (header.length () - 1); // delete \r if (field == i2p::util::http::ETAG) m_Etag = header.substr (colon + 1); else if (field == i2p::util::http::LAST_MODIFIED) m_LastModified = header.substr (colon + 1); else if (field == i2p::util::http::TRANSFER_ENCODING) isChunked = !header.compare (colon + 1, std::string::npos, "chunked"); } } LogPrint (eLogInfo, m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); if (!response.eof ()) { success = true; if (!isChunked) m_Book.LoadHostsFromStream (response); else { // merge chunks std::stringstream merged; i2p::util::http::MergeChunkedResponse (response, merged); m_Book.LoadHostsFromStream (merged); } } } else if (status == 304) { success = true; LogPrint (eLogInfo, "No updates from ", m_Link); } else LogPrint (eLogWarning, "Adressbook HTTP response ", status); } else
void TCPConnection::CallBack_Read_Some(const boost::system::error_code &ec, hf_uint16 size) { if ( !ec ) { // Logger::GetLogger()->Debug("size:%u",size); m_dataPos += size; hf_uint16 currentPos = 0; STR_PackHead head; SessionMgr::SessionPointer smap = SessionMgr::Instance()->GetSession(); while(currentPos < m_dataPos) { if(m_dataPos < sizeof(STR_PackHead)) { Logger::GetLogger()->Debug("datalen:%u",m_dataPos); break; } memcpy(&head, m_buf + currentPos, sizeof(STR_PackHead)); if(head.Len > 512) { Logger::GetLogger()->Debug("Client head.len > 512 Disconnected"); Server::GetInstance()->GetPlayerLogin()->SavePlayerOfflineData(shared_from_this() ); } if(currentPos + sizeof(STR_PackHead) + head.Len == m_dataPos) //最后一个包 { memcpy(m_pack.data, m_buf + currentPos, sizeof(STR_PackHead) + head.Len); hf_uint8 value = JudgePlayerLogin(head.Flag); if(value == 2) { m_pack.roleid = (*smap)[shared_from_this()].m_roleid; Server::GetInstance()->PushPackage(m_pack); } else if(value == 1)//未登录角色 { CommandParseLogin(shared_from_this(), m_pack.data); } m_dataPos = 0; break; } else if(currentPos + sizeof(STR_PackHead) + head.Len < m_dataPos) //未处理的数据长度多于一个包 { memcpy(m_pack.data, m_buf + currentPos, sizeof(STR_PackHead) + head.Len); hf_uint8 value = JudgePlayerLogin(head.Flag); if(value == 2) { m_pack.roleid = (*smap)[shared_from_this()].m_roleid; Server::GetInstance()->PushPackage(m_pack); } else if(value == 1)//未登录角色 { CommandParseLogin(shared_from_this(), m_pack.data); } currentPos += sizeof(STR_PackHead) + head.Len; continue; } else //不够一个包的长度 { Logger::GetLogger()->Debug("you wei jie xi de bao currentPos:%u,m_dataPos:%u,head.len:%u,head.flag:%u",currentPos,m_dataPos,head.Len, head.Flag); hf_char buff[TCP_BUFFER_SIZE] = { 0 }; memcpy(buff, m_buf + currentPos, m_dataPos - currentPos); memcpy(m_buf, buff, TCP_BUFFER_SIZE); m_dataPos -= currentPos; break; } } ReadSome(); } else if ( size == 0 || ec == boost::asio::error::eof || ec == boost::asio::error::shut_down) { Logger::GetLogger()->Debug("Client head Disconnected"); Server::GetInstance()->GetPlayerLogin()->SavePlayerOfflineData(shared_from_this()); } }
void TCPConnection::Start() { ReadSome(); }
int CSTREAMSOCK::ReadSomeTimeout (int size, void *buf, const milliseconds_t timeout) const { GTL ("CSTREAMSOCK::ReadSomeTimeout()"); if (timeout < 0) { return ReadSome (size, buf); } // struct timeval start; gettimeofday (&start, 0x0); milliseconds_t timeRemain = timeout; // while (42) { fd_set rfd; FD_ZERO (&rfd); FD_SET (_fd, &rfd); struct timeval tv = {timeRemain / 1000, (timeRemain % 1000) * 1000}; int ret = select (_fd + 1, &rfd, 0x0, 0x0, &tv); if (ret > 1) { GTLFATAL (("select() weirdness, returned %i\n", ret)); abort (); } if (ret == 1) // got data { if (!FD_ISSET (_fd, &rfd)) { GTLFATAL (("select() failed\n")); return -1; } break; } if (!ret) // timeout { return 0; } if (errno == EINTR) // the only case where we restart { // ++itschere20030318: linux manpage says that only linux select() // changes the timeout argument to indicate how much time was left, // other systems (probably qnx?) don't do that. so we can't rely on // this behaviour and have to calculate the remaining timeout on our // own. struct timeval now; gettimeofday (&now, 0x0); const milliseconds_t elapsed = 1000 * (now.tv_sec - start.tv_sec) + (now.tv_usec - start.tv_usec) / 1000; timeRemain = elapsed > timeout ? 0 : timeout - elapsed; continue; } GTLFATAL (("select() failed\n")); return -1; } // // now we know that "some" data is available, but not yet how much. // #ifdef _WIN32 unsigned long cnt; #else int cnt; #endif if (ioctl (_fd, FIONREAD, &cnt) < 0) { GTLFATAL (("ioctl(FIONREAD) failed\n")); return -1; } if (cnt > size) { cnt = size; } // // now we know how much data we can safely read. // #ifdef _WIN32 int done = recv (_fd, (char *)buf, cnt, 0); #else int done = read (_fd, buf, cnt); #endif if (done != cnt) { GTLFATAL (("read() failed\n")); #if 0 int nWSAError = WSAGetLastError(); printf("done: %d, WSAError: %d\n", done, nWSAError); #endif } return done; }
BOOL CIVPlaybackDataBuf::ChannelTarget::MoveToAndReadSome( const FILETIME& time) { if ( !m_Reader.is_open() ) { return FALSE; } size_t nQueueSize = m_DataIndex.size(); if ( (time < m_BeginTime || time > m_EndTime) || nQueueSize == 0 ) { //m_Reader.close(); return FALSE; } ClearTempData(); size_t nPos = GetPos(time); if ( nPos == size_t(-1) ) { assert(FALSE); m_Reader.close(); return FALSE; } FILETIME CurrentFrameTime; m_IVSomeData.clear(); IVFileDataIndex& TmpDataIndex = m_DataIndex[nPos]; m_Reader.seekg(TmpDataIndex.DataOffset); if ( nPos == nQueueSize - 1 ) { if ( ReadSome(CurrentFrameTime) ) { m_BufStartTime = m_BufEndTime = CurrentFrameTime; assert(CurrentFrameTime==m_EndTime); } m_BufStartTime = m_BufEndTime = s_ErrFileTime; return FALSE; } BOOL bFirstFrame = TRUE; IVFileDataIndex& TmpDataIndex2 = m_DataIndex[nPos+1]; size_t nCurrentPos; while ( (nCurrentPos = m_Reader.tellg()) < TmpDataIndex2.DataOffset ) { if ( !ReadSome(CurrentFrameTime) ) { if ( bFirstFrame ) { m_BufStartTime = m_BufEndTime = s_ErrFileTime; } return FALSE; } if ( bFirstFrame ) { m_BufStartTime = m_BufEndTime = CurrentFrameTime; bFirstFrame = FALSE; } else { m_BufEndTime = CurrentFrameTime; } } return TRUE; }
static void OpenCallback(void* data, int32_t result) { assert(result == PP_OK); ReadSome(data); }