Ignore:
Timestamp:
05/09/09 13:48:12 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc

    r9432 r9439  
    4343MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx)
    4444{
    45     gLog << inf2 << "- Open send socket to " << addr << ":" << tx << endl;
     45    // was "inf2" but nobody knows what the prg is doing if it times out
     46    gLog << all << "- Open send socket to " << addr << ":" << tx << endl;
     47
    4648    fTxSocket = new TSocket(addr, tx);
     49    fTxSocket->SetOption(kNoBlock, 1);
     50
     51    MTcpIpO::RunThread();
    4752}
    4853
    4954MTcpIpO::~MTcpIpO()
    5055{
     56    CancelThread();
     57
    5158    // Now delete all TCP/IP objects
    5259    delete fTxSocket;
     
    5562MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx)
    5663{
    57     RunThread();
     64    MTcpIpI::RunThread();
    5865}
    5966
     
    7481}
    7582
     83/*
    7684TString MTcpIpO::GetSocketAddress() const
    7785{
     86    TLockGuard guard(const_cast<TMutex*>(&fMutex));
    7887    return GetSocketAddress(*fTxSocket);
    7988}
    80 
     89*/
    8190bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len)
    8291{
    8392    if (!tx.IsValid())
    84     {
    85         //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;
    8693        return false;
    87     }
    8894
    8995#ifdef DEBUG
     
    9197#endif
    9298
    93     const Int_t l = tx.SendRaw(msg, len);
     99    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
     100
     101    // Frame not sent, EWOULDBLOCK
     102    if (l==-4)
     103    {
     104        gLog << err << "ERROR - Sending to " << GetSocketAddress(tx) << " would block." << endl;
     105/*--->*/        tx.Close(); // ?????
     106        return false;
     107    }
     108
    94109    if (l<0)
    95110    {
     
    100115    if (l!=len)
    101116    {
    102         gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
     117        gLog << err << "ERROR - Could only sent " << l << " out of "  << len << " bytes to " << GetSocketAddress(tx) << endl;
    103118        return false;
    104119    }
     
    109124bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len)
    110125{
    111     //    R__LOCKGUARD2(myMutex);
    112126#ifdef DEBUG
    113127    cout << "Connecting to " << addr << ":" << port << endl;
     
    117131    TSocket tx(addr, port);
    118132    return SendFrame(tx, msg, len);
    119 /*
    120     if (!tx.IsValid())
    121     {
    122         gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl;
     133}
     134
     135bool MTcpIpO::Send(const char *msg, Int_t len)
     136{
     137    const Int_t mtx = fMutex.TryLock();
     138    if (mtx==13)
     139        gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl;
     140
     141    // If Mutex cannot be locked, i.e. we are currently reopening
     142    // the send socket we cannot wait, because we would block the
     143    // executing threrad.
     144    if (mtx)
    123145        return false;
    124     }
    125 
    126     gLog << dbg << "Sending to " << addr << ":" << port << endl;
    127 
    128     const Int_t l = tx.SendRaw(msg, len, kDontBlock);
    129     if (l<0)
    130     {
    131         gLog << err << "ERROR - Sending TCP/IP frame to " << addr << ":" << port << endl;
    132         return false;
    133     }
    134 
    135     if (l!=len)
    136     {
    137         gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << addr << ":" << port << endl;
    138         return false;
    139     }
    140 
    141 #ifdef DEBUG
    142     cout << "Tx: " << msg << flush;
    143 #endif
    144 
    145     return true;
    146     */
    147 }
    148 
    149 bool MTcpIpO::Send(const char *msg, Int_t len)
    150 {
    151     if (!fTxSocket->IsValid())
    152     {
    153         const TInetAddress &a = fTxSocket->GetInetAddress();
    154         if (!a.IsValid())
    155             return false;
     146
     147    const bool rc = SendFrame(*fTxSocket, msg, len);
     148
     149    fMutex.UnLock();
     150
     151    return rc;
     152}
     153
     154Int_t MTcpIpO::Thread()
     155{
     156    const TInetAddress &a = fTxSocket->GetInetAddress();
     157    if (!a.IsValid())
     158    {
     159        gLog << err << "ERROR: Send socket address invalid." << endl;
     160        return 0;
     161    }
     162
     163    Int_t wait = 1000; /// Wait a ms
     164
     165    while (!IsThreadCanceled())
     166    {
     167        if (fTxSocket->IsValid())
     168        {
     169            MThread::Sleep(1000); // Wait a ms
     170            wait = 1000;
     171            continue;
     172        }
     173
     174
    156175#ifdef DEBUG
    157176        cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
    158177#endif
     178
     179        fMutex.Lock();
     180
    159181        delete fTxSocket;
     182
    160183        fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
    161     }
    162 
    163     return SendFrame(*fTxSocket, msg, len);
     184        fTxSocket->SetOption(kNoBlock, 1);
     185
     186        fMutex.UnLock();
     187
     188        if (fTxSocket->IsValid())
     189            continue;
     190
     191        MThread::Sleep(wait); // Wait a ms
     192        if (wait<30000000) // 30s
     193            wait *= 2;
     194    }
     195
     196    return 1;
    164197}
    165198
     
    174207    TString str;
    175208
    176     while (!IsThreadCanceled())
     209    while (!MTcpIpI::IsThreadCanceled())
    177210    {
    178211        char c;
Note: See TracChangeset for help on using the changeset viewer.