aboutsummaryrefslogtreecommitdiff
path: root/src/parsec_server/include/net_udpdriver.h
blob: fd2dcc5d00a5802e4f770bfce9e807a13a8aafe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* PARSEC HEADER: NET_UDPDriver.h
*/

#ifndef _NET_UDPDRIVER_H_
#define _NET_UDPDRIVER_H_

// NOTE: 
//
// platform dependent implementations are in e.g. NW_UDPDRIVER.CPP
//
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <stdarg.h>
#include <ctype.h>
#include <vector>

#ifndef PARSEC_SERVER
	//FIXME: we should do a e_glob_svm.h just like e_glob_sv.h
	#define TheUDPDriver		(NET_UDPDriver::GetUDPDriver())
#endif // !PARSEC_SERVER


/* Following shortens all the type casts of pointer arguments */
#define	SA	struct sockaddr


// class defining a UDP driver ------------------------------------------------
//
class NET_UDPDriver
{
protected:
	int		m_socket;
	int		m_selected_port;
	char	m_szInterface[ MAX_IPADDR_LEN + 1 ];
	char	m_szIP       [ MAX_IPADDR_LEN + 1 ];
	node_t	m_Node;								// node containing the IP adress
	std::vector<node_t >		NetworkInterfaces;
	int 	m_PreferredInterface;
	bool 	m_DriverRunning;
protected:
	// init the OS API
	int	_InitOSAPI();
	
	// kill the OS API
	int	_KillOSAPI();
	
	// open and initialize the UDP socket 
	int	_OpenSocket();
	
	// close the UDP socket 
	int	_CloseSocket();
	
	// get the local IP 
	int	_RetrieveLocalIP();

	// setup the interface
	void _SetupInterface(node_t *p_Node);

	NET_UDPDriver();
	~NET_UDPDriver();

public:

	// SINGLETON pattern
	static NET_UDPDriver* GetUDPDriver()
	{
		static NET_UDPDriver _TheUDPDriver;
		return &_TheUDPDriver;
	}
	
	// init the UDP driver
	int	InitDriver( char* szInterface, int selected_port );
	
	// kill the UDP driver
	int KillDriver();
	
	// fetch a UDP packet from the socket
	int FetchPacket( char* buf, int maxlen, SA* saddr );
	
	// send a UDP packet on the socket
	int SendPacket( char* buf, int pktsize, SA* saddr );
	
	// sleep until we have a network input or a timeout occurs
	int SleepUntilNetInput( int timeout_msec )
	{
		fd_set			rset;
		struct timeval	select_timeout;
		
		// initialize socket set
		FD_ZERO( &rset );
		FD_SET( m_socket, &rset );
		
		select_timeout.tv_sec  = 0;
		select_timeout.tv_usec = timeout_msec * 1000;
		
		// select for readable socket
		int rc = select( m_socket + 1, &rset, NULL, NULL, &select_timeout );
		return rc;
	}

	// resolve a hostname using the DNS service
	int ResolveHostName( char *hostname, node_t* node );


	// ------------------------------------------------------------------------
	// accessor methods
	// ------------------------------------------------------------------------
	int			GetSocket()	{ return m_socket; }
	node_t*		GetNode()	{ return &m_Node; }
	int 		GetPreferredInterface() { return m_PreferredInterface; }
	void		SetPreferredInterface(int p_Interface) { m_PreferredInterface = p_Interface; }
	int 		IsRunning() { return m_DriverRunning; }
};



#endif // _NET_UDPDRIVER_H_