| 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 | /*
|
|---|
| 34 | #ifdef _REENTRANT
|
|---|
| 35 | #include <pthread.h>
|
|---|
| 36 | #endif
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <unistd.h> // usleep
|
|---|
| 40 |
|
|---|
| 41 | #include <TMath.h> // TMath::Min
|
|---|
| 42 | #include <TTime.h> // TTime
|
|---|
| 43 | #include <TDatime.h> // TDatime
|
|---|
| 44 | #include <TSystem.h> // gSystem
|
|---|
| 45 | #include <TSocket.h> // TSocket
|
|---|
| 46 | #include <TServerSocket.h> // TServerSocket
|
|---|
| 47 |
|
|---|
| 48 | ClassImp(MReadSocket);
|
|---|
| 49 |
|
|---|
| 50 | using namespace std;
|
|---|
| 51 |
|
|---|
| 52 | MReadSocket::MReadSocket(int port, int mtu) : istream(this), fMtu(mtu), fTimeout(2500), fServSock(NULL), fRxSocket(NULL)
|
|---|
| 53 | {
|
|---|
| 54 | fBuffer = new char[mtu];
|
|---|
| 55 |
|
|---|
| 56 | setg(fBuffer, fBuffer, fBuffer+1);
|
|---|
| 57 |
|
|---|
| 58 | cout << "Starting server socket on port 7000..." << endl;
|
|---|
| 59 |
|
|---|
| 60 | while (1)
|
|---|
| 61 | {
|
|---|
| 62 | fServSock=new TServerSocket(port, kTRUE);
|
|---|
| 63 | if (fServSock->IsValid())
|
|---|
| 64 | break;
|
|---|
| 65 |
|
|---|
| 66 | cout << "ServerSocket not valid: ";
|
|---|
| 67 | switch (fServSock->GetErrorCode())
|
|---|
| 68 | {
|
|---|
| 69 | case 0: cout << "No error." << endl; break;
|
|---|
| 70 | case -1: cout << "low level socket() call failed." << endl; break;
|
|---|
| 71 | case -2: cout << "low level bind() call failed." << endl; break;
|
|---|
| 72 | case -3: cout << "low level listen() call failed." << endl; break;
|
|---|
| 73 | default: cout << "Unknown." << endl; break;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | delete fServSock;
|
|---|
| 77 | fServSock=NULL;
|
|---|
| 78 | break;
|
|---|
| 79 | }
|
|---|
| 80 | if (!fServSock)
|
|---|
| 81 | {
|
|---|
| 82 | cout << "MReadSocket: TServerSocket - Connection timed out." << endl;
|
|---|
| 83 | setstate(ios::failbit);
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | fServSock->SetOption(kNoBlock, 1);
|
|---|
| 88 |
|
|---|
| 89 | while (1)
|
|---|
| 90 | {
|
|---|
| 91 | const TTime timeout = gSystem->Now() + TTime(5000);
|
|---|
| 92 |
|
|---|
| 93 | TDatime now;
|
|---|
| 94 | cout << now.AsString() << ": Waiting for conntection on port 7000..." << endl;
|
|---|
| 95 | fRxSocket = NULL;
|
|---|
| 96 | while ((Long_t)fRxSocket<=0 && gSystem->Now()<timeout)
|
|---|
| 97 | {
|
|---|
| 98 | fRxSocket = fServSock->Accept();
|
|---|
| 99 | if (fRxSocket==0)
|
|---|
| 100 | cout << "Error: TServerSock::Accept" << endl;
|
|---|
| 101 | usleep(1);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if ((Long_t)fRxSocket<=0)
|
|---|
| 105 | continue;
|
|---|
| 106 |
|
|---|
| 107 | if (fRxSocket->IsValid())
|
|---|
| 108 | break;
|
|---|
| 109 |
|
|---|
| 110 | cout << "TSocket: Connection not valid..." << endl;
|
|---|
| 111 | delete fRxSocket;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if ((Long_t)fRxSocket<=0)
|
|---|
| 115 | {
|
|---|
| 116 | cout << "MReadSocket: TServerSocket::Accept - Connection timed out." << endl;
|
|---|
| 117 | fRxSocket=NULL;
|
|---|
| 118 | setstate(ios::failbit);
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | cout << "Connection established..." << endl;
|
|---|
| 123 |
|
|---|
| 124 | fRxSocket->SetOption(kNoBlock, 1);
|
|---|
| 125 |
|
|---|
| 126 | underflow();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | // --------------------------------------------------------------------------
|
|---|
| 130 | //
|
|---|
| 131 | // Destructor, destroying the gui mutex.
|
|---|
| 132 | //
|
|---|
| 133 | MReadSocket::~MReadSocket()
|
|---|
| 134 | {
|
|---|
| 135 | if (fRxSocket)
|
|---|
| 136 | delete fRxSocket;
|
|---|
| 137 | if (fServSock)
|
|---|
| 138 | delete fServSock;
|
|---|
| 139 |
|
|---|
| 140 | delete fBuffer;
|
|---|
| 141 |
|
|---|
| 142 | cout << "Connection on Port 7000 closed." << endl;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | // --------------------------------------------------------------------------
|
|---|
| 146 | //
|
|---|
| 147 | // This is called to flush the buffer of the streaming devices
|
|---|
| 148 | //
|
|---|
| 149 | int MReadSocket::sync()
|
|---|
| 150 | {
|
|---|
| 151 | cout << "sync" << endl;
|
|---|
| 152 | return 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | int MReadSocket::underflow()
|
|---|
| 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 l, len=-1;
|
|---|
| 164 | while (len<0 && gSystem->Now()<timeout)
|
|---|
| 165 | {
|
|---|
| 166 | fRxSocket->GetOption(kBytesToRead, l);
|
|---|
| 167 | if (l==0)
|
|---|
| 168 | {
|
|---|
| 169 | usleep(1);
|
|---|
| 170 | continue;
|
|---|
| 171 | }
|
|---|
| 172 | len = fRxSocket->RecvRaw(fBuffer, TMath::Min(fMtu, l));
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | if (len<0)
|
|---|
| 176 | {
|
|---|
| 177 | cout << "MReadSocket: TSocket::RecvRaw - Connection timed out." << endl;
|
|---|
| 178 | setstate(ios::failbit);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | setg(fBuffer, fBuffer, fBuffer+len);
|
|---|
| 182 | return 0;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|