TCPConnWrapper.h
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:2k
源码类别:
P2P编程
开发平台:
Visual C++
- #pragma once
- namespace gaov {
- namespace chaos {
- #include <Winsock2.h>
- #include "NetParse.h"
- class TCPConn
- {
- public:
- bool InitNetwork();
- PSTREAM InitData(UINT maxlen);
- int Send(PSTREAM s); // -1 : sock error; 1 : success
- PSTREAM Recv(PSTREAM s, UINT length);
- void Close(){
- if(sock!=INVALID_SOCKET){
- closesocket(sock);
- sock = INVALID_SOCKET;
- WSACleanup();
- }
- }
- protected:
- TCPConn() : sock(INVALID_SOCKET), m_bNetworkInited(false){}
- virtual ~TCPConn(){
- Close();
- }
- protected:
- bool m_bNetworkInited;
- SOCKET sock;
- STREAM in;
- STREAM out;
- };
- class TCPClient : public TCPConn
- {
- public:
- TCPClient(){};
- TCPClient(SOCKET s) { sock = s; };
- ~TCPClient(){}
- int Connect(const char* sIP, USHORT port); // -2 : connection exists; -1 : sock error
- // 0 : mem error; 1 : success
- bool SendEx(UINT8 msgType, UINT8 subMsgType, PBYTE data, UINT dataLen);
- bool SendEx2(UINT8 msgType, UINT8 subMsgType, PBYTE data1, UINT dataLen1, PBYTE data2, UINT dataLen2);
- bool RecvEx(UINT8& msgType, UINT8& subMsgType, PBYTE& data, UINT& dataLen, const bool bRecvData=true);
- bool RecvEx2(UINT8& msgType, UINT8& subMsgType, PBYTE& data1, UINT& dataLen1, PBYTE& data2, UINT& dataLen2, const bool bRecvData=true );
- private:
- };
- class TCPServer : public TCPConn
- {
- public:
- TCPServer(){};
- ~TCPServer(){}
- bool Listen(const char* sIP, USHORT port);
- SOCKET Accept();
- private:
- };
- };
- };