1 #ifndef BTANKS_NET_SCANNER_H__
2 #define BTANKS_NET_SCANNER_H__
3 
4 #include <set>
5 #include <string>
6 #include <map>
7 #include <queue>
8 #include "sdlx/thread.h"
9 #include "sdlx/mutex.h"
10 #include "mrt/sys_socket.h"
11 #include "game_type.h"
12 
13 namespace mrt {
14 	class Chunk;
15 	class UDPSocket;
16 }
17 
18 class Scanner : public sdlx::Thread {
19 public:
20 	struct Host {
21 		std::string name, map;
22 		unsigned ping, players, slots;
23 		GameType game_type;
24 
HostHost25 		Host() : name(), map(), ping(0), players(0), slots(0), game_type(GameTypeDeathMatch) {}
26 	};
27 	typedef std::map<const mrt::Socket::addr, Host> HostMap;
28 
29 	Scanner();
30 	~Scanner();
scan()31 	void scan() { _scan = true; }
changed()32 	const bool changed() const { return _changed; }
reset()33 	void reset() { _changed = false; }
34 
35 	void get(HostMap &hosts) const;
36 
37 	void add(const mrt::Socket::addr &ip, const std::string &name);
38 
39 private:
40 
41 	void createMessage(mrt::Chunk & data);
42 	void ping(mrt::UDPSocket &udp_sock);
43 
44 	virtual const int run();
45 	volatile bool _running, _scan, _changed;
46 	sdlx::Mutex _hosts_lock;
47 	HostMap _hosts;
48 	typedef std::queue<std::pair<mrt::Socket::addr, std::string> > CheckQueue;
49 	CheckQueue check_queue;
50 
51 	//std::string _bindaddr;
52 	int _port;
53 
54 
55 	//dns cache. rewrite all this cryptic scanner stuff asap
56 	std::string get_name_by_addr(const mrt::Socket::addr &addr);
57 	mrt::Socket::addr get_addr_by_name(const std::string &name);
58 
59 	typedef std::map<const std::string, mrt::Socket::addr> dns_cache_t;
60 	dns_cache_t dns_cache;
61 };
62 
63 #endif
64