Freeciv21
Develop your civilization from humble roots to a global empire
servers.h
Go to the documentation of this file.
1 /***********************************************************************
2 Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  /\/\ part of Freeciv21. Freeciv21 is free software: you can
4  \_\ _..._ redistribute it and/or modify it under the terms of the
5  (" )(_..._) GNU General Public License as published by the Free
6  ^^ // \\ Software Foundation, either version 3 of the License,
7  or (at your option) any later version. You should have
8 received a copy of the GNU General Public License along with Freeciv21.
9  If not, see https://www.gnu.org/licenses/.
10 ***********************************************************************/
11 
12 #pragma once
13 
14 #include <QNetworkDatagram>
15 #include <QUdpSocket>
16 
17 #define SERVER_LAN_PORT 4555
18 #define SERVER_LAN_TTL 1
19 #define SERVER_LAN_VERSION 2
20 
27 };
28 
29 class fcUdpScan : public QUdpSocket {
30 public:
31  static void drop();
32  static fcUdpScan *i();
33  ~fcUdpScan() override = default;
34  ;
35  bool begin_scan(struct server_scan *scan);
37 public slots:
38  void readPendingDatagrams();
39 private slots:
40  void sockError(QAbstractSocket::SocketError socketError);
41 
42 private:
44  fcUdpScan(QObject *parent = 0);
46  QList<QNetworkDatagram> datagram_list;
47 };
48 
49 struct str_players {
50  char *name;
51  char *type;
52  char *host;
53  char *nation;
54 };
55 struct server {
56  char *host;
57  int port;
58  char *capability;
59  char *patches;
60  char *version;
61  char *state;
62  char *topic;
63  char *message;
65  int nplayers;
66  int humans;
67 };
68 
69 #define SPECLIST_TAG server
70 #define SPECLIST_TYPE struct server
71 #include "speclist.h"
72 
73 #define server_list_iterate(serverlist, pserver) \
74  TYPED_LIST_ITERATE(struct server, serverlist, pserver)
75 #define server_list_iterate_end LIST_ITERATE_END
76 
77 struct server_scan;
78 
80  SERVER_SCAN_LOCAL, // Local servers, detected through a LAN scan
81  SERVER_SCAN_GLOBAL, // Global servers, read from the metaserver
83 };
84 
85 typedef void (*ServerScanErrorFunc)(struct server_scan *scan,
86  const char *message);
87 
90 enum server_scan_type server_scan_get_type(const struct server_scan *scan);
92 struct server_list *server_scan_get_list(struct server_scan *scan);
93 void server_scan_finish(struct server_scan *scan);
QList< QNetworkDatagram > datagram_list
Definition: servers.h:46
fcUdpScan(QObject *parent=0)
Definition: servers.cpp:66
static fcUdpScan * i()
returns fcUdpScan instance, use it to initizalize fcUdpScan or for calling methods
Definition: servers.cpp:79
~fcUdpScan() override=default
static void drop()
deletes fcUdpScan
Definition: servers.cpp:104
bool begin_scan(struct server_scan *scan)
Broadcast an UDP package to all servers on LAN, requesting information about the server.
Definition: servers.cpp:115
void readPendingDatagrams()
Definition: servers.cpp:156
enum server_scan_status get_server_list(struct server_scan *scan)
Listens for UDP packets broadcasted from a server that responded to the request-packet sent from the ...
Definition: servers.cpp:178
struct server_scan * fcudp_scan
Definition: servers.h:43
static fcUdpScan * m_instance
Definition: servers.h:45
void sockError(QAbstractSocket::SocketError socketError)
Pass errors to main scan.
Definition: servers.cpp:90
struct server_list * server_scan_get_list(struct server_scan *scan)
Returns the srv_list currently held by the scan (may be nullptr).
Definition: servers.cpp:594
struct server_scan * server_scan_begin(enum server_scan_type type, ServerScanErrorFunc error_func)
Creates a new server scan and returns it, or nullptr if impossible.
Definition: servers.cpp:518
void server_scan_finish(struct server_scan *scan)
Closes the socket listening on the scan, frees the list of servers, and frees the memory allocated fo...
Definition: servers.cpp:607
server_scan_status
Definition: servers.h:21
@ SCAN_STATUS_WAITING
Definition: servers.h:23
@ SCAN_STATUS_ABORT
Definition: servers.h:26
@ SCAN_STATUS_PARTIAL
Definition: servers.h:24
@ SCAN_STATUS_ERROR
Definition: servers.h:22
@ SCAN_STATUS_DONE
Definition: servers.h:25
server_scan_type
Definition: servers.h:79
@ SERVER_SCAN_LOCAL
Definition: servers.h:80
@ SERVER_SCAN_LAST
Definition: servers.h:82
@ SERVER_SCAN_GLOBAL
Definition: servers.h:81
enum server_scan_type server_scan_get_type(const struct server_scan *scan)
A simple query function to determine the type of a server scan (previously allocated in server_scan_b...
Definition: servers.cpp:546
void(* ServerScanErrorFunc)(struct server_scan *scan, const char *message)
Definition: servers.h:85
enum server_scan_status server_scan_poll(struct server_scan *scan)
A function to query servers of the server scan.
Definition: servers.cpp:569
ServerScanErrorFunc error_func
Definition: servers.cpp:46
enum server_scan_type type
Definition: servers.cpp:45
Definition: servers.h:55
char * capability
Definition: servers.h:58
char * patches
Definition: servers.h:59
int humans
Definition: servers.h:66
char * host
Definition: servers.h:56
char * version
Definition: servers.h:60
char * message
Definition: servers.h:63
char * state
Definition: servers.h:61
str_players * players
Definition: servers.h:64
int port
Definition: servers.h:57
char * topic
Definition: servers.h:62
int nplayers
Definition: servers.h:65
char * host
Definition: servers.h:52
char * name
Definition: servers.h:50
char * type
Definition: servers.h:51
char * nation
Definition: servers.h:53