⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cnet.h

📁 这是我用vc编写的一个黑白棋网络游戏的使用程序
💻 H
字号:
// Cnet.h: interface for the CCnet class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CNET_H__811EC0FE_F2FC_4C9D_8B55_904D167A7F74__INCLUDED_)
#define AFX_CNET_H__811EC0FE_F2FC_4C9D_8B55_904D167A7F74__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef CCONNECTION_H
#define CCONNECTION_H

#include <winsock.h>
#include <stdio.h>

#pragma comment(lib, "wsock32.lib")		// search for wsock32 lib at compile time
#pragma comment(lib, "mpr.lib")			// search for mpr lib at compile time

typedef void (*CALLBACKFUNC)(DWORD ptr);

///////////////////////////以下是类成员函数的说明:////////////////////////////////////////////////////////
/*                              CNetworking类的成员函数
   Connection
Listen	                侦听指定的端口
StopListen	            终止侦听指定的端口
GetAccepted	            返回第一个接收的连接
SetAcceptFunc	        设置连接被接收时要调用的回调函数
SetAcceptEvent	        设置连接被接收时要处理的事件

    Information
GetLocalName	        获取本地计算机名
GetLocalIP	            获取首个本地IP地址
GetLocalIPs	            获取所有的本地IP地址
ResolveName	            通过查找IP来获取远端机器名
ResolveIP	            通过查找机器名来获取远端机器的IP地址
ResolveIPs	            通过查找机器名来获取所有远端机器的IP
GetNeighborhood	        获取所有连接到LAN的计算机名

   Status
IsListening	            如果Winsock正在侦听,返回true。
HasAccepted	            返回已经接收的连接数
GetLastError 	        返回最后的出错信息
ResolveName	            通过查找IP来获取远端机器名
ResolveIP	            通过查找机器名来获取远端机器的IP地址
ResolveIPs	            通过查找机器名来获取所有远端机器的IP
GetNeighborhood	        获取所有连接到LAN的计算机名

                      CConnection 类的成员函数
   Connection 
Connect	                连接到指定的IP和端口
Disconnect	            关闭连接
Receive	                接收位处理的(pending)数据
SetReceiveFunc	        设置数据到达时要调用的回调函数
SetReceiveEvent	        设置数据到达时要处理的事件
SetCloseFunc	        设置连接关闭时要调用的回调函数
SetCloseEvent	        设置连接关闭时要处理的事件

   Information
PeerInfo	            获取其它端点的信息

   Status  
IsConnected	            如果此连接对象被连接,则返回true。
HasReceived	            返回所接收到的字节数
GetLastError	        返回最后的出错信息
*/




class CSync
{
	HANDLE		m_sync;

public:
	CSync ();
	~CSync ();

	CSync (CSync& s);
	CSync& operator= (CSync& s);

	void Enter () const;
	void Leave () const;
};

//	another little class :
//	(class itself is not thread-safe)
class CError
{
	long		m_number;

	CError (long err);

	friend class CNetworking;
	friend class CConnection;
	friend class CSync;

public:

	long GetErrorString (char* str, long len);
};

//	class definition for CNetworking
//	this class is thread-safe!
//	access to Tag is NOT synchronized!
class CNetworking
{
public:

	//	a FIFO style list
	class CConnectionList
	{
		class Node
		{
		public:
			CConnection*	m_con;
			Node*			m_next;

			Node ();
		};

		Node*		m_first;
		Node*		m_last;
		int			m_length;

	public:
		CConnectionList ();
		~CConnectionList ();

		void			Add (CConnection* con);
		CConnection*	Remove ();
		CConnection*	Remove (int i);
		CConnection*	Item (int i);
		CConnection*	operator [] (int i);
		long			Length ();
	};

private:
	static long		m_count;

	CSync			m_sync;
	CConnectionList	m_accepted;

	SOCKET			m_socket;
	sockaddr_in		m_addr;

	HANDLE			hAcceptEvent;
	CALLBACKFUNC	hAcceptFunc;

	HANDLE			hAcceptThread;
	DWORD			dwAcceptThreadID;

	CError			m_lasterror;

	int AcceptWait ();
	static int AcceptThread (void* pThis);

	void			SetLastError (long err);

	//	allow CConnection to do stuff with this class
	//	actually, it should only check if Winsock is
	//	already intialized or not
	friend class CConnection;

public:
	CNetworking ();
	~CNetworking ();

	bool			Listen (int port);

	void			StopListen ();
	CConnection*	GetAccepted ();

	void			SetAcceptEvent (HANDLE hEvent);
	void			SetAcceptFunc (CALLBACKFUNC hFunc);

	int				HasAccepted ();
	bool			IsListening ();

	bool			GetLocalName(char *localname, int len);
	bool			GetLocalIP(char *localip, int len);
	bool			GetLocalIPs(char *localips, int len);
	bool			ResolveName(char *hostip, char *hostname, int len);
	bool			ResolveIP(char *hostname, char *hostip, int len);
	bool			ResolveIPs(char *hostname, char *hostips, int len);

	bool			GetNeighborhood(char* names, int len);

	void			GetLastError (char* str, long len);

	DWORD			Tag;
};

//	class definition for CConnection
//	this class is thread-safe!
//	access to Tag is NOT synchronized!
class CConnection
{
	//	used to cache the received data
	//	this class is thread-safe!
	class CDataStack
	{
		char*		m_buffer;
		long		m_length;
		CSync		m_sync;

	public:
		CDataStack ();
		~CDataStack ();

		void Append (const char* data, int len);
		int Remove (char* data, int len);
		int Length ();
	};

	static long		m_count;

	HANDLE			m_event;
	CSync			m_sync;
	CDataStack		m_data;

	SOCKET			m_socket;
	sockaddr_in		m_addr;

	HANDLE			hRecvEvent;
	CALLBACKFUNC	hRecvFunc;
	HANDLE			hCloseEvent;
	CALLBACKFUNC	hCloseFunc;

	HANDLE			hRecvThread;
	DWORD			dwRecvThreadID;

	CError			m_lasterror;

	int RecvWait ();
	static int RecvThread (void* pThis);

	void SetLastError (long err);

	//	allow CNetworking to do stuff with this class
	friend class CNetworking;

public:

	CConnection ();
	CConnection (const char* host, unsigned short port);
	~CConnection ();

	bool	Connect (const char* host, unsigned short port);
	void	Disconnect ();

	bool	PeerInfo (char* host, int host_len, unsigned int* port);

	int		Send (const char* buffer, int bufferlen);
	int		Receive (char* buffer, int bufferlen);

	void	SetReceiveEvent (HANDLE hEvent);
	void	SetReceiveFunc (CALLBACKFUNC hFunc);
	void	SetCloseEvent (HANDLE hEvent);
	void	SetCloseFunc (CALLBACKFUNC hFunc);

	bool	IsConnected ();
	int		HasReceived ();

	void	GetLastError (char* str, long len);

	DWORD	Tag;
};
#endif
#endif // !defined(AFX_CNET_H__811EC0FE_F2FC_4C9D_8B55_904D167A7F74__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -