Ticket #1605: socket1.diff

File socket1.diff, 5.2 KB (added by jwestfall@…, 18 years ago)

updated to fix bug

  • libs/libmyth/util.cpp

     
    125125 */
    126126bool WriteStringList(QSocketDevice *socket, QStringList &list)
    127127{
    128     if (!socket->isOpen() || socket->error())
     128    if (!socket->isOpen() || !socket->isValid() || socket->error())
    129129    {
    130130        VERBOSE(VB_IMPORTANT, "WriteStringList: Bad socket");
    131131        return false;
     
    185185            socket->close();
    186186            return false;
    187187        }
     188        else if (!socket->isValid())
     189        {
     190            VERBOSE(VB_IMPORTANT,"WriteStringList: Socket became invalid");
     191            return false;
     192        }
    188193        else
    189194        {
    190195            if (timer.elapsed() > 100000)
     
    207212{
    208213    list.clear();
    209214
    210     if (!socket->isOpen() || socket->error())
     215    if (!socket->isOpen() || !socket->isValid() || socket->error())
    211216    {
    212217        VERBOSE(VB_IMPORTANT, "ReadStringList: Bad socket");
    213218        return false;
     
    284289            socket->close();
    285290            return false;
    286291        }
     292        else if (!socket->isValid())
     293        {
     294            VERBOSE(VB_IMPORTANT, "ReadStringList: Socket went invalid");
     295            return false;
     296        }
    287297        else
    288298        {
    289299            elapsed = timer.elapsed();
     
    339349bool WriteBlock(QSocketDevice *socket, void *data, uint len)
    340350{
    341351   
    342     if (!socket->isOpen() || socket->error())
     352    if (!socket->isOpen() || !socket->isValid() || socket->error())
    343353    {
    344354        VERBOSE(VB_IMPORTANT, "WriteBlock: Bad socket");
    345355        return false;
     
    372382            socket->close();
    373383            return false;
    374384        }
     385        else if (!socket->isValid())
     386        {
     387            VERBOSE(VB_IMPORTANT,"WriteBlock: Socket went invalid");
     388            return false;
     389        }
    375390        else
    376391        {
    377392            if (++zerocnt > 200)
  • programs/mythbackend/mainserver.cpp

     
    880880        fileTransferList.push_back(ft);
    881881
    882882        retlist << QString::number(socket->socket());
     883        ft->UpRef();
    883884        encodeLongLong(retlist, ft->GetFileSize());
     885        ft->DownRef();
    884886    }
    885887
    886888    WriteStringList(socket, retlist);
     
    899901    }
    900902    else
    901903    {
    902         cerr << "Unable to write to client socket, as it's no longer there\n";
     904        VERBOSE(VB_IMPORTANT, "SendResponse: Unable to write to client socket,"
     905                " as it's no longer there");
    903906    }
    904907}
    905908
     
    29822985    QString command = slist[1];
    29832986
    29842987    QStringList retlist;
     2988    ft->UpRef();
    29852989
    29862990    if (command == "IS_OPEN")
    29872991    {
     
    30213025        retlist << "ok";
    30223026    }
    30233027
     3028    ft->DownRef();
    30243029    SendResponse(pbssock, retlist);
    30253030}
    30263031
     
    34303435        QSocket *sock = (*ft)->getSocket();
    34313436        if (sock == socket)
    34323437        {
    3433             socket->DownRef();
    3434             delete (*ft);
     3438            (*ft)->DownRef();
    34353439            fileTransferList.erase(ft);
    34363440            return;
    34373441        }
    34383442    }
  • programs/mythbackend/filetransfer.cpp

     
    1010#include "filetransfer.h"
    1111
    1212#include "RingBuffer.h"
     13#include "server.h"
    1314#include "libmyth/util.h"
    1415
    15 FileTransfer::FileTransfer(QString &filename, QSocket *remote)
     16FileTransfer::FileTransfer(QString &filename, RefSocket *remote)
    1617{
    1718    rbuffer = new RingBuffer(filename, false);
    1819    sock = remote;
    1920    readthreadlive = true;
    2021    ateof = false;
     22    refCount = 0;
    2123}
    2224
    2325FileTransfer::~FileTransfer()
    2426{
    25     Stop();
     27    sock->DownRef();
     28}
    2629
    27     if (rbuffer)
    28         delete rbuffer;
     30void FileTransfer::UpRef(void)
     31{
     32    refCount++;
     33}
    2934
    30     readthreadLock.unlock();
     35bool FileTransfer::DownRef(void)
     36{
     37    refCount--;
     38
     39    if (refCount < 0)
     40    {
     41        Stop();
     42
     43        if (rbuffer)
     44            delete rbuffer;
     45
     46        readthreadLock.unlock();
     47        delete(this);
     48        return true;
     49    }
     50    return false;
    3151}
    3252
    3353bool FileTransfer::isOpen(void)
  • programs/mythbackend/filetransfer.h

     
    1313#include <qmutex.h>
    1414
    1515class RingBuffer;
    16 class QSocket;
     16class RefSocket;
    1717
    1818class FileTransfer
    1919{
    2020  public:
    21     FileTransfer(QString &filename, QSocket *remote);
     21    FileTransfer(QString &filename, RefSocket *remote);
    2222   ~FileTransfer();
    2323
    24     QSocket *getSocket() { return sock; }
     24    RefSocket *getSocket() { return sock; }
    2525
    2626    bool isOpen(void);
    2727
    2828    void Stop(void);
    2929
     30    void UpRef(void);
     31    bool DownRef(void);
     32
    3033    void Pause(void);
    3134    void Unpause(void);
    3235    int RequestBlock(int size);
     
    4245    QMutex readthreadLock;
    4346
    4447    RingBuffer *rbuffer;
    45     QSocket *sock;
     48    RefSocket *sock;
    4649    bool ateof;
    47 
     50    int refCount;
     51   
    4852    vector<char> requestBuffer;
    4953};
    5054