/*----------------------------------------------------------------------------- * datagram.cpp * Copyright (C) 2004, 2005 Akito Nozaki * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Original code came from cutestuff bytestream.h and cpp by Justin K. * Converted to use Tanoshi library stuff. * */ #include #include #include NAMESPACE_TANOSHI { //============================================ // DataGramPrivate code starts here DataGramPrivate::DataGramPrivate() { } DataGramPrivate::~DataGramPrivate() { } void DataGramPrivate::reset() { resetReadBuffer(); resetWriteBuffer(); } void DataGramPrivate::resetReadBuffer() { while(readBuf.size()) { delete readBuf.front(); readBuf.pop(); } } void DataGramPrivate::resetWriteBuffer() { while(writeBuf.size()) { delete writeBuf.front(); writeBuf.pop(); } } //============================================ // DataGram code starts here DataGram::DataGram(Object *parent) : Object(parent), d(new DataGramPrivate) { } DataGram::~DataGram() { } bool DataGram::isOpen() const { return false; } void DataGram::close() { } void DataGram::write(DGramPacket *dgp) { if(isOpen()) return; bool doWrite = packetsToWrite() == 0 ? true : false; appendWrite(dgp); } DGramPacket *DataGram::read() { return takeRead(); } int DataGram::packetsAvailable() const { return d->readBuf.size(); } int DataGram::packetsToWrite() const { return d->writeBuf.size(); } void DataGram::clearReadBuffer() { d->resetReadBuffer(); } void DataGram::clearWriteBuffer() { d->resetWriteBuffer(); } void DataGram::appendRead(DGramPacket *dgp) { d->readBuf.push(dgp); } void DataGram::appendWrite(DGramPacket *dgp) { d->writeBuf.push(dgp); } // TODO del == false this will cause problems!! DGramPacket *DataGram::takeRead(bool del) { if(!packetsAvailable()) return NULL; DGramPacket *t(d->readBuf.front()); if(del) { d->readBuf.pop(); } return t; } // TODO DGramPacket *DataGram::takeWrite(bool del) { if(!packetsToWrite()) return NULL; DGramPacket *t(d->writeBuf.front()); if(del) { d->writeBuf.pop(); } return t; } std::queue &DataGram::readBuf() { return d->readBuf; } std::queue &DataGram::writeBuf() { return d->writeBuf; } int DataGram::tryWrite() { return -1; } }