| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@uni-sw.gwdg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2001
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | //
|
|---|
| 28 | // MReadSocket
|
|---|
| 29 | //
|
|---|
| 30 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 31 | #include "MReadSocket.h"
|
|---|
| 32 |
|
|---|
| 33 | #include <unistd.h> // usleep
|
|---|
| 34 |
|
|---|
| 35 | #include <TMath.h> // TMath::Min
|
|---|
| 36 | #include <TTime.h> // TTime
|
|---|
| 37 | #include <TDatime.h> // TDatime
|
|---|
| 38 | #include <TSystem.h> // gSystem
|
|---|
| 39 | #include <TSocket.h> // TSocket
|
|---|
| 40 | #include <TServerSocket.h> // TServerSocket
|
|---|
| 41 |
|
|---|
| 42 | ClassImp(MReadSocket);
|
|---|
| 43 |
|
|---|
| 44 | using namespace std;
|
|---|
| 45 |
|
|---|
| 46 | MReadSocket::MReadSocket(int port, int mtu) : istream(this), fMtu(mtu), fTimeout(2500), fServSock(NULL), fRxSocket(NULL)
|
|---|
| 47 | {
|
|---|
| 48 | fBuffer = new char[mtu];
|
|---|
| 49 |
|
|---|
| 50 | setg(fBuffer, fBuffer, fBuffer+1);
|
|---|
| 51 |
|
|---|
| 52 | cout << "Starting server socket on port 7000..." << endl;
|
|---|
| 53 |
|
|---|
| 54 | while (1)
|
|---|
| 55 | {
|
|---|
| 56 | fServSock=new TServerSocket(port, kTRUE);
|
|---|
| 57 | if (fServSock->IsValid())
|
|---|
| 58 | break;
|
|---|
| 59 |
|
|---|
| 60 | cout << "ServerSocket not valid: ";
|
|---|
| 61 | switch (fServSock->GetErrorCode())
|
|---|
| 62 | {
|
|---|
| 63 | case 0: cout << "No error." << endl; break;
|
|---|
| 64 | case -1: cout << "low level socket() call failed." << endl; break;
|
|---|
| 65 | case -2: cout << "low level bind() call failed." << endl; break;
|
|---|
| 66 | case -3: cout << "low level listen() call failed." << endl; break;
|
|---|
| 67 | default: cout << "Unknown." << endl; break;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | delete fServSock;
|
|---|
| 71 | fServSock=NULL;
|
|---|
| 72 | break;
|
|---|
| 73 | }
|
|---|
| 74 | if (!fServSock)
|
|---|
| 75 | {
|
|---|
| 76 | cout << "MReadSocket: TServerSocket - Connection timed out." << endl;
|
|---|
| 77 | setstate(ios::failbit);
|
|---|
| 78 | return;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | fServSock->SetOption(kNoBlock, 1);
|
|---|
| 82 |
|
|---|
| 83 | while (1)
|
|---|
| 84 | {
|
|---|
| 85 | const TTime timeout = gSystem->Now() + TTime(5000);
|
|---|
| 86 |
|
|---|
| 87 | TDatime now;
|
|---|
| 88 | cout << now.AsString() << ": Waiting for conntection on port 7000..." << endl;
|
|---|
| 89 | fRxSocket = NULL;
|
|---|
| 90 | while ((Long_t)fRxSocket<=0 && gSystem->Now()<timeout)
|
|---|
| 91 | {
|
|---|
| 92 | fRxSocket = fServSock->Accept();
|
|---|
| 93 | if (fRxSocket==0)
|
|---|
| 94 | cout << "Error: TServerSock::Accept" << endl;
|
|---|
| 95 | usleep(1);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if ((Long_t)fRxSocket<=0)
|
|---|
| 99 | continue;
|
|---|
| 100 |
|
|---|
| 101 | if (fRxSocket->IsValid())
|
|---|
| 102 | break;
|
|---|
| 103 |
|
|---|
| 104 | cout << "TSocket: Connection not valid..." << endl;
|
|---|
| 105 | delete fRxSocket;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if ((Long_t)fRxSocket<=0)
|
|---|
| 109 | {
|
|---|
| 110 | cout << "MReadSocket: TServerSocket::Accept - Connection timed out." << endl;
|
|---|
| 111 | fRxSocket=NULL;
|
|---|
| 112 | setstate(ios::failbit);
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | cout << "Connection established..." << endl;
|
|---|
| 117 |
|
|---|
| 118 | fRxSocket->SetOption(kNoBlock, 1);
|
|---|
| 119 |
|
|---|
| 120 | underflow();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | // --------------------------------------------------------------------------
|
|---|
| 124 | //
|
|---|
| 125 | // Destructor, destroying the gui mutex.
|
|---|
| 126 | //
|
|---|
| 127 | MReadSocket::~MReadSocket()
|
|---|
| 128 | {
|
|---|
| 129 | if (fRxSocket)
|
|---|
| 130 | delete fRxSocket;
|
|---|
| 131 | if (fServSock)
|
|---|
| 132 | delete fServSock;
|
|---|
| 133 |
|
|---|
| 134 | delete fBuffer;
|
|---|
| 135 |
|
|---|
| 136 | cout << "Connection on Port 7000 closed." << endl;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // --------------------------------------------------------------------------
|
|---|
| 140 | //
|
|---|
| 141 | // This is called to flush the buffer of the streaming devices
|
|---|
| 142 | //
|
|---|
| 143 | int MReadSocket::sync()
|
|---|
| 144 | {
|
|---|
| 145 | cout << "sync" << endl;
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | int MReadSocket::underflow()
|
|---|
| 150 | {
|
|---|
| 151 | if (fail())
|
|---|
| 152 | {
|
|---|
| 153 | setg(fBuffer, fBuffer, fBuffer+fMtu);
|
|---|
| 154 | return 0;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | //
|
|---|
| 158 | // This simple trick should do its job, because the
|
|---|
| 159 | // TCP/IP stream is buffered already
|
|---|
| 160 | //
|
|---|
| 161 | const TTime timeout = fTimeout+gSystem->Now();
|
|---|
| 162 |
|
|---|
| 163 | Int_t len=-1;
|
|---|
| 164 | while (len<0 && gSystem->Now()<timeout)
|
|---|
| 165 | {
|
|---|
| 166 | Int_t l;
|
|---|
| 167 | fRxSocket->GetOption(kBytesToRead, l);
|
|---|
| 168 | if (l==0)
|
|---|
| 169 | {
|
|---|
| 170 | gSystem->Sleep(1);
|
|---|
| 171 | continue;
|
|---|
| 172 | }
|
|---|
| 173 | len = fRxSocket->RecvRaw(fBuffer, TMath::Min(fMtu, l));
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (len<0)
|
|---|
| 177 | {
|
|---|
| 178 | cout << "MReadSocket: TSocket::RecvRaw - Connection timed out." << endl;
|
|---|
| 179 | setstate(ios::failbit);
|
|---|
| 180 | memset(fBuffer, 0, fMtu);
|
|---|
| 181 | len = fMtu;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | setg(fBuffer, fBuffer, fBuffer+len);
|
|---|
| 185 | return 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|