30 m_pSocket->setSocket ( nSocket, MSocketDevice::Stream );
34 struct linger ling = {1, 1};
36 if ( setsockopt(
socket(), SOL_SOCKET, SO_LINGER, (
const char *)&ling,
38 LOG(VB_GENERAL, LOG_ERR,
39 "BufferedSocketDevice: setsockopt - SO_LINGER: " +
ENO);
141 QHostAddress hostAddress, quint16 nPort)
174 qlonglong maxToRead = 0;
180 if ( maxToRead <= 0 )
184 qlonglong nbytes =
m_pSocket->bytesAvailable();
187 QByteArray *a =
nullptr;
191 a =
new QByteArray();
195 a->data(), maxToRead ? std::min(nbytes, maxToRead) : nbytes);
197 if (( nread > 0 ) && ( nread != a->size() ))
208 for(
long n = 0; n < a->count(); n++ )
209 msg += QString(
"%1").arg(a->at(n));
210 LOG(VB_GENERAL, LOG_DEBUG, msg);
265 bool osBufferFull =
false;
270 deque<QByteArray*>::iterator it =
m_bufWrite.begin();
282 int s = a->size() - j;
284 while ( a && i+s < out.size() )
286 memcpy( out.data()+i, a->data()+j, s );
291 s = a ? a->size() : 0;
297 nwritten =
m_pSocket->writeBlock( out.data(), i );
354 m_bufRead.consumeBytes( (qulonglong)index,
nullptr );
391 int msecs,
bool *pTimeout )
393 bool bTimeout =
false;
429 nBytes =
m_pSocket->waitForMore( msecs, &bTimeout );
431 if (pTimeout !=
nullptr)
432 *pTimeout = bTimeout;
477 if ( data ==
nullptr && maxlen != 0 )
485 if ( maxlen >= (qulonglong)
m_bufRead.size() )
505 bool writeNow = ( (
m_nWriteSize + len >= 1400) || (len > 512) );
507 if ( a && (a->size() + len < 128) )
513 memcpy( a->data()+i, data, len );
518 m_bufWrite.push_back(
new QByteArray(data, len));
534 const char *data, qulonglong len)
536 qlonglong nWritten = 0;
545 nWritten =
m_pSocket->writeBlock( data, len );
653 bool bTimeout =
false;
660 LOG(VB_HTTP, LOG_DEBUG,
"Can't Read Line... Waiting for more." );
665 if ( timer.
elapsed() >= msecs )
668 LOG(VB_HTTP, LOG_INFO,
"Exceeded Total Elapsed Wait Time." );
qulonglong BytesToWrite() const
qulonglong BytesAvailable()
A QElapsedTimer based timer to replace use of QTime as a timer.
qlonglong WriteBlock(const char *data, qulonglong len)
void SetReadBufferSize(qulonglong)
QHostAddress Address() const
qulonglong m_nMaxReadBufferSize
MSocketDevice * m_pSocket
bool Connect(const QHostAddress &addr, quint16 port)
BufferedSocketDevice(int nSocket)
deque< QByteArray * > m_bufWrite
bool ConsumeWriteBuf(qulonglong nbytes)
qlonglong WriteBlockDirect(const char *data, qulonglong len)
qulonglong WaitForMore(int msecs, bool *timeout=nullptr)
virtual ~BufferedSocketDevice()
QHostAddress PeerAddress() const
qlonglong ReadBlock(char *data, qulonglong maxlen)
qint64 m_nWriteIndex
write index
#define ENO
This can be appended to the LOG args with "+".
MSocketDevice * SocketDevice()
void SetSocketDevice(MSocketDevice *pSocket)
int elapsed(void) const
Returns milliseconds elapsed since last start() or restart()
#define LOG(_MASK_, _LEVEL_, _STRING_)
QHostAddress m_DestHostAddress
qint64 m_nWriteSize
write total buf size
void SetDestAddress(QHostAddress hostAddress, quint16 nPort)
qulonglong ReadBufferSize() const
void start(void)
starts measuring elapsed time.
bool m_bHandleSocketDelete