gloox的TCP连接类

1、  gloox的TCP连接类


1)、其中ConnectionBase是个纯虚基类,大部分接口都是纯虚函数。常见的connect()、recv()、send()、disconnect()。它的私有数据有

/** Ahandler for incoming data and connect/disconnect events. */

ConnectionDataHandler* m_handler;

/** Holdsthe current connection state. */

ConnectionState m_state;

/** Holdsthe server's name/address. */

std::string m_server;

/** Holdsthe port to connect to. */

int m_port;

2)、ConnectionTCPBase继承于ConnectionBase。实现了里面大量的纯虚函数,其中send和receive还采用了加锁(互斥量)处理。但connect()、recv()、newInstance()纯虚函数没有实现。因此也不能直接实例化它。它新增的私有数据有:

const LogSink& m_logInstance; //日志类

  char* m_buf;

     int m_socket;

     long int m_totalBytesIn;

     long int m_totalBytesOut;

     const int m_bufsize;

     bool m_cancel;


     util::Mutex m_sendMutex 

      util::Mutex m_recvMutex;

//在window下为临界区,在pthreads,配置mutex为可递归的

头文件预编译

#if defined( _WIN32 ) && !defined(__SYMBIAN32__ )

# include <windows.h>

#endif

 

#ifdef _WIN32_WCE

# include <winbase.h>

#endif

 

#ifdef HAVE_PTHREAD

# include <pthread.h>

#endif

 //互斥量或临界区的声明


#if defined( _WIN32 ) && !defined(__SYMBIAN32__ )

       CRITICAL_SECTION m_cs;

#elif defined( HAVE_PTHREAD )

       pthread_mutex_t m_mutex;

#endif

//互斥量或临界区的初始化

#if defined( _WIN32 ) && !defined(__SYMBIAN32__ )

     // NOTE: Critical sections by nature allow "recursive"

     //  (the same thread can get itagain, and just bump the ref count).

     InitializeCriticalSection( &m_cs );

#elif defined( HAVE_PTHREAD )

     // For pthreads, configured the mutex to be recursive

     //  (the same thread can get itagain, and just bump the ref count).

     pthread_mutexattr_t mutexAttribute;

     pthread_mutexattr_init( &mutexAttribute );

     pthread_mutexattr_settype( &mutexAttribute, PTHREAD_MUTEX_RECURSIVE);

     pthread_mutex_init( &m_mutex, &mutexAttribute );

     pthread_mutexattr_destroy( &mutexAttribute );

#endif

3)ConectionTCPClient继承于ConnectionTCPBase。它实现了更上层ConnectionBase声明而ConectionTCPBase未给出默认实现的纯虚接口connect()、recv()、newInstance()。

Connect函数通过DNS::connect()连接m_server(m_server在此是服务器名称,不一定是运行服务器的主机名)和m_port。在DNS::connect()中windows下调用DnsQuery_UTF8() ,DNS查询API查找m_server对应的主机名。然后进行连接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值