1 /* XQF - Quake server browser and launcher
2 * Copyright (C) 1998-2000 Roman Pozlevich <roma@botik.ru>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17 */
18
19 #ifndef __SERVER_H__
20 #define __SERVER_H__
21
22 #include <stdio.h>
23
24 #include <glib.h>
25
26 #include "xqf.h"
27 #include "debug.h"
28
29 extern struct server *server_add (struct host *h, unsigned short port, enum server_type type);
30 extern struct userver *userver_add (const char *name, unsigned short port, enum server_type type);
31 extern void server_free_info (struct server *s);
32
33 // return NULL if refcount dropped to zero, s otherwise
34 extern struct server* server_unref (struct server *s);
35 extern void userver_unref (struct userver *s);
36
37 struct server* server_change_port (struct server* s, int newport);
38
39 extern GSList *server_list_copy (GSList *list);
40 extern GSList *userver_list_copy (GSList *list);
41
42 extern GSList *server_list_append_list (GSList *list, GSList *servers, enum server_type type);
43 extern GSList *userver_list_append_list (GSList *list, GSList *uservers, enum server_type type);
44
45 extern int servers_total (void);
46 extern int uservers_total (void);
47 extern GSList *all_servers (void);
48
49
50 extern int parse_address (char *str, char **addr, unsigned short *port);
51
52 extern struct server *userver_set_host (struct userver *s, struct host *h);
53 extern void uservers_to_servers (GSList **uservers, GSList **servers);
54
55 extern void server_lists_intersect (GSList **list1, GSList **list2);
56
57 extern void server_list_fprintf (FILE *f, GSList *servers);
58 extern void userver_list_fprintf (FILE *f, GSList *uservers);
59
server_ref(struct server * server)60 static inline void server_ref (struct server *server) {
61 if (server){
62 server->ref_count++;
63 debug (7, "server_ref() -- Server %lx now at %d", server, server->ref_count);
64 }
65 }
66
userver_ref(struct userver * us)67 static inline void userver_ref (struct userver *us) {
68 if (us) us->ref_count++;
69 }
70
server_list_prepend(GSList * list,struct server * server)71 static inline GSList *server_list_prepend (GSList *list, struct server *server) {
72 if (g_slist_find (list, server) == NULL) {
73 debug (6, "server_list_prepend() -- Server %lx", server);
74 list = g_slist_prepend (list, server);
75 server_ref (server);
76 }
77 return list;
78 }
79
80 // This version does not to the duplicate check - you must do it yourself
server_list_prepend_ndp(GSList * list,struct server * server)81 static inline GSList *server_list_prepend_ndp (GSList *list, struct server *server) {
82 debug (6, "server_list_prepend_ndp() -- Server %lx", server);
83 list = g_slist_prepend (list, server);
84 server_ref (server);
85
86 return list;
87 }
88
server_list_append(GSList * list,struct server * server)89 static inline GSList *server_list_append (GSList *list, struct server *server) {
90 if (g_slist_find (list, server) == NULL) {
91 debug (6, "server_list_append() -- Server %lx", server);
92 list = g_slist_append (list, server);
93 server_ref (server);
94 }
95 return list;
96 }
97
server_list_remove(GSList * list,struct server * s)98 static inline GSList *server_list_remove (GSList *list, struct server *s) {
99 debug (6, "server_list_remove() -- remove server %lx from list %lx", s, list);
100 if (g_slist_find (list, s)) {
101 list = g_slist_remove (list, s);
102 server_unref (s);
103 }
104 return list;
105 }
106
userver_list_add(GSList * list,struct userver * us)107 static inline GSList *userver_list_add (GSList *list, struct userver *us) {
108 if (g_slist_find (list, us) == NULL) {
109 list = g_slist_prepend (list, us);
110 userver_ref (us);
111 }
112 return list;
113 }
114
userver_list_remove(GSList * list,struct userver * us)115 static inline GSList *userver_list_remove (GSList *list, struct userver *us) {
116 if (g_slist_find (list, us)) {
117 list = g_slist_remove (list, us);
118 userver_unref (us);
119 }
120 return list;
121 }
122
server_list_free(GSList * list)123 static inline void server_list_free (GSList *list) {
124 if (list) {
125 debug (6, "server_list_free() -- list %lx", list);
126 g_slist_foreach (list, (GFunc) server_unref, NULL);
127 g_slist_free (list);
128 }
129 }
130
userver_list_free(GSList * list)131 static inline void userver_list_free (GSList *list) {
132 if (list) {
133 g_slist_foreach (list, (GFunc) userver_unref, NULL);
134 g_slist_free (list);
135 }
136 }
137
138 /** \brief put server properties into environment */
139 void server_set_env(const struct server* s);
140
141 #endif /* __SERVER_H__ */
142