| [3542] | 1 | #include "burawstream.h" | 
|---|
|  | 2 | #include "sopnamsp.h" | 
|---|
|  | 3 | #include "pexceptions.h" | 
|---|
|  | 4 |  | 
|---|
|  | 5 | /*! | 
|---|
|  | 6 | \class SOPHYA::RawInOutBuffStream | 
|---|
|  | 7 | \ingroup SysTools | 
|---|
|  | 8 | This class implements the interface defined by RawInOutStream | 
|---|
|  | 9 | over a socket (read and write operations) | 
|---|
|  | 10 | */ | 
|---|
|  | 11 |  | 
|---|
|  | 12 | #define SWRB_SIZE 1024 /* Taille des buffers I/O */ | 
|---|
|  | 13 |  | 
|---|
|  | 14 | /* --Methode-- */ | 
|---|
|  | 15 | RawInOutBuffStream::RawInOutBuffStream(const char * nom) | 
|---|
|  | 16 | : RawInOutStream() | 
|---|
|  | 17 | { | 
|---|
|  | 18 | _fip = fopen(nom, "w+b"); | 
|---|
|  | 19 | _rdbuff.buff = new char[SWRB_SIZE]; | 
|---|
|  | 20 | _rdbuff.sz = SWRB_SIZE; | 
|---|
|  | 21 | _rdbuff.cpos = SWRB_SIZE; | 
|---|
|  | 22 |  | 
|---|
|  | 23 | _wrbuff.buff = new char[SWRB_SIZE]; | 
|---|
|  | 24 | _wrbuff.sz = SWRB_SIZE; | 
|---|
|  | 25 | _wrbuff.cpos = 0; | 
|---|
|  | 26 | _totnwr = 0; | 
|---|
|  | 27 | _totnrd = 0; | 
|---|
|  | 28 | } | 
|---|
|  | 29 |  | 
|---|
|  | 30 | /* --Methode-- */ | 
|---|
|  | 31 | RawInOutBuffStream::~RawInOutBuffStream() | 
|---|
|  | 32 | { | 
|---|
|  | 33 | cout << " ---- DESTRUCTEUR ---- ~RawInOutBuffStream() " << endl; | 
|---|
|  | 34 | if (_wrbuff.cpos > 0) { | 
|---|
|  | 35 | for (size_t k= _wrbuff.cpos; k<_wrbuff.sz; k++) | 
|---|
|  | 36 | _wrbuff.buff[k] = '\0'; | 
|---|
|  | 37 | _wrbuff.cpos = _wrbuff.sz; | 
|---|
|  | 38 | SendBuffer(); | 
|---|
|  | 39 | } | 
|---|
|  | 40 | fclose(_fip); | 
|---|
|  | 41 | delete [] _rdbuff.buff; | 
|---|
|  | 42 | delete [] _wrbuff.buff; | 
|---|
|  | 43 | } | 
|---|
|  | 44 |  | 
|---|
|  | 45 | /* --Methode-- */ | 
|---|
|  | 46 | size_t RawInOutBuffStream::CopyToSendBuffer(const char* s, size_t n) | 
|---|
|  | 47 | { | 
|---|
|  | 48 |  | 
|---|
|  | 49 | size_t len = _wrbuff.sz-_wrbuff.cpos; | 
|---|
|  | 50 | if (len > n) len = n; | 
|---|
|  | 51 | if ( (_wrbuff.cpos == 0) && (len == _wrbuff.sz) ) { | 
|---|
|  | 52 | cout << "RawInOutBuffStream::CopyToSend/DBG-1 - n=" | 
|---|
|  | 53 | << n << " len=" << len << endl; | 
|---|
|  | 54 | Send(s, len); | 
|---|
|  | 55 | } | 
|---|
|  | 56 | else { | 
|---|
|  | 57 | cout << "RawInOutBuffStream::CopyToSend/DBG-2 - n=" | 
|---|
|  | 58 | << n << " len=" << len << " cpos=" << _wrbuff.cpos << endl; | 
|---|
|  | 59 | memcpy(_wrbuff.buff+_wrbuff.cpos, s, len); | 
|---|
|  | 60 | cout << " memcpy OK - cpos= " << _wrbuff.cpos << endl; | 
|---|
|  | 61 | _wrbuff.cpos += len; | 
|---|
|  | 62 | if (_wrbuff.cpos == _wrbuff.sz)  SendBuffer(); | 
|---|
|  | 63 | } | 
|---|
|  | 64 | return len; | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | /* --Methode-- */ | 
|---|
|  | 68 | size_t RawInOutBuffStream::CopyFromRecvBuffer(char* s, size_t n) | 
|---|
|  | 69 | { | 
|---|
|  | 70 | if ( (_rdbuff.cpos == _rdbuff.sz) && ( n >= _rdbuff.sz)) { | 
|---|
|  | 71 | size_t len = _rdbuff.sz; | 
|---|
|  | 72 | cout << "RawInOutBuffStream::CopyFromRecv/DBG-1 - n=" << n | 
|---|
|  | 73 | << " len=" << len << endl; | 
|---|
|  | 74 | Receive(s, len); | 
|---|
|  | 75 | return len; | 
|---|
|  | 76 | } | 
|---|
|  | 77 | else { | 
|---|
|  | 78 | cout << "RawInOutBuffStream::CopyFromRecv/DBG-2 - n=" << n << endl; | 
|---|
|  | 79 | if (_rdbuff.cpos == _rdbuff.sz)  ReceiveBuffer(); | 
|---|
|  | 80 | size_t len = _rdbuff.sz-_rdbuff.cpos; | 
|---|
|  | 81 | if (len > n) len = n; | 
|---|
|  | 82 | memcpy(s, _rdbuff.buff+_rdbuff.cpos, len); | 
|---|
|  | 83 | _rdbuff.cpos += len; | 
|---|
|  | 84 | return len; | 
|---|
|  | 85 | } | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | /* --Methode-- */ | 
|---|
|  | 89 | void RawInOutBuffStream::SendBuffer() | 
|---|
|  | 90 | { | 
|---|
|  | 91 | cout << "RawInOutBuffStream::SendBuffer/DBG - cpos=" | 
|---|
|  | 92 | <<  _wrbuff.cpos << " sz=" << _wrbuff.sz << endl; | 
|---|
|  | 93 | //  if (_wrbuff.cpos != _wrbuff.sz)  return; | 
|---|
|  | 94 | Send(_wrbuff.buff, _wrbuff.sz); | 
|---|
|  | 95 | _wrbuff.cpos = 0; | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
|  | 98 | /* --Methode-- */ | 
|---|
|  | 99 | void RawInOutBuffStream::ReceiveBuffer() | 
|---|
|  | 100 | { | 
|---|
|  | 101 | //  if (_rdbuff.cpos != _rdbuff.sz)  return; | 
|---|
|  | 102 | cout << "RawInOutBuffStream::ReceiveBuffer/DBG - cpos=" | 
|---|
|  | 103 | <<  _rdbuff.cpos << " sz=" << _rdbuff.sz << endl; | 
|---|
|  | 104 | Receive(_rdbuff.buff, _rdbuff.sz); | 
|---|
|  | 105 | _rdbuff.cpos = 0; | 
|---|
|  | 106 |  | 
|---|
|  | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | /* --Methode-- */ | 
|---|
|  | 110 | size_t RawInOutBuffStream::Send(const char* s, size_t n) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | size_t  nst = 0; | 
|---|
|  | 113 | while (nst < n) { | 
|---|
|  | 114 | size_t ns = fwrite(s, 1, n, _fip);; | 
|---|
|  | 115 | if (ns < 1)  break; | 
|---|
|  | 116 | nst += ns; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | if ( nst < n) | 
|---|
|  | 119 | throw IOExc("RawInOutBuffStream::Send()/write() Error nwrite < n"); | 
|---|
|  | 120 | return nst; | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /* --Methode-- */ | 
|---|
|  | 124 | size_t RawInOutBuffStream::Receive(char* s, size_t n) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | size_t  nst = 0; | 
|---|
|  | 127 | int ntry = 0; | 
|---|
|  | 128 | while (nst < n) { | 
|---|
|  | 129 | size_t ns = fread(s, 1, n, _fip); | 
|---|
|  | 130 | ntry++; | 
|---|
|  | 131 | if (ns < 1)  break; | 
|---|
|  | 132 | nst += ns; | 
|---|
|  | 133 | } | 
|---|
|  | 134 | if ( nst < n) { | 
|---|
|  | 135 | cout << "  RawInOutBuffStream::Receive() / Pb ! ntry=" << ntry | 
|---|
|  | 136 | << " nst=" << nst << " n=" << n << endl; | 
|---|
|  | 137 | throw IOExc("RawInOutBuffStream::Receive/read() Error nread < n"); | 
|---|
|  | 138 | } | 
|---|
|  | 139 | return nst; | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
|  | 142 | /* --Methode-- */ | 
|---|
|  | 143 | int_8 RawInOutBuffStream::tellg() | 
|---|
|  | 144 | { | 
|---|
|  | 145 | return _totnrd; | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|
|  | 148 | /* --Methode-- */ | 
|---|
|  | 149 | RawInOutStream& RawInOutBuffStream::read(char* s, uint_8 n) | 
|---|
|  | 150 | { | 
|---|
|  | 151 | cout << "RawInOutBuffStream::read()/DBG - n=" << n << endl; | 
|---|
|  | 152 | size_t  nst = 0; | 
|---|
|  | 153 | while (nst < n) { | 
|---|
|  | 154 | size_t ns = CopyFromRecvBuffer(s+nst, n-nst); | 
|---|
|  | 155 | if (ns < 1)  break; | 
|---|
|  | 156 | nst += ns; | 
|---|
|  | 157 | } | 
|---|
|  | 158 | if ( nst < n) | 
|---|
|  | 159 | throw IOExc("RawInOutBuffStream::read() Error nread < n"); | 
|---|
|  | 160 | _totnrd += n; | 
|---|
|  | 161 | return *this; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | /* --Methode-- */ | 
|---|
|  | 165 | int_8 RawInOutBuffStream::tellp() | 
|---|
|  | 166 | { | 
|---|
|  | 167 | return _totnwr; | 
|---|
|  | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /* --Methode-- */ | 
|---|
|  | 171 | RawInOutStream& RawInOutBuffStream::write(const char* s, uint_8 n) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | cout << "RawInOutBuffStream::write()/DBG - n=" << n << endl; | 
|---|
|  | 174 | size_t  nst = 0; | 
|---|
|  | 175 | while (nst < n) { | 
|---|
|  | 176 | size_t ns = CopyToSendBuffer(s+nst, n-nst); | 
|---|
|  | 177 | if (ns < 1)  break; | 
|---|
|  | 178 | nst += ns; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | cout << "RawInOutBuffStream::write()/DBG ---> nst=" << nst << endl; | 
|---|
|  | 181 |  | 
|---|
|  | 182 | if ( nst < n) | 
|---|
|  | 183 | throw IOExc("RawInOutBuffStream::write() Error nwrite < n"); | 
|---|
|  | 184 | _totnwr += n; | 
|---|
|  | 185 | return *this; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 |  | 
|---|