CSClient.h
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:3k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #ifndef _CSCLIENT_H_
- #define _CSCLIENT_H_
- namespace NPLayer1 {
- class Communicator;
- class BaseResource;
- enum {
- // TS发来的UDP包的最大长度
- MAX_UDP_LEN = 1400,
- };
- class CSClient {
- public:
- CSClient(Communicator* c);
- ~CSClient();
- // 解析消息
- BOOL ParseMsg(sockaddr_in& addr, int& addrlen);
- // 返回监听的Socket
- SOCKET GetSocket() { return m_Socket; };
- bool GetIsLogin() { return isLogin; }
- // 初始化,绑定一个监听端口
- P2P_RETURN_TYPE Init();
- void SendLogin();
- void SendResList();
- void SendReqRes();
- void SendDelRes(BaseResource* res);
- void SendReport(bool bRefresh);
- void SendNeedPeers();
- void SendQueryRes();
- void SendLogout();
- void SendReport2();
- void ResetLoginFail() {loginFailCount = 0;}
- // 利用memcpy复制数据,并且把src指针加上size
- static void CopyMoveSrc(void * des, const char *& src, size_t size);
- // 利用memcpy复制数据,并且把des指针加上size
- static void CopyMoveDes(char *& des, const void * src, size_t size);
- private:
- BOOL OnWelcome();
- BOOL OnPeers();
- BOOL OnConnectTo();
- BOOL OnResInfo();
- BOOL OnMsg();
- BOOL SwitchMsg(UINT8 msgType);
- // 每次编制发送消息时,第一步的操作
- BOOL SendBegin(UINT8 msgType);
- // 每次编制发送消息时,最后一步的操作
- void SendEnd();
- // 取得本机IP地址列表
- static UINT8 GetLocalIPList(in_addr*&);
- // 重新初始化
- void Reinit();
- private:
- enum {
- // 当发现known peer个数过少时,才发送NP2TS_NEED_PEERS
- MIN_KNOWN_PEERS=15,
- // 因为NP拥有的块可能有很多区间,但是不可能全部提交给TS,
- // 而且大多数情况下区间个数不会超过MAX_BLOCK_INTERVALS个,所以定义一个提交的最大个数
- MAX_BLOCK_INTERVALS = 20,
- // 连续登陆失败的次数,如果连续失败MAX_LOGIN_FAIL_COUNT次,就向界面报告
- MAX_LOGIN_FAIL_COUNT = 10,
- };
- // 监听的Socket
- SOCKET m_Socket;
- // 是否已经成功登陆
- bool isLogin;
- // 是否继续登陆
- bool bStopLogin;
- // 是否成功请求了资源
- bool isReqResSucceed;
- // 接收数据的缓冲区
- char recvBuf[MAX_UDP_LEN];
- // 从接收到的数据包读取数据的移动指针,同样是
- // 向被发送的数据包写入数据的移动指针
- char* content;
- // 登陆失败的次数
- UINT loginFailCount;
- // 初始值是MAX_LOGIN_FAIL_COUNT,每次失败变成MAX_LOGIN_FAIL_COUNT倍
- UINT maxFailCount;
- // 发送消息的互斥变量
- CriticalSection sendCS;
- // TS返回的验证码
- char checkCode[7];
- Communicator* comm;
- // TEST: 测试代码,因为有时候Login之后NeedPeers不返回,只好再次登录
- // 利用这个变量记录上次收到Peers消息的时间,如果30秒内没收到新的Peers,就重新登录
- DWORD lastRecvPeers;
- };
- }
- #endif