about summary refs log tree commit diff
path: root/src/engine/shared/masterserver.cpp
blob: 3037d152efdb1b3c5f3c0c4111264cc2be400f19 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com.                */
#include <stdio.h>	// sscanf

#include <base/system.h>

#include <engine/engine.h>
#include <engine/masterserver.h>
#include <engine/storage.h>

#include "linereader.h"

class CMasterServer : public IEngineMasterServer
{
public:
	// master server functions
	struct CMasterInfo
	{
		char m_aHostname[128];
		NETADDR m_Addr;
		bool m_Valid;
		
		CHostLookup m_Lookup;
	} ;

	CMasterInfo m_aMasterServers[MAX_MASTERSERVERS];
	int m_NeedsUpdate;
	IEngine *m_pEngine;
	IStorage *m_pStorage;
	
	CMasterServer()
	{
		SetDefault();
		m_NeedsUpdate = -1;
		m_pEngine = 0;
	}

	virtual int RefreshAddresses()
	{
		int i;
		
		if(m_NeedsUpdate != -1)
			return 0;
		
		dbg_msg("engine/mastersrv", "refreshing master server addresses");

		// add lookup jobs
		for(i = 0; i < MAX_MASTERSERVERS; i++)
		{
			m_pEngine->HostLookup(&m_aMasterServers[i].m_Lookup, m_aMasterServers[i].m_aHostname);
			m_aMasterServers[i].m_Valid = false;
		}
		
		m_NeedsUpdate = 1;
		return 0;
	}

	virtual void Update()
	{
		// check if we need to update
		if(m_NeedsUpdate != 1)
			return;
		m_NeedsUpdate = 0;
		
		for(int i = 0; i < MAX_MASTERSERVERS; i++)
		{
			if(m_aMasterServers[i].m_Lookup.m_Job.Status() != CJob::STATE_DONE)
				m_NeedsUpdate = 1;
			else
			{
				if(m_aMasterServers[i].m_Lookup.m_Job.Result() == 0)
				{
					m_aMasterServers[i].m_Addr = m_aMasterServers[i].m_Lookup.m_Addr;
					m_aMasterServers[i].m_Addr.port = 8300;
					m_aMasterServers[i].m_Valid = true;
				}
				else
					m_aMasterServers[i].m_Valid = false;
			}
		}
		
		if(!m_NeedsUpdate)
		{
			dbg_msg("engine/mastersrv", "saving addresses");
			Save();
		}
	}

	virtual int IsRefreshing()
	{
		return m_NeedsUpdate;
	}

	virtual NETADDR GetAddr(int Index) 
	{
		return m_aMasterServers[Index].m_Addr;
	}

	virtual const char *GetName(int Index) 
	{
		return m_aMasterServers[Index].m_aHostname;
	}

	virtual bool IsValid(int Index)
	{
		return m_aMasterServers[Index].m_Valid;
	}

	virtual void DumpServers()
	{
		for(int i = 0; i < MAX_MASTERSERVERS; i++)
		{
			char aAddrStr[NETADDR_MAXSTRSIZE];
			net_addr_str(&m_aMasterServers[i].m_Addr, aAddrStr, sizeof(aAddrStr));
			dbg_msg("mastersrv", "#%d = %s", i, aAddrStr);
		}
	}

	virtual void Init()
	{
		m_pEngine = Kernel()->RequestInterface<IEngine>();
		m_pStorage = Kernel()->RequestInterface<IStorage>();
	}

	virtual void SetDefault()
	{
		mem_zero(m_aMasterServers, sizeof(m_aMasterServers));
		for(int i = 0; i < MAX_MASTERSERVERS; i++)
			str_format(m_aMasterServers[i].m_aHostname, sizeof(m_aMasterServers[i].m_aHostname), "master%d.teeworlds.com", i+1);
	}

	virtual int Load()
	{
		CLineReader LineReader;
		IOHANDLE File;
		int Count = 0;
		if(!m_pStorage)
			return -1;
		
		// try to open file
		File = m_pStorage->OpenFile("masters.cfg", IOFLAG_READ, IStorage::TYPE_SAVE);
		if(!File)
			return -1;
		
		LineReader.Init(File);
		while(1)
		{
			CMasterInfo Info = {{0}};
			int aIp[4];
			const char *pLine = LineReader.Get();
			if(!pLine)
				break;

			// parse line
			char aAddrStr[NETADDR_MAXSTRSIZE];
			if(sscanf(pLine, "%s %s", Info.m_aHostname, aAddrStr) == 2 && net_addr_from_str(&Info.m_Addr, aAddrStr) == 0)
			{
				Info.m_Addr.port = 8300;
				if(Count != MAX_MASTERSERVERS)
				{
					m_aMasterServers[Count] = Info;
					Count++;
				}
				//else
				//	dbg_msg("engine/mastersrv", "warning: skipped master server '%s' due to limit of %d", pLine, MAX_MASTERSERVERS);
			}
			//else
			//	dbg_msg("engine/mastersrv", "warning: couldn't parse master server '%s'", pLine);
		}
		
		io_close(File);
		return 0;
	}

	virtual int Save()
	{
		IOHANDLE File;

		if(!m_pStorage)
			return -1;
			
		// try to open file
		File = m_pStorage->OpenFile("masters.cfg", IOFLAG_WRITE, IStorage::TYPE_SAVE);
		if(!File)
			return -1;

		for(int i = 0; i < MAX_MASTERSERVERS; i++)
		{
			char aAddrStr[NETADDR_MAXSTRSIZE];
			net_addr_str(&m_aMasterServers[i].m_Addr, aAddrStr, sizeof(aAddrStr));
			char aBuf[1024];
			str_format(aBuf, sizeof(aBuf), "%s %s\n", m_aMasterServers[i].m_aHostname, aAddrStr);
				
			io_write(File, aBuf, str_length(aBuf));
		}
		
		io_close(File);
		return 0;
	}
};

IEngineMasterServer *CreateEngineMasterServer() { return new CMasterServer; }