Logo Search packages:      
Sourcecode: verlihub version File versions

casyncconn.h

/***************************************************************************
                          casyncconn.h  -  description
                             -------------------
    begin                : Wed Jun 11 2003
    copyright            : (C) 2003 by Daniel Muller
    email                : dan at verliba dot cz
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef CASYNCCONN_H
#define CASYNCCONN_H

// buffer sizes, for more users, use larger buffers
#define MAX_MESS_SIZE 0x2FFFF           // max length of message that we accept
#define MAX_SEND_SIZE 0x0FFFFF         // max size of output buffer
#define MAX_SEND_FILL_SIZE 0x0AFFFF    // on this level, incoming daa is blocked
#define MAX_SEND_UNBLOCK_SIZE 0x07FFFF // under this level it's unblocked again


#include "cobj.h"
#include "ctime.h"
#include "cconnbase.h"

#ifndef WIN32
#include <netinet/in.h>
#endif

#include <string>
#include <list>

namespace nServer {

//class cSelector;
class cAsyncSocketServer;
class cAsyncConn;
class cProtocol;

class cConnFactory
{
public:
      cConnFactory(cProtocol *Protocol): mProtocol(Protocol) {};
      virtual ~cConnFactory(){};
      virtual cAsyncConn * CreateConn(tSocket sd=0);
      virtual void DeleteConn(cAsyncConn * &);
      cProtocol *mProtocol;
};

namespace nEnums
{
/** Type of socket connection
  */
typedef enum tConnType
{
      eCT_LISTEN,    //< listening tcp connection
      eCT_CLIENT,    //< client connection connected to me (server)
      eCT_CLIENTUDP, //< udp client
      eCT_SERVER,    //< i'm client connected to another sever (TCP)
      eCT_SERVERUDP  //< udp server
};

enum
{
      AC_LS_NO_LINE,
      AC_LS_PARTLY,
      AC_LS_LINE_DONE,
      AC_LS_ERROR
};
};

using namespace ::nServer::nEnums;
using namespace std;
using namespace nUtils;

class cMessageParser;

/**a network connection for asynchronous (rather non-blocking) server
  *@author Daniel Muller
  *@author Janos Horvath (udp support)
  */

00089 class cAsyncConn : public cConnBase, public cObj
{
public:
      cAsyncConn(int sd=0, cAsyncSocketServer *s=NULL, tConnType ct= eCT_CLIENT);
      cAsyncConn(const string & host, int port, bool udp=false);
      virtual ~cAsyncConn();
      /** close connection to peer */
      void Close();
      /** flush as much from output buffer as possible to the iochannel */
      void Flush();
      /** register the connection into given selector */
      //int Register(cSelector&, int);
      //void UnRegister(cSelector&);
      /** socket descriptor */
00103       tSocket mSockDesc;
private: // Private attributes
      /** string for line */
00106       string *mxLine;
      /** line status */
00108       int meLineStatus;
      /** msBuffer End Position */
00110       int mBufEnd;
      /** msBuffer read Position */
00112       int mBufReadPos;
      /** registered flags in selector */
00114       int mRegFlag;
      /** time to close the connection */
00116       cTime mCloseAfter;

      typedef list<cAsyncConn*> tConnList;
      typedef tConnList::iterator tCLIt;
      
public:
      tCLIt mIterator;
      /** indicate whether server should take care of this connection */
00124       bool ok;
      /** indicates, if data is allowed to insert into the buffer, you can set it as you wish */
00126       bool mWritable;
      /** flags externall to poll, but used by poller in a way */
00128       int mExtraPoll;
      /** read property for meLineStatus */
00130       int LineStatus(){return meLineStatus;}
      /** you can provide a string for reading a line by this function
            <precond>
            LineStatus() == AC_LS_NO_LINE
            </precond> */
      void SetLineToRead(string *,char , int max=-1);
      /** clears the line status */
      void ClearLine();
      /** return the line's pointer and keep it */
      string * GetLine();
      /** No descriptions */
      void CloseNice(int msec=0);
      /** reads a line from the msBuffer preaviously filled by ReadAll into private members */
      int ReadLineLocal();
public:
      /** external pointer to the server class */
00146       cAsyncSocketServer * mxServer;
      cConnFactory * mxMyFactory;
      cConnFactory * mxAcceptingFactory;
      cProtocol * mxProtocol;
      cMessageParser *mpMsgParser;
      static unsigned long sSocketCounter;      
protected: // Protected methods
      /** static buffer as intermediate place to stock data when reading parts of lines */
00154       static char *msBuffer;
      /** send buffer, what is not sent immediately, is sent later from this buffer */
00156       string mBufSend;
      /** line spearator */
00158       char mSeparator;
      /** byte count already in the line */
00160       int mLineSize;
      /** ip address */
00162       string mAddrHost;
      string mAddrIP;
      int mAddrPort;
      unsigned long mMaxBuffer;
      /** byte count limit for the line */
00167       unsigned mLineSizeMax;
      /** the ip address - binary format */
00169       unsigned long mIp;
      /** connestion type */
00171       tConnType mType;
      /** send *len bytes from the msBufSend */
      int SendAll(const char *buf, size_t &len);
      /** create a tcp or udp socket (default tcp) */
      tSocket CreateSock(bool udp=false);
      /** binds given socket to a port */
      int BindSocket(int sock, int port, const char *addr=NULL);
      /** listen on given socket */
      int ListenSock(int sock);
      /** set socket for non-blocking */
      tSocket NonBlockSock(int sock);
      /** return new socket */
      tSocket AcceptSock();
      /** function called before closing nicely */
      virtual int OnCloseNice(void);
      /** for udp */
00187       struct sockaddr_in mAddrIN;
      
public:
      virtual cMessageParser *CreateParser();
      virtual void DeleteParser(cMessageParser *);
      virtual string * FactoryString();
      
      /** retrun IP address */
00195       const string & AddrIP(){return mAddrIP;}
      const string & AddrHost(){return mAddrHost;}
      const int AddrPort(){return mAddrPort;}
      const unsigned long GetSockAddress() { return mAddrIN.sin_addr.s_addr; }
      
      /** reads all available data from the socket and stores it into a static member buffer */
      int ReadAll();
      /** tells if msBuffer has some more data available or not
      return value: true if not */
00204       int BufferEmpty(){ return mBufEnd == mBufReadPos;};
      /** No descriptions */
      int ListenOnPort(int port, const char *ia=NULL, bool udp=false);
      /** create new connection from the listening port */
      virtual cAsyncConn * Accept();
      /** Read property of tConnType mType. */
      virtual const tConnType& getType();
      /** this is called every period of time */
      virtual int OnTimer(cTime &now);
      /** this function is going to be executed periodicaly every N seconds by the  function of the same name in server */
      int OnTimerBase(cTime &now);
      /** immediately close the connection */
      void CloseNow();
      /** this is called when write buffer gets empty */
      virtual void OnFlushDone();
      /** write given data or store into buffer */
      int Write(const string &data, bool Flush);
      /** register the connection into given selector */
      //void UnRegister(cPoller & );
      /** register the connection into given selector */
      //int Register(cPoller & , int );
      /** No descriptions */
00226       tConnType GetType(){return mType;};
      /** connect to given host (ip) on port */
      int Connect(const string &, int);
      /** setup an udp destionation address */
      int SetupUDP(const string &, int);
      bool DNSLookup();
      bool DNSResolveReverse(const string &ip, string &host);
      virtual operator tSocket()const {return mSockDesc;}
      cTime mTimeLastIOAction;
      // for UDP messages to given host and port
      static int SendUDPMsg(const string &host, int port, const string &data);
      static unsigned long DNSResolveHost(const string &host);
      static const char *IPAsString(unsigned long addr);

      int SetSockOpt(int optname, const void *optval, int optlen);
      int GetSockOpt(int optname,       void *optval, int &optlen);
protected:
      virtual cConnFactory * GetAcceptingFactory();
};

};

#endif

Generated by  Doxygen 1.6.0   Back to index