123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- /************************************************************************/
- /* 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){
- 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<unsigned char> _data;
- SOCKET _socket;
- SockAddrIn _sockAddr;
- PerSocketContext();
- public:
- unsigned int npendingSize; // 总包大小(多个分包);
- unsigned int ncurSize; // 当前接收到的分包接收的大小;
- unsigned char *pendingbuf; // 超过一次接收包,需要分包处理的包缓冲区;
- };
- typedef std::list<PerSocketContext> SocketContextList;
- #endif // __PER_SOCKET_CONTEXT_20160303__
|