File: glooxwrapper.h

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (680 lines) | stat: -rw-r--r-- 17,379 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/* Copyright (C) 2018 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_GLOOXWRAPPER_H
#define INCLUDED_GLOOXWRAPPER_H

/*

The gloox API uses various STL types (std::string, std::list, etc), and
it has functions that acquire/release ownership of objects and expect the
library's user's 'new'/'delete' functions to be compatible with the library's.

These assumptions are invalid when the game and library are built with
different compiler versions (or the same version with different build flags):
the STL types have different layouts, and new/delete can use different heaps.

We want to let people build the game on Windows with any compiler version
(VC2008, 2010, 2012, 2013, and debug vs release), without requiring them to
rebuild the gloox library themselves. And we don't want to provide ~8 different
prebuilt versions of the library.

glooxwrapper replaces the gloox API with a version that is safe to use across
compiler versions. glooxwrapper and gloox must be compiled together with the
same version, but the resulting library can be used with any other compiler.

This is the small subset of the API that the game currently uses, with no
attempt to be comprehensive.

General design and rules:

 * There is a strict boundary between gloox+glooxwrapper.cpp, and the game
   code that includes glooxwrapper.h.
   Objects allocated with new/delete on one side of the boundary must be
   freed/allocated on the same side.
   Objects allocated with glooxwrapper_alloc()/glooxwrapper_delete() can be
   freely shared across the boundary.

 * glooxwrapper.h and users of glooxwrapper must not use any types from
   the gloox namespace, except for enums.

 * std::string is replaced with glooxwrapper::string,
   std::list with glooxwrapper::list

 * Most glooxwrapper classes are simple wrappers around gloox classes.
   Some always take ownership of their wrapped gloox object (i.e. their
   destructor will delete the wrapped object too); some never do; and some
   can be used either way (indicated by an m_Owned field).

*/

#if OS_WIN
# include "lib/sysdep/os/win/win.h"
// Prevent gloox pulling in windows.h
# define _WINDOWS_
#endif

#include <gloox/client.h>
#include <gloox/mucroom.h>
#include <gloox/registration.h>
#include <gloox/message.h>
#include <gloox/jinglecontent.h>
#include <gloox/jingleiceudp.h>
#include <gloox/jinglesessionhandler.h>
#include <gloox/jinglesessionmanager.h>

#include <cstring>

#if OS_WIN
#define GLOOXWRAPPER_API __declspec(dllexport)
#else
#define GLOOXWRAPPER_API
#endif

namespace glooxwrapper
{
	class Client;
	class DataForm;
	class DelayedDelivery;
	class Disco;
	class IQ;
	class JID;
	class MUCRoom;
	class MUCRoomConfigHandler;
	class Message;
	class MessageSession;
	class OOB;
	class Presence;
	class StanzaError;
	class StanzaExtension;
	class Tag;

	class ClientImpl;
	class MUCRoomHandlerWrapper;
	class SessionHandlerWrapper;

	GLOOXWRAPPER_API void* glooxwrapper_alloc(size_t size);
	GLOOXWRAPPER_API void glooxwrapper_free(void* p);

	class string
	{
	private:
		size_t m_Size;
		char* m_Data;
	public:
		string()
		{
			m_Size = 0;
			m_Data = (char*)glooxwrapper_alloc(1);
			m_Data[0] = '\0';
		}

		string(const string& str)
		{
			m_Size = str.m_Size;
			m_Data = (char*)glooxwrapper_alloc(m_Size + 1);
			memcpy(m_Data, str.m_Data, m_Size + 1);
		}

		string(const std::string& str) : m_Data(NULL)
		{
			m_Size = str.size();
			m_Data = (char*)glooxwrapper_alloc(m_Size + 1);
			memcpy(m_Data, str.c_str(), m_Size + 1);
		}

		string(const char* str)
		{
			m_Size = strlen(str);
			m_Data = (char*)glooxwrapper_alloc(m_Size + 1);
			memcpy(m_Data, str, m_Size + 1);
		}

		string& operator=(const string& str)
		{
			if (this != &str)
			{
				glooxwrapper_free(m_Data);
				m_Size = str.m_Size;
				m_Data = (char*)glooxwrapper_alloc(m_Size + 1);
				memcpy(m_Data, str.m_Data, m_Size + 1);
			}
			return *this;
		}

		~string()
		{
			glooxwrapper_free(m_Data);
		}

		std::string to_string() const
		{
			return std::string(m_Data, m_Size);
		}

		const char* c_str() const
		{
			return m_Data;
		}

		bool empty() const
		{
			return m_Size == 0;
		}

		bool operator==(const char* str) const
		{
			return strcmp(m_Data, str) == 0;
		}

		bool operator!=(const char* str) const
		{
			return strcmp(m_Data, str) != 0;
		}
	};

	static inline std::ostream& operator<<(std::ostream& stream, const string& string)
	{
		return stream << string.c_str();
	}

	template<typename T>
	class list
	{
	private:
		struct node
		{
			node(const T& item) : m_Item(item), m_Next(NULL) {}
			T m_Item;
			node* m_Next;
		};
		node* m_Head;
		node* m_Tail;

	public:
		struct const_iterator
		{
			const node* m_Node;
			const_iterator(const node* n) : m_Node(n) {}
			bool operator!=(const const_iterator& it) { return m_Node != it.m_Node; }
			const_iterator& operator++() { m_Node = m_Node->m_Next; return *this; }
			const T& operator*() { return m_Node->m_Item; }
		};
		const_iterator begin() const { return const_iterator(m_Head); }
		const_iterator end() const { return const_iterator(NULL); }

		list() : m_Head(NULL), m_Tail(NULL) {}

		list(const list& src) : m_Head(NULL), m_Tail(NULL)
		{
			*this = src;
		}

		list& operator=(const list& src)
		{
			if (this != &src)
			{
				clear();
				for (node* n = src.m_Head; n; n = n->m_Next)
					push_back(n->m_Item);
			}
			return *this;
		}

		~list()
		{
			clear();
		}

		void push_back(const T& item)
		{
			node* n = new (glooxwrapper_alloc(sizeof(node))) node(item);
			if (m_Tail)
				m_Tail->m_Next = n;
			m_Tail = n;
			if (!m_Head)
				m_Head = n;
		}

		void clear()
		{
			node* n = m_Head;
			while (n)
			{
				node* next = n->m_Next;
				glooxwrapper_free(n);
				n = next;
			}
			m_Head = m_Tail = NULL;
		}

		const T& front() const
		{
			return *begin();
		}
	};

	typedef glooxwrapper::list<Tag*> TagList;
	typedef glooxwrapper::list<const Tag*> ConstTagList;

	struct CertInfo
	{
		int status;
		bool chain;
		string issuer;
		string server;
		int date_from;
		int date_to;
		string protocol;
		string cipher;
		string mac;
		string compression;
	};

	struct RegistrationFields
	{
		string username;
		string nick;
		string password;
		string name;
		string first;
		string last;
		string email;
		string address;
		string city;
		string state;
		string zip;
		string phone;
		string url;
		string date;
		string misc;
		string text;
	};

	struct MUCRoomParticipant
	{
		JID* nick;
		gloox::MUCRoomAffiliation affiliation;
		gloox::MUCRoomRole role;
		JID* jid;
		int flags;
		string reason;
		JID* actor;
		string newNick;
		string status;
		JID* alternate;
	};


	class GLOOXWRAPPER_API ConnectionListener
	{
	public:
		virtual ~ConnectionListener() {}
		virtual void onConnect() = 0;
		virtual void onDisconnect(gloox::ConnectionError e) = 0;
		virtual bool onTLSConnect(const CertInfo& info) = 0;
	};

	class GLOOXWRAPPER_API IqHandler
	{
	public:
		virtual ~IqHandler() {}
		virtual bool handleIq(const IQ& iq) = 0;
		virtual void handleIqID(const IQ& iq, int context) = 0;
	};

	class GLOOXWRAPPER_API MessageHandler
	{
	public:
		virtual ~MessageHandler() {}
		virtual void handleMessage(const Message& msg, MessageSession* session = 0) = 0; // MessageSession not supported
	};

	class GLOOXWRAPPER_API MUCRoomHandler
	{
	public:
		virtual ~MUCRoomHandler() {}
		virtual void handleMUCParticipantPresence(MUCRoom* room, const MUCRoomParticipant participant, const Presence& presence) = 0; // MUCRoom not supported
		virtual void handleMUCMessage(MUCRoom* room, const Message& msg, bool priv) = 0; // MUCRoom not supported
		virtual void handleMUCError(MUCRoom* room, gloox::StanzaError error) = 0; // MUCRoom not supported
		virtual void handleMUCSubject(MUCRoom* room, const string& nick, const string& subject) = 0; // MUCRoom not supported
	};

	class GLOOXWRAPPER_API RegistrationHandler
	{
	public:
		virtual ~RegistrationHandler() {}
		virtual void handleRegistrationFields(const JID& from, int fields, string instructions) = 0;
		virtual void handleAlreadyRegistered(const JID& from) = 0;
		virtual void handleRegistrationResult(const JID& from, gloox::RegistrationResult regResult) = 0;
		virtual void handleDataForm(const JID& from, const DataForm& form) = 0; // DataForm not supported
		virtual void handleOOB(const JID& from, const OOB& oob) = 0; // OOB not supported
	};

	class GLOOXWRAPPER_API StanzaExtension
	{
	public:
		StanzaExtension(int type) : m_extensionType(type) {}
		virtual ~StanzaExtension() {}
		virtual const string& filterString() const = 0;
		virtual StanzaExtension* newInstance(const Tag* tag) const = 0;
		virtual glooxwrapper::Tag* tag() const = 0;
		virtual StanzaExtension* clone() const = 0;

		int extensionType() const { return m_extensionType; }
	private:
		int m_extensionType;
	};


	class GLOOXWRAPPER_API Client
	{
		NONCOPYABLE(Client);
	private:
		gloox::Client* m_Wrapped;
		ClientImpl* m_Impl;
		Disco* m_DiscoWrapper;

	public:
		gloox::Client* getWrapped() { return m_Wrapped; }

		bool connect(bool block = true);
		gloox::ConnectionError recv(int timeout = -1);
		const string getID() const;
		void send(const IQ& iq);

		void setTls(gloox::TLSPolicy tls);
		void setCompression(bool compression);

		void setSASLMechanisms(int mechanisms);
		void registerStanzaExtension(StanzaExtension* ext);
		void registerConnectionListener(ConnectionListener* cl);
		void registerIqHandler(IqHandler* ih, int exttype);
		void registerMessageHandler(MessageHandler* mh);

		bool removePresenceExtension(int type);

		Disco* disco() const { return m_DiscoWrapper; }

		Client(const string& server);
		Client(const JID& jid, const string& password, int port = -1);
		~Client();

		void setPresence(gloox::Presence::PresenceType pres, int priority, const string& status = "");
		void disconnect();
	};

	class GLOOXWRAPPER_API DelayedDelivery
	{
		NONCOPYABLE(DelayedDelivery);
	private:
		const gloox::DelayedDelivery* m_Wrapped;
	public:
		DelayedDelivery(const gloox::DelayedDelivery* wrapped);
		const string stamp() const;
	};

	class GLOOXWRAPPER_API Disco
	{
		NONCOPYABLE(Disco);
	private:
		gloox::Disco* m_Wrapped;
	public:
		Disco(gloox::Disco* wrapped);
		void setVersion(const string& name, const string& version, const string& os = "");
		void setIdentity(const string& category, const string& type, const string& name = "");
	};

	class GLOOXWRAPPER_API IQ
	{
		NONCOPYABLE(IQ);
	private:
		gloox::IQ* m_Wrapped;
		bool m_Owned;
	public:
		const gloox::IQ& getWrapped() const { return *m_Wrapped; }
		IQ(const gloox::IQ& iq) : m_Wrapped(const_cast<gloox::IQ*>(&iq)), m_Owned(false) { }

		IQ(gloox::IQ::IqType type, const JID& to, const string& id);
		~IQ();

		void addExtension(const StanzaExtension* se);
		const StanzaExtension* findExtension(int type) const;

		template<class T>
		inline const T* findExtension(int type) const
		{
			return static_cast<const T*>(findExtension(type));
		}

		gloox::IQ::IqType subtype() const;
		const string id() const;
		const gloox::JID& from() const;

		gloox::StanzaError error_error() const; // wrapper for ->error()->error()
		Tag* tag() const;
	};

	class GLOOXWRAPPER_API JID
	{
		NONCOPYABLE(JID);
	private:
		gloox::JID* m_Wrapped;
		bool m_Owned;
		void init(const char* data, size_t len);
	public:
		const gloox::JID& getWrapped() const { return *m_Wrapped; }
		JID(const gloox::JID& jid) : m_Wrapped(const_cast<gloox::JID*>(&jid)), m_Owned(false) { }

		JID();
		JID(const string& jid);
		JID(const std::string& jid) { init(jid.c_str(), jid.size()); }
		~JID();

		string username() const;
		string resource() const;
	};

	class GLOOXWRAPPER_API Message
	{
		NONCOPYABLE(Message);
	private:
		gloox::Message* m_Wrapped;
		bool m_Owned;
		glooxwrapper::JID* m_From;
	public:
		Message(gloox::Message* wrapped, bool owned);
		~Message();
		gloox::Message::MessageType subtype() const;
		const JID& from() const;
		string body() const;
		string subject(const string& lang = "default") const;
		string thread() const;
		const glooxwrapper::DelayedDelivery* when() const;
	};

	class GLOOXWRAPPER_API MUCRoom
	{
		NONCOPYABLE(MUCRoom);
	private:
		gloox::MUCRoom* m_Wrapped;
		MUCRoomHandlerWrapper* m_HandlerWrapper;
	public:
		MUCRoom(Client* parent, const JID& nick, MUCRoomHandler* mrh, MUCRoomConfigHandler* mrch = 0);
		~MUCRoom();
		const string nick() const;
		void join(gloox::Presence::PresenceType type = gloox::Presence::Available, const string& status = "", int priority = 0);
		void leave(const string& msg = "");
		void send(const string& message);
		void setNick(const string& nick);
		void setPresence(gloox::Presence::PresenceType presence, const string& msg = "");
		void setRequestHistory(int value, gloox::MUCRoom::HistoryRequestType type);
		void kick(const string& nick, const string& reason);
		void ban(const string& nick, const string& reason);
	};

	class GLOOXWRAPPER_API Presence
	{
		gloox::Presence::PresenceType m_Presence;
	public:
		Presence(gloox::Presence::PresenceType presence) : m_Presence(presence) {}
		gloox::Presence::PresenceType presence() const { return m_Presence; }
	};

	class GLOOXWRAPPER_API Registration
	{
		NONCOPYABLE(Registration);
	private:
		gloox::Registration* m_Wrapped;
		std::list<shared_ptr<gloox::RegistrationHandler> > m_RegistrationHandlers;
	public:
		Registration(Client* parent);
		~Registration();
		void fetchRegistrationFields();
		bool createAccount(int fields, const RegistrationFields& values);
		void registerRegistrationHandler(RegistrationHandler* rh);
	};

	class GLOOXWRAPPER_API Tag
	{
		NONCOPYABLE(Tag);
	private:
		gloox::Tag* m_Wrapped;
		bool m_Owned;

		Tag(const string& name);
		Tag(const string& name, const string& cdata);
		Tag(gloox::Tag* wrapped, bool owned) : m_Wrapped(wrapped), m_Owned(owned) {}
		~Tag();

	public:
		// Internal use:
		gloox::Tag* getWrapped() { return m_Wrapped; }
		gloox::Tag* stealWrapped() { m_Owned = false; return m_Wrapped; }
		static Tag* allocate(gloox::Tag* wrapped, bool owned);

		// Instead of using new/delete, Tags must be allocated/freed with these functions
		static Tag* allocate(const string& name);
		static Tag* allocate(const string& name, const string& cdata);
		static void free(const Tag* tag);

		bool addAttribute(const string& name, const string& value);
		string findAttribute(const string& name) const;
		Tag* clone() const;
		string xmlns() const;
		bool setXmlns(const string& xmlns);
		string xml() const;
		void addChild(Tag* child);
		string name() const;
		string cdata() const;
		const Tag* findTag_clone(const string& expression) const; // like findTag but must be Tag::free()d
		ConstTagList findTagList_clone(const string& expression) const; // like findTagList but each tag must be Tag::free()d
	};

	namespace Jingle
	{

		class GLOOXWRAPPER_API Plugin
		{
		protected:
			const gloox::Jingle::Plugin* m_Wrapped;
			bool m_Owned;

		public:
			Plugin(const gloox::Jingle::Plugin* wrapped, bool owned) : m_Wrapped(wrapped), m_Owned(owned) {}

			virtual ~Plugin();

			const Plugin findPlugin(int type) const;
			const gloox::Jingle::Plugin* getWrapped() const { return m_Wrapped; }
		};

		typedef list<const Plugin*> PluginList;

		class GLOOXWRAPPER_API Content : public Plugin
		{
		public:
			Content(const string& name, const PluginList& plugins);
			Content();
		};

		class GLOOXWRAPPER_API ICEUDP : public Plugin
		{
		public:
			struct Candidate {
				string ip;
				int port;
			};

			typedef std::list<Candidate> CandidateList;

			ICEUDP(CandidateList& candidates);
			ICEUDP();

			const CandidateList candidates() const;
		};

		class GLOOXWRAPPER_API Session
		{
		protected:
			gloox::Jingle::Session* m_Wrapped;
			bool m_Owned;

		public:
			class GLOOXWRAPPER_API Jingle
			{
			private:
				const gloox::Jingle::Session::Jingle* m_Wrapped;
				bool m_Owned;
			public:
				Jingle(const gloox::Jingle::Session::Jingle* wrapped, bool owned) : m_Wrapped(wrapped), m_Owned(owned) {}

				const PluginList plugins() const;

				ICEUDP::Candidate getCandidate() const;
			};

			Session(gloox::Jingle::Session* wrapped, bool owned) : m_Wrapped(wrapped), m_Owned(owned) {}

			bool sessionInitiate(char* ipStr, uint16_t port);
		};

		class GLOOXWRAPPER_API SessionHandler
		{
		public:
			virtual ~SessionHandler() {}
			virtual void handleSessionAction(gloox::Jingle::Action action, Session* session, const Session::Jingle* jingle) = 0;
		};

	}

	class GLOOXWRAPPER_API SessionManager
	{
	private:
		gloox::Jingle::SessionManager* m_Wrapped;
		SessionHandlerWrapper* m_HandlerWrapper;

	public:
		SessionManager(Client* parent, Jingle::SessionHandler* sh);
		~SessionManager();
		void registerPlugins();
		Jingle::Session createSession(const JID& callee);
	};

}

#endif // INCLUDED_GLOOXWRAPPER_H