void RpcClientImpl::onOutput(StreamBuffer& sb) { try { _exceptionPending = false; sb.endWrite(); if (sb.out_avail() > 0) sb.beginWrite(); else sb.beginRead(); } catch (const std::exception&) { IRemoteProcedure* proc = _proc; cancel(); if (!proc) throw; _exceptionPending = true; proc->onFinished(); if (_exceptionPending) throw; } }
// 释放空间 void SendBuffer::Free(int32_t len) { if (len <= 0) { return; } AUTO_LOCK(_locker); _buf.Free(len); assert(!_buf.IsFull()); while (!_buf.IsFull() && !_standby_list.empty()) { StreamBuffer<kStandbyCapacity>* standby = _standby_list.front(); int32_t move_len = 0; const char * data = standby->Peek(move_len); assert(data && move_len > 0); int32_t push_len = _buf.Push(data, move_len); assert(push_len > 0); standby->Free(push_len); if (standby->IsEmpty()) { ObjectPool<StreamBuffer<kStandbyCapacity>>::Instance().Delete(standby); _standby_list.pop_front(); } } }
bool Socket::onOutput(StreamBuffer& sb) { log_trace("onOutput"); log_debug("send data to " << getPeerAddr()); try { sb.endWrite(); if ( sb.out_avail() ) { sb.beginWrite(); } else { if (sb.in_avail()) onInput(sb); else sb.beginRead(); } } catch (const std::exception& e) { log_warn("exception occured when processing request: " << e.what()); close(); return false; } return true; }
StreamBuffer StreamBuffer::cut(std::size_t size){ StreamBuffer ret; if(m_size <= size){ ret.swap(*this); } else { AUTO(it, m_chunks.begin()); std::size_t total = 0; // 这是 [m_chunks.begin(), it) 的字节数,不含零头。 while(total < size){ assert(it != m_chunks.end()); const std::size_t remaining = size - total; const std::size_t avail = it->writePos - it->readPos; if(remaining < avail){ AUTO_REF(back, pushBackPooled(ret.m_chunks)); std::memcpy(back.data, it->data + it->readPos, remaining); back.writePos = remaining; it->readPos += remaining; ret.m_size += remaining; m_size -= remaining; break; } total += avail; ++it; } ret.m_chunks.splice(ret.m_chunks.begin(), m_chunks, m_chunks.begin(), it); ret.m_size += total; m_size -= total; } return ret; }
StreamBuffer StreamBuffer::CutOff(std::size_t uSize){ StreamBuffer sbufRet; if(x_uSize <= uSize){ sbufRet.Swap(*this); } else { auto pNode = x_lstBuffers.GetFirst(); std::size_t uTotal = 0; // 这是 [x_lstBuffers.GetFirst(), pNode) 的字节数,不含零头。 while(uTotal < uSize){ ASSERT(pNode); auto &vBuffer = pNode->Get(); const std::size_t uRemaining = uSize - uTotal; const auto uAvail = vBuffer.m_uWrite - vBuffer.m_uRead; if(uRemaining < uAvail){ auto &vNewBuffer = xChunk::PushPooled(sbufRet.x_lstBuffers); vNewBuffer.m_uRead = 0; vNewBuffer.m_uWrite = uRemaining; std::memcpy(vNewBuffer.m_abyData, vBuffer.m_abyData + vBuffer.m_uRead, uRemaining); vBuffer.m_uRead += uRemaining; sbufRet.x_uSize += uRemaining; x_uSize -= uRemaining; break; } uTotal += uAvail; pNode = pNode->GetNext(); } sbufRet.x_lstBuffers.Splice(sbufRet.x_lstBuffers.GetFirst(), x_lstBuffers, x_lstBuffers.GetFirst(), pNode); sbufRet.x_uSize += uTotal; x_uSize -= uTotal; } return sbufRet; }
void writeBitsImpl(DataType value, int bitCount, StreamBuffer& streamBuffer, int& holdingBitCount, int& totalBitCount) { assert(isEnoughBits<DataType>(bitCount)); if (! isEnoughBits<DataType>(bitCount)) { throw StreamingException(__FILE__, __LINE__, "invalid bitCount"); } DataType theValue = resetUnusedBits(value, bitCount); if (holdingBitCount > 0) { if (streamBuffer.empty()) { throw StreamingException(__FILE__, __LINE__, "buffer is empty"); } streamBuffer.back() |= UInt8(theValue << holdingBitCount); } else { streamBuffer.push(UInt8(theValue)); } const int bitShiftCount = CHAR_BIT - holdingBitCount; if (bitCount > bitShiftCount) { int leftBitCount = bitCount - bitShiftCount; theValue >>= bitShiftCount; do { streamBuffer.push(UInt8(theValue)); theValue >>= CHAR_BIT; leftBitCount -= CHAR_BIT; } while (leftBitCount > 0); }
proto::message unmarshall(StreamBuffer& buff) { buff.set_mark(); auto result = proto::deserialize<proto::binary_message_deserializer>(buff); buff.unset_mark(); return result; }
std::ostream & operator<< (std::ostream &out, const SetFile manip) { StreamBuffer *buf = dynamic_cast<StreamBuffer *> (out.rdbuf ()); if (buf) buf->SetFile (manip.file); return out; }
std::ostream & operator<< (std::ostream &out, const SetLevel manip) { StreamBuffer *buf = dynamic_cast<StreamBuffer *> (out.rdbuf ()); if (buf) buf->SetLevel (manip.level); return out; }
StreamBuffer StreamBuffer::cut_off(std::size_t bytes){ StreamBuffer ret; const AUTO(bytes_to_copy, std::min(bytes, m_size)); if(bytes_to_copy == 0){ return ret; } if(m_size <= bytes_to_copy){ ret.swap(*this); return ret; } std::size_t bytes_copied = 0; AUTO(cut_end, m_first); for(;;){ const AUTO(bytes_remaining, bytes_to_copy - bytes_copied); const AUTO(bytes_avail, cut_end->end - cut_end->begin); if(bytes_remaining <= bytes_avail){ if(bytes_remaining == bytes_avail){ cut_end = cut_end->next; } else { const AUTO(chunk, new Chunk); chunk->next = cut_end; chunk->prev = cut_end->prev; chunk->begin = 0; chunk->end = bytes_remaining; std::memcpy(chunk->data, cut_end->data + cut_end->begin, bytes_remaining); cut_end->begin += bytes_remaining; if(cut_end->prev){ cut_end->prev->next = chunk; } else { m_first = chunk; } cut_end->prev = chunk; } break; } bytes_copied += bytes_avail; cut_end = cut_end->next; } const AUTO(cut_first, m_first); const AUTO(cut_last, cut_end->prev); cut_last->next = NULLPTR; cut_end->prev = NULLPTR; m_first = cut_end; m_size -= bytes_to_copy; ret.m_first = cut_first; ret.m_last = cut_last; ret.m_size = bytes_to_copy; return ret; }
void URLTable::serialize( StreamBuffer &stream ) { for ( std::map<int, string>::iterator it = map.begin(); it!=map.end(); ++it){ stream.write(&it->first); int tmp = it->second.length(); stream.write(&tmp); stream.write(it->second.c_str(), it->second.length()); } }
void Stream::ReadData(const StreamBuffer& value) { if (streamMode == SmMemory) { unsigned int size = std::max(static_cast<int>(value.size()), static_cast<int>(value.size() - buffer.size())); std::memcpy((void*)value.data(), buffer.data() + position, size); position += value.size(); } else fileStream.read(reinterpret_cast<char*>(const_cast<unsigned char*>(value.data())), value.size()); }
void WordMap::serialize( StreamBuffer &stream ) { for ( std::map<string, int>::iterator it = map.begin(); it!=map.end(); ++it) { int tmp = it->first.length(); stream.write(&tmp); stream.write(it->first.c_str(), it->first.length()); stream.write(&it->second); } }
std::ostream & operator<< (std::ostream &out, const LockType) { StreamBuffer *buf = dynamic_cast<StreamBuffer *> (out.rdbuf ()); if (buf) { while (buf->IsLocked ()); // Loop infinitely until we can lock. buf->Lock (); } return out; }
void ClientImpl::onInput(StreamBuffer& sb) { try { try { log_trace("ClientImpl::onInput; readHeader=" << _readHeader); _errorPending = false; sb.endRead(); if (sb.device()->eof()) throw IOError("end of input"); _reconnectOnError = false; if (_readHeader) { processHeaderAvailable(sb); } else { processBodyAvailable(sb); } } catch (const IOError& e) { // after writing the request, the first read request may // detect, that the server has already closed the connection, // so check it here if (_readHeader && _reconnectOnError && _request != 0) { log_debug("reconnect on error"); _socket.close(); _reconnectOnError = false; reexecuteBegin(*_request); return; } throw; } } catch (const std::exception& e) { _errorPending = true; _client->replyFinished(*_client); if (_errorPending) throw; } }
bool Socket::onOutput(StreamBuffer& sb) { log_trace("onOutput"); log_debug("send data to " << getPeerAddr()); try { sb.endWrite(); if ( sb.out_avail() ) { sb.beginWrite(); _timer.start(_server.writeTimeout()); } else { bool keepAlive = _request.header().keepAlive() && _reply.header().keepAlive(); if (keepAlive) { log_debug("do keep alive"); _timer.start(_server.keepAliveTimeout()); _request.clear(); _reply.clear(); _parser.reset(false); if (sb.in_avail()) onInput(sb); else _stream.buffer().beginRead(); } else { log_debug("don't do keep alive"); close(); return false; } } } catch (const std::exception& e) { log_warn("exception occured when processing request: " << e.what()); close(); timeout(*this); return false; } return true; }
void ClientImpl::onOutput(StreamBuffer& sb) { log_trace("ClientImpl::onOutput; out_avail=" << sb.out_avail()); try { try { _errorPending = false; sb.endWrite(); if( sb.out_avail() > 0 ) { sb.beginWrite(); } else { sb.beginRead(); _client->requestSent(*_client); _parser.reset(true); _readHeader = true; } } catch (const IOError& e) { if (_reconnectOnError && _request != 0) { log_debug("reconnect on error"); _socket.close(); _reconnectOnError = false; reexecuteBegin(*_request); return; } throw; } } catch (const std::exception& e) { log_warn("error of type " << typeid(e).name() << " occured: " << e.what()); _errorPending = true; _client->replyFinished(*_client); if (_errorPending) throw; } }
void URLTable::deserialize( StreamBuffer &stream ){ map.clear(); while(stream.active()){ int key; stream.read(&key); int length; stream.read(&length); char buffer[length+1]; buffer[length]='\0'; stream.read(buffer,length); string buf(buffer); map.insert(pair<int,string>(key,buf)); } }
StreamBuffer::StreamBuffer(const StreamBuffer &rhs) : m_first(NULLPTR), m_last(NULLPTR), m_size(0) { for(AUTO(ce, rhs.get_chunk_enumerator()); ce; ++ce){ put(ce.data(), ce.size()); } }
void RpcClientImpl::onInput(StreamBuffer& sb) { try { _exceptionPending = false; sb.endRead(); if (sb.device()->eof()) throw IOError("end of input"); while (_stream.buffer().in_avail()) { char ch = StreamBuffer::traits_type::to_char_type(_stream.buffer().sbumpc()); if (_scanner.advance(ch)) { _scanner.finish(); IRemoteProcedure* proc = _proc; _proc = 0; proc->onFinished(); return; } } if (!_stream) { close(); throw std::runtime_error("reading result failed"); } sb.beginRead(); } catch (const std::exception&) { IRemoteProcedure* proc = _proc; cancel(); if (!proc) throw; _exceptionPending = true; proc->onFinished(); if (_exceptionPending) throw; } }
void file_get_contents(StreamBuffer &contents, const char *path){ const UniqueFile file(::open(path, O_RDONLY)); if(!file){ DEBUG_THROW(SystemException); } StreamBuffer temp; for(;;){ char read_buf[4096]; const ::ssize_t bytes_read = ::read(file.get(), read_buf, sizeof(read_buf)); if(bytes_read < 0){ DEBUG_THROW(SystemException); } else if(bytes_read == 0){ break; } temp.put(read_buf, static_cast<std::size_t>(bytes_read)); } contents.splice(temp); }
bool getLine(StreamBuffer &buffer, std::string &line){ line.clear(); if(buffer.empty()){ return false; } do { const int ch = buffer.get(); if(ch == '\n'){ break; } else if(ch == '\r'){ if(buffer.peek() == '\n'){ continue; } break; } line.push_back(ch); } while(!buffer.empty()); return true; }
void ListenerWorker::onMainReceived(TcpConnection* conn, StreamBuffer& istream, StreamBuffer& ostream) { #if 0 if (istream.length() < sizeof(UInt32) + strlen(sClientConnectionStr)) { return; } UInt32 connStrLen = 0; istream >> connStrLen; connStrLen = connStrLen ^ 2865; if (connStrLen != strlen(sClientConnectionStr)) { LOG_WARN("Player Connection string length don't match."); conn->close(); return; } String connStr; for (UInt32 i = 0; i < connStrLen; ++i) { unsigned char c; istream >> c; c = c ^ 103; connStr += c; } LOG_DEBUG("Received a Player connection string [%s]", connStr.c_str()); if (connStr != sClientConnectionStr) { LOG_WARN("Player Connection string don't match."); conn->close(); return; } #endif //删除回调函数,绑定别的回调函数做准备 王戊辰 LYNX_DEREGISTER_RECEIVED(conn, this, &ListenerWorker::onMainReceived); LYNX_DEREGISTER_CONNECT_BROKEN(conn, this, &ListenerWorker::onMainDisconnected); //从主链接上删除,绑定到network的连接上 王戊辰 mMainConnectionSet.erase(conn); //内部删除了读写事件 王戊辰 conn->pause(); //把io服务断开 王戊辰 conn->setService(NULL); ConnectionAcceptedNotify notify; notify.mType = 0; notify.mConnPointer = conn; postMsgToOutputQueue(notify, 0); }
OpBitmap* IconUtils::GetBitmap(const OpStringC& filename, int width, int height) { OpFile file; OpFileLength length; if (OpStatus::IsError(file.Construct(filename)) || OpStatus::IsError(file.Open(OPFILE_READ)) || OpStatus::IsError(file.GetFileLength(length)) || length == 0) return 0; OpFileLength bytes_read; char* data = OP_NEWA(char, length); if (!data || OpStatus::IsError(file.Read(data, length, &bytes_read)) || bytes_read != length) { OP_DELETEA(data); return 0; } StreamBuffer<UINT8> buffer; buffer.TakeOver(data, length); return GetBitmap(buffer, width, height); }
void Socket::onInput(StreamBuffer& sb) { log_debug("onInput"); sb.endRead(); if (sb.in_avail() == 0 || sb.device()->eof()) { close(); return; } while (sb.in_avail() > 0) { if (_responder.advance(sb.sbumpc())) { _responder.finalize(_stream); buffer().beginWrite(); onOutput(sb); return; } } sb.beginRead(); }
void Socket::onInput(StreamBuffer& sb) { log_debug("onInput"); sb.endRead(); if (sb.in_avail() == 0 || sb.device()->eof()) { close(); return; } if (_responder.onInput(_stream)) { sb.beginWrite(); onOutput(sb); } else { sb.beginRead(); } }
int main(int, char const* const*) { try { using libember::util::StreamBuffer; StreamBuffer<unsigned short, 256> testStream; for (unsigned int i = 0; i < 1000; ++i) { { unsigned short writeBuffer[7]; for (unsigned int j = 0; j < 7; ++j) { writeBuffer[j] = static_cast<unsigned short>(i * 7 + j); } unsigned short const* const begin = writeBuffer; unsigned short const* const end = writeBuffer + sizeof(writeBuffer) / sizeof(writeBuffer[0]); testStream.append(begin, end); } for (unsigned int j = 0; j < 5; ++j) { unsigned short const current = testStream.front(); testStream.consume(); if (current != (i * 5 + j)) { THROW_TEST_EXCEPTION("Invalid head of buffer! Expected " << (i * 5 + j) << ", found " << current); } } std::size_t const size = testStream.size(); if (size != 2 * (i + 1)) { THROW_TEST_EXCEPTION("Invalid size of buffer! Expected " << ((i + 1) * 2) << ", found " << size); } } for (unsigned int i = 0; i < 1000; ++i) { for (unsigned int j = 0; j < 2; ++j) { unsigned short const current = testStream.front(); testStream.consume(); if (current != (5000 + i * 2 + j)) { THROW_TEST_EXCEPTION("Invalid head of buffer! Expected " << ((i + 1000) * 5 + j) << ", found " << current); } } } } catch (std::exception const& e) { std::cerr << "ERROR: " << e.what() << std::endl; return 1; } return 0; }
void ListenerWorker::onGMReceived(TcpConnection* conn, StreamBuffer& istream, StreamBuffer& ostream) { #if 0 if (istream.length() < sizeof(UInt32) + strlen(sGMConnectionStr)) { return; } UInt32 connStrLen = 0; istream >> connStrLen; connStrLen = connStrLen ^ 2865; if (connStrLen != strlen(sGMConnectionStr)) { LOG_WARN("GMClient Connection string length don't match."); conn->close(); return; } String connStr; for (UInt32 i = 0; i < connStrLen; ++i) { unsigned char c; istream >> c; c = c ^ 103; connStr += c; } LOG_DEBUG("Received a GMClient connection string [%s]", connStr.c_str()); if (connStr != sGMConnectionStr) { LOG_WARN("GMClient Connection string don't match."); conn->close(); return; } #endif LYNX_DEREGISTER_RECEIVED(conn, this, &ListenerWorker::onGMReceived); LYNX_DEREGISTER_CONNECT_BROKEN(conn, this, &ListenerWorker::onGMDisconnected); mGMConnectionSet.erase(conn); conn->pause(); conn->setService(NULL); ConnectionAcceptedNotify notify; notify.mType = 1; notify.mConnPointer = conn; postMsgToOutputQueue(notify, 0); }
void printInfo(StreamBuffer &sb, int bs_type) { std::cout << sb.getStreamInfo() << std::endl; SpdifFrameParser spdifFrameParser(false); HeaderInfo hi = sb.getHeaderInfo(); if ( hi.getSpeakers().isSpdif() ) { if ( spdifFrameParser.parseFrame(sb.getFrame(), sb.getFrameSize()) ) hi = spdifFrameParser.getHeaderInfo(); else std::cout << "\nERROR!!!\nCannot parse SPDIF frame" << std::endl; } if ( is14Bit(bs_type) && ! hi.getSpeakers().isDts() ) { std::cout << "\nWARNING!!!" << std::endl << hi.getSpeakers().getFormatText() << " does not support 14bit stream format!" << std::endl << "It will be converted to byte stream." << std::endl << std::endl; } }
void file_put_contents(const char *path, StreamBuffer contents, bool append){ const UniqueFile file(::open(path, O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC))); if(!file){ DEBUG_THROW(SystemException); } for(;;){ char write_buf[4096]; const std::size_t bytes_to_write = contents.get(write_buf, sizeof(write_buf)); if(bytes_to_write == 0){ break; } if(::write(file.get(), write_buf, bytes_to_write) < static_cast< ::ssize_t>(bytes_to_write)){ DEBUG_THROW(SystemException); } } }