udplistener.h

来自「这是整套横扫千军3D版游戏的源码」· C头文件 代码 · 共 73 行

H
73
字号
#ifndef _UDPLISTENER 
#define _UDPLISTENER

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <list>
#include <queue>

#include "UDPSocket.h"
#include "UDPConnection.h"

namespace netcode
{


/**
@brief Class for handling Connections on an UDPSocket
Use this class if you want to use a UDPSocket to connect to more than one other clients. You can Listen for new connections, initiate new ones and send/recieve data to/from them.
@author Karl-Robert Ernst
*/
class UDPListener : boost::noncopyable
{
public:
	/**
	@brief Open a socket and make it ready for listening
	*/
	UDPListener(int port);
	
	/**
	@brief close the socket and DELETE all connections
	*/
	~UDPListener();
	
	/**
	@brief Run this from time to time
	This does: recieve data from the socket and hand it to the associated UDPConnection, or open a new UDPConnection. It also Updates all of its connections
	*/
	void Update(std::queue< boost::shared_ptr<CConnection> >& waitingQueue);
	
	/**
	@brief Initiate a connection
	Make a new connection to address:port. It will be pushed back in conn
	*/
	boost::shared_ptr<UDPConnection> SpawnConnection(const std::string& address, const unsigned port);
	
	/**
	@brief Set if we are going to accept new connections or drop all data from unconnected addresses
	*/
	void SetWaitingForConnections(const bool state);
	
	/**
	@brief Are we accepting new connections?
	*/
	bool GetWaitingForConnections() const;
	
private:
	/**
	@brief Do we accept packets from unknown sources?
	If true, we will create a new connection, if false, it get dropped
	*/
	bool acceptNewConnections;
	
	/// Our socket
	boost::shared_ptr<UDPSocket> mySocket;
	
	/// all connections
	std::list< boost::weak_ptr< UDPConnection> > conn;
};

}

#endif

⌨️ 快捷键说明

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