/************************************************************************/ /* Copyright (C), 2016-2020, [IT], 保留所有权利; /* 模 块 名:PerSocketContext; /* 描 述:重叠IO缓冲类,每个客户端连接对象类; /* /* 版 本:[V]; /* 作 者:[IT]; /* 日 期:[2/23/2016]; /* /* /* 注 意:; /* /* 修改记录:[IT]; /* 修改日期:; /* 修改版本:; /* 修改内容:; /************************************************************************/ #ifndef __PER_SOCKET_CONTEXT_20160303__ #define __PER_SOCKET_CONTEXT_20160303__ #pragma once #include #include #include "SockAddrIn.h" class PerSocketContext { public: PerSocketContext(SOCKET sock):_socket(sock){ npendingSize = 0; ncurSize = 0; pendingbuf = NULL; memset(&_Overlapped, 0, sizeof(_Overlapped)); } explicit PerSocketContext(SOCKET sock, size_t size): _socket(sock){ npendingSize = 0; ncurSize= 0; pendingbuf = NULL; memset(&_Overlapped, 0, sizeof(_Overlapped)); _data.resize( size ); } PerSocketContext(const PerSocketContext& sbuf){ Copy(sbuf); } PerSocketContext& operator=(const PerSocketContext& sbuf){ return Copy(sbuf); } ~PerSocketContext(){ Free(); } bool IsValid() const { return (_socket != INVALID_SOCKET); } void ReAlloc( size_t count) { _data.resize(count); } void Free() { _data.clear(); npendingSize = 0; ncurSize = 0; if ( pendingbuf ) { delete []pendingbuf; pendingbuf = NULL; } } size_t BufferSize() const { return _data.size(); } PerSocketContext& Copy(const PerSocketContext& sbuf) { if ( this != &sbuf ) { _socket = sbuf._socket; _sockAddr = sbuf._sockAddr; if ( !sbuf._data.empty() ) { _data.resize( sbuf._data.size() ); memcpy(&_data[0], &(sbuf._data[0]), _data.size()); } /*if ( ncurSize && pendingbuf ) { delete []pendingbuf; pendingbuf = NULL; }*/ ncurSize = sbuf.ncurSize; npendingSize = sbuf.npendingSize; pendingbuf = new unsigned char[npendingSize]; memset(pendingbuf, 0, npendingSize); memcpy(pendingbuf, sbuf.pendingbuf, ncurSize); } return *this; } void SetPendingCurPack(const int& nCurSize) { if ( pendingbuf ) { delete []pendingbuf; pendingbuf = NULL; } ncurSize = nCurSize; pendingbuf = new unsigned char[nCurSize]; memset(pendingbuf, 0, nCurSize); } void SetPendingPack(const int& nSize) { if ( pendingbuf ) { delete []pendingbuf; pendingbuf = NULL; } npendingSize = nSize; pendingbuf = new unsigned char[nSize]; memset(pendingbuf, 0, nSize); } void ReSetPengingPack(const int& nSize) { if ( pendingbuf ) { unsigned char *ptemp = new unsigned char[ncurSize]; memcpy(ptemp, pendingbuf, ncurSize); delete []pendingbuf; pendingbuf = NULL; if ( npendingSize ) { pendingbuf = new unsigned char[npendingSize]; memset(pendingbuf, 0, npendingSize); memcpy(pendingbuf, ptemp, ncurSize); } else { pendingbuf = new unsigned char[nSize]; memset(pendingbuf, 0, nSize); memcpy(pendingbuf, ptemp, ncurSize); } } } // Quick access operator operator SOCKET() { return _socket; } operator SOCKET() const { return _socket; } operator SockAddrIn&() { return _sockAddr; } operator const SockAddrIn&() const { return _sockAddr; } operator LPSOCKADDR() { return _sockAddr; } operator LPWSAOVERLAPPED() { return &_Overlapped; } operator LPBYTE() { return &_data[0]; } bool IsEqual(const PerSocketContext& sbuf) const { return (_socket == sbuf._socket); } bool operator==(const PerSocketContext& sbuf) const { return IsEqual( sbuf ); } bool operator==(SOCKET sock) const { return (_socket == sock); } bool operator!=(const PerSocketContext& sbuf) const { return !IsEqual( sbuf ); } private: WSAOVERLAPPED _Overlapped; std::vector _data; SOCKET _socket; SockAddrIn _sockAddr; PerSocketContext(); public: unsigned int npendingSize; // 总包大小(多个分包); unsigned int ncurSize; // 当前接收到的分包接收的大小; unsigned char *pendingbuf; // 超过一次接收包,需要分包处理的包缓冲区; }; typedef std::list SocketContextList; #endif // __PER_SOCKET_CONTEXT_20160303__