00001 /* 00002 00003 Copyright (C) 2005-2007 by Peter Dimov. 00004 00005 This file is part of Calitko (http://www.calitko.org). 00006 00007 Calitko is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 Calitko is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with Calitko; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 */ 00022 00023 #ifndef PROTOCOLS__GENERICS__TCP_SOCKET_BUFFER_H 00024 #define PROTOCOLS__GENERICS__TCP_SOCKET_BUFFER_H 00025 00026 #include "Imports.h" 00027 #include "SocketBuffer.h" 00028 00029 namespace Protocols { 00030 namespace Generics { 00031 00033 00045 class TcpSocketBuffer : public SocketBuffer 00046 { 00047 CALITKO_TESTABLE 00048 REFERENCE_OBJECT (TcpSocketBuffer) 00049 00050 public: 00051 TcpSocketBuffer (int readBufferSize, int writeBufferSize); 00052 ~TcpSocketBuffer(); 00053 00054 bool canRead (int count) const; 00055 00056 QByteArray peek (int count) const; 00057 QByteArray peekAtMost (int count) const; 00058 QByteArray peekTo (const QByteArray &delimiter) const; 00059 QByteArray peekAll() const; 00060 00061 QByteArray read (int count); 00062 QByteArray readAtMost (int count); 00063 QByteArray readTo (const QByteArray &delimiter); 00064 QByteArray readAll(); 00065 00066 bool canWrite (int count) const; 00067 00068 bool write (const QByteArray &bytes, bool flush = true); 00069 00070 void tryReadFrom (Socket *socket); 00071 void tryWriteTo (Socket *socket); 00072 00073 private: 00074 void peek (char *destination, int size) const; 00075 void purgeReadBuffer (int count); 00076 void setReadBufferSize (int size); 00077 int readBufferSize() const; 00078 int bytesToRead() const; 00079 00080 char * nextReadChunk() const; 00081 int nextReadChunkSize() const; 00082 void hasRead (int count); 00083 00084 void write (const char *source, int size, bool flush); 00085 void setWriteBufferSize (int size); 00086 int writeBufferSize() const; 00087 int bytesToWrite() const; 00088 00089 char * nextWriteChunk() const; 00090 int nextWriteChunkSize() const; 00091 void hasWritten (int count); 00092 00093 private: 00094 char * readBuffer; 00095 int readSize; 00096 int readStart; 00097 int readEnd; 00098 char * writeBuffer; 00099 int writeSize; 00100 int writeStart; 00101 int writeEnd; 00102 int writeFlushEnd; 00103 }; 00104 00105 } // namespace Generics 00106 } // namespace Protocols 00107 00108 #endif // PROTOCOLS__GENERICS__TCP_SOCKET_BUFFER_H