1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /************************************************************************/
- /* 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 <vector>
- #include <list>
- #include "SockAddrIn.h"
- class PerSocketContext
- {
- public:
- PerSocketContext(SOCKET sock):_socket(sock){
- memset(&_Overlapped, 0, sizeof(_Overlapped));
- }
- explicit PerSocketContext(SOCKET sock, size_t size): _socket(sock){
- 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();
- }
- 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());
- }
- }
- return *this;
- }
- // 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:
- SOCKET _socket;
- SockAddrIn _sockAddr;
- WSAOVERLAPPED _Overlapped;
- std::vector<unsigned char> _data;
- PerSocketContext();
- };
- typedef std::list<PerSocketContext> SocketContextList;
- #endif // __PER_SOCKET_CONTEXT_20160303__
|