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对应的主机名。然后进行连接。