qsocketsession.h

00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Gav Wood                                        *
00003  *   gav@cs.york.ac.uk                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  ***************************************************************************/
00010 #ifndef __QSOCKETSESSION_H
00011 #define __QSOCKETSESSION_H
00012 
00013 // TODO: may need compatibility fixes.
00014 #include <byteswap.h>
00015 
00016 #include <stdint.h>
00017 
00018 #include <qsocketdevice.h>
00019 #include <qcstring.h>
00020 
00039 class QSocketSession
00040 {
00041  bool theIsMaster, theSameByteOrder, theClosed;
00042  QSocketDevice *theSD;
00043 
00044  void findByteOrder();
00045  
00046 public:
00055  
00061  const bool isOpen()
00062  {
00063   if(!theSD->isValid())
00064    theClosed = true;
00065   return !theClosed;
00066  }
00067  
00071  void close();
00072  
00082  void ack(const bool sign = true) { sendByte(sign ? 1 : 2); }
00083 
00091  void sendByte(const uchar c)
00092  {
00093   if(theSD->writeBlock((char *)&c, 1) == 1)
00094    return;
00095   qWarning("*** ERROR: Socket send error. Unable to send a byte.");
00096   close();
00097  }
00098  
00108  void sendChunk(const uchar *buffer, const uint size);
00109  
00121  const uchar receiveByte()
00122  {
00123   uchar c;
00124   if(theSD->readBlock((char *)&c, 1) == 1)
00125    return c;
00126   qWarning("***  ERROR: Socket receive error. Unable to read a byte.");
00127   close();
00128   return 0;
00129  }
00130  
00144  void receiveChunk(uchar *buffer, const uint size);
00145  
00162  const bool receiveChunk(uchar *buffer, const uint size, const uint timeOut);
00163  
00178  const bool waitForAck(bool *ackType = 0);
00179 
00195  const bool waitForAck(const uint timeOut, bool *ackType = 0);
00196  
00198  
00211 
00224  void handshake(const bool opposite);
00225 
00237  void handshake();
00238 
00244  const bool sameByteOrder() const { return theSameByteOrder; }
00245  
00254  void safeSendWord(const float i) { sendChunk((const uchar *)&i, 4); }
00255  void safeSendWord(const int32_t i) { sendChunk((const uchar *)&i, 4); }
00256  void safeSendWord(const uint32_t i) { sendChunk((const uchar *)&i, 4); }
00257  
00267  void safeSendWordArray(const float *i, const uint size) { sendChunk((const uchar *)i, 4 * size); }
00268  void safeSendWordArray(const int32_t *i, const uint size) { sendChunk((const uchar *)i, 4 * size); }
00269  void safeSendWordArray(const uint32_t *i, const uint size) { sendChunk((const uchar *)i, 4 * size); }
00270  
00284  template<typename T> const T safeReceiveWord();
00285  
00300  template<typename T> void safeReceiveWordArray(T *t, const uint32_t size);
00301 
00307  void sendString(const QCString &s);
00308  
00317  const QCString receiveString(); 
00319 
00327  QSocketSession(QSocketDevice *sd);
00328  
00332  ~QSocketSession();
00333 };
00334 
00335 template<> const float QSocketSession::safeReceiveWord();
00336 template<> const int32_t QSocketSession::safeReceiveWord();
00337 template<> const uint32_t QSocketSession::safeReceiveWord();
00338 template<> void QSocketSession::safeReceiveWordArray(float *t, const uint32_t size);
00339 template<> void QSocketSession::safeReceiveWordArray(int32_t *t, const uint32_t size);
00340 template<> void QSocketSession::safeReceiveWordArray(uint32_t *t, const uint32_t size);
00341 
00342 #endif

Generated on Fri Nov 10 21:58:26 2006 for Exscalibar by  doxygen 1.5.1