summaryrefslogtreecommitdiff
path: root/indra/llmessage/message.h
blob: 3f9dfa08d6e0f6f3296c5b6a736807c67a49d6b6 (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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
/** 
 * @file message.h
 * @brief LLMessageSystem class header file
 *
 * Copyright (c) 2001-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#ifndef LL_MESSAGE_H
#define LL_MESSAGE_H

#include <cstring>
#include <stdio.h>
#include <map>
#include <set>

#if LL_LINUX
#include <endian.h>
#include <netinet/in.h>
#endif

#if LL_WINDOWS
#include "winsock2.h" // htons etc.
#endif

#include "llerror.h"
#include "net.h"
#include "string_table.h"
#include "llptrskipmap.h"
#include "llcircuit.h"
#include "lltimer.h"
#include "llpacketring.h"
#include "llhost.h"
#include "llpacketack.h"
#include "doublelinkedlist.h"
#include "message_prehash.h"
#include "llstl.h"
#include "lldarray.h"

const U32 MESSAGE_MAX_STRINGS_LENGTH = 64;
const U32 MESSAGE_NUMBER_OF_HASH_BUCKETS = 8192;

const S32 MESSAGE_MAX_PER_FRAME = 400;

class LLMessageStringTable
{
public:
	LLMessageStringTable();
	~LLMessageStringTable();

	char *getString(const char *str);

	U32	 mUsed;
	BOOL mEmpty[MESSAGE_NUMBER_OF_HASH_BUCKETS];
	char mString[MESSAGE_NUMBER_OF_HASH_BUCKETS][MESSAGE_MAX_STRINGS_LENGTH];	/* Flawfinder: ignore */
};

extern LLMessageStringTable gMessageStringTable;

// Individual Messages are described with the following format
// Note that to ease parsing, keywords are used
//
//	// Comment 					(Comment like a C++ single line comment)
//  							Comments can only be placed between Messages
// {
// MessageName					(same naming restrictions as C variable)
// Frequency					("High", "Medium", or "Low" - determines whether message ID is 8, 16, or 32-bits -- 
//								 there can 254 messages in the first 2 groups, 32K in the last group)
//								(A message can be made up only of the Name if it is only a signal)
// Trust						("Trusted", "NotTrusted" - determines if a message will be accepted
//								 on a circuit.  "Trusted" messages are not accepted from NotTrusted circuits
//								 while NotTrusted messages are accepted on any circuit.  An example of a
//								 NotTrusted circuit is any circuit from the viewer.)
// Encoding						("Zerocoded", "Unencoded" - zerocoded messages attempt to compress sequences of 
//								 zeros, but if there is no space win, it discards the compression and goes unencoded)
//		{
//		Block Name				(same naming restrictions as C variable)
//		Block Type				("Single", "Multiple", or "Variable" - determines if the block is coded once, 
//								 a known number of times, or has a 8 bit argument encoded to tell the decoder 
//								 how many times the group is repeated)
//		Block Repeat Number		(Optional - used only with the "Multiple" type - tells how many times the field is repeated
//			{
//			Variable 1 Name		(same naming restrictions as C variable)
//			Variable Type		("Fixed" or "Variable" - determines if the variable is of fixed size or needs to 
//								 encode an argument describing the size in bytes)
//			Variable Size		(In bytes, either of the "Fixed" variable itself or of the size argument)
//
//			repeat variables
//
//			}
//
//			Repeat for number of variables in block		
//		}
//
//		Repeat for number of blocks in message
// }
// Repeat for number of messages in file
//

// Constants
const S32 MAX_MESSAGE_INTERNAL_NAME_SIZE = 255;
const S32 MAX_BUFFER_SIZE = NET_BUFFER_SIZE;
const S32 MAX_BLOCKS = 255;

const U8 LL_ZERO_CODE_FLAG = 0x80;
const U8 LL_RELIABLE_FLAG = 0x40;
const U8 LL_RESENT_FLAG = 0x20;
const U8 LL_ACK_FLAG = 0x10;

const S32	LL_MINIMUM_VALID_PACKET_SIZE = LL_PACKET_ID_SIZE + 1;		// 4 bytes id + 1 byte message name (high)

const S32 LL_DEFAULT_RELIABLE_RETRIES = 3;
const F32 LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS = 1.f;
const F32 LL_MINIMUM_SEMIRELIABLE_TIMEOUT_SECONDS = 1.f;
const F32 LL_PING_BASED_TIMEOUT_DUMMY = 0.0f;

// *NOTE: Maybe these factors shouldn't include the msec to sec conversion
// implicitly.
// However, all units should be MKS.
const F32 LL_SEMIRELIABLE_TIMEOUT_FACTOR	= 5.f / 1000.f;		// factor * averaged ping
const F32 LL_RELIABLE_TIMEOUT_FACTOR		= 5.f / 1000.f;      // factor * averaged ping
const F32 LL_FILE_XFER_TIMEOUT_FACTOR		= 5.f / 1000.f;      // factor * averaged ping
const F32 LL_LOST_TIMEOUT_FACTOR			= 16.f / 1000.f;     // factor * averaged ping for marking packets "Lost"
const F32 LL_MAX_LOST_TIMEOUT				= 5.f;				// Maximum amount of time before considering something "lost"

const S32 MAX_MESSAGE_COUNT_NUM = 1024;

// Forward declarations
class LLCircuit;
class LLVector3;
class LLVector4;
class LLVector3d;
class LLQuaternion;
class LLSD;
class LLUUID;
class LLMessageSystem;

// message data pieces are used to collect the data called for by the message template

// iterator typedefs precede each class as needed
typedef enum e_message_variable_type
{
	MVT_NULL,
	MVT_FIXED,
	MVT_VARIABLE,
	MVT_U8,
	MVT_U16,
	MVT_U32,
	MVT_U64,
	MVT_S8,
	MVT_S16,
	MVT_S32,
	MVT_S64,
	MVT_F32,
	MVT_F64,
	MVT_LLVector3,
	MVT_LLVector3d,
	MVT_LLVector4,
	MVT_LLQuaternion,
	MVT_LLUUID,	
	MVT_BOOL,
	MVT_IP_ADDR,
	MVT_IP_PORT,
	MVT_U16Vec3,
	MVT_U16Quat,
	MVT_S16Array,
	MVT_EOL
} EMsgVariableType;

// message system exceptional condition handlers.
enum EMessageException
{
	MX_UNREGISTERED_MESSAGE, // message number not part of template
	MX_PACKET_TOO_SHORT, // invalid packet, shorter than minimum packet size
	MX_RAN_OFF_END_OF_PACKET, // ran off the end of the packet during decode
	MX_WROTE_PAST_BUFFER_SIZE // wrote past buffer size in zero code expand
};
typedef void (*msg_exception_callback)(LLMessageSystem*,void*,EMessageException);



class LLMsgData;
class LLMsgBlkData;
class LLMessageTemplate;

class LLMessagePollInfo;

class LLMessageSystem
{
	LOG_CLASS(LLMessageSystem);
	
public:
	U8										mSendBuffer[MAX_BUFFER_SIZE];
	// Encoded send buffer needs to be slightly larger since the zero
	// coding can potentially increase the size of the send data.
	U8										mEncodedSendBuffer[2 * MAX_BUFFER_SIZE];
	S32										mSendSize;
	S32										mCurrentSendTotal;

	LLPacketRing							mPacketRing;
	LLReliablePacketParams					mReliablePacketParams;

	//LLLinkedList<LLPacketAck>				mAckList;

	// Set this flag to TRUE when you want *very* verbose logs.
	BOOL mVerboseLog;

	U32                                     mMessageFileChecksum;	
	F32                                     mMessageFileVersionNumber;

	typedef std::map<const char *, LLMessageTemplate*> message_template_name_map_t;
	typedef std::map<U32, LLMessageTemplate*> message_template_number_map_t;

private:
	message_template_name_map_t				mMessageTemplates;
	message_template_number_map_t			mMessageNumbers;

public:
	S32										mSystemVersionMajor;
	S32										mSystemVersionMinor;
	S32										mSystemVersionPatch;
	S32										mSystemVersionServer;
	U32										mVersionFlags;


	BOOL									mbProtected;

	U32										mNumberHighFreqMessages;
	U32										mNumberMediumFreqMessages;
	U32										mNumberLowFreqMessages;
	S32										mPort;
	S32										mSocket;

	U32										mPacketsIn;			// total packets in, including compressed and uncompressed
	U32										mPacketsOut;		// total packets out, including compressed and uncompressed

	U64										mBytesIn;			// total bytes in, including compressed and uncompressed
	U64										mBytesOut;			// total bytes out, including compressed and uncompressed
	
	U32										mCompressedPacketsIn;		// total compressed packets in
	U32										mCompressedPacketsOut;	    // total compressed packets out

	U32										mReliablePacketsIn;		    // total reliable packets in
	U32										mReliablePacketsOut;	    // total reliable packets out

	U32                                     mDroppedPackets;            // total dropped packets in
	U32                                     mResentPackets;             // total resent packets out
	U32                                     mFailedResendPackets;       // total resend failure packets out
	U32                                     mOffCircuitPackets;         // total # of off-circuit packets rejected
	U32                                     mInvalidOnCircuitPackets;   // total # of on-circuit but invalid packets rejected

	S64										mUncompressedBytesIn;		// total uncompressed size of compressed packets in
	S64										mUncompressedBytesOut;		// total uncompressed size of compressed packets out
	S64										mCompressedBytesIn;			// total compressed size of compressed packets in
	S64										mCompressedBytesOut;		// total compressed size of compressed packets out
	S64										mTotalBytesIn;			    // total size of all uncompressed packets in
	S64										mTotalBytesOut;				// total size of all uncompressed packets out

	BOOL                                    mSendReliable;              // does the outgoing message require a pos ack?

	LLCircuit								mCircuitInfo;
	F64										mCircuitPrintTime;			// used to print circuit debug info every couple minutes
	F32										mCircuitPrintFreq;			// seconds

	std::map<U64, U32>						mIPPortToCircuitCode;
	std::map<U32, U64>						mCircuitCodeToIPPort;
	U32										mOurCircuitCode;
	S32										mSendPacketFailureCount;
	S32										mUnackedListDepth;
	S32										mUnackedListSize;
	S32										mDSMaxListDepth;

public:
	// Read file and build message templates
	LLMessageSystem(const char *filename, U32 port, S32 version_major,
		S32 version_minor, S32 version_patch);

public:
	// Subclass use.
	LLMessageSystem();

public:
	virtual ~LLMessageSystem();

	BOOL isOK() const { return !mbError; }
	S32 getErrorCode() const { return mErrorCode; }

	// Read file and build message templates filename must point to a
	// valid string which specifies the path of a valid linden
	// template.
	void loadTemplateFile(const char* filename);


	// methods for building, sending, receiving, and handling messages
	void	setHandlerFuncFast(const char *name, void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data = NULL);
	void	setHandlerFunc(const char *name, void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data = NULL)
	{
		setHandlerFuncFast(gMessageStringTable.getString(name), handler_func, user_data);
	}

	bool callHandler(const char *name, bool trustedSource,
							LLMessageSystem* msg);

	// Set a callback function for a message system exception.
	void setExceptionFunc(EMessageException exception, msg_exception_callback func, void* data = NULL);
	// Call the specified exception func, and return TRUE if a
	// function was found and called. Otherwise return FALSE.
	BOOL callExceptionFunc(EMessageException exception);

	// This method returns true if the code is in the circuit codes map.
	BOOL isCircuitCodeKnown(U32 code) const;

	// usually called in response to an AddCircuitCode message, but
	// may also be called by the login process.
	bool addCircuitCode(U32 code, const LLUUID& session_id);

	BOOL	poll(F32 seconds); // Number of seconds that we want to block waiting for data, returns if data was received
	BOOL	checkMessages( S64 frame_count = 0 );
	void	processAcks();

	BOOL	isMessageFast(const char *msg);
	BOOL	isMessage(const char *msg)
	{
		return isMessageFast(gMessageStringTable.getString(msg));
	}

	void dumpPacketToLog();

	char	*getMessageName();

	const LLHost& getSender() const;
	U32		getSenderIP() const;			// getSender() is preferred
	U32		getSenderPort() const;		// getSender() is preferred

	// This method returns the uuid associated with the sender. The
	// UUID will be null if it is not yet known or is a server
	// circuit.
	const LLUUID& getSenderID() const;

	// This method returns the session id associated with the last
	// sender.
	const LLUUID& getSenderSessionID() const;

	// set & get the session id (useful for viewers for now.)
	void setMySessionID(const LLUUID& session_id) { mSessionID = session_id; }
	const LLUUID& getMySessionID() { return mSessionID; }

	virtual void newMessageFast(const char *name);
	void	newMessage(const char *name)
	{
		newMessageFast(gMessageStringTable.getString(name));
	}

	void	copyMessageRtoS();
	void	clearMessage();

	virtual void nextBlockFast(const char *blockname);
	void	nextBlock(const char *blockname)
	{
		nextBlockFast(gMessageStringTable.getString(blockname));
	}
private:
	void	addDataFast(const char *varname, const void *data, EMsgVariableType type, S32 size);	// Use only for types not in system already
	void	addData(const char *varname, const void *data, EMsgVariableType type, S32 size)
	{
		addDataFast(gMessageStringTable.getString(varname), data, type, size);
	}

	
	void	addDataFast(const char *varname, const void *data, EMsgVariableType type);				// DEPRECATED - not typed, doesn't check storage space
	void	addData(const char *varname, const void *data, EMsgVariableType type)
	{
		addDataFast(gMessageStringTable.getString(varname), data, type);
	}
public:
	void	addBinaryDataFast(const char *varname, const void *data, S32 size)
	{
		addDataFast(varname, data, MVT_FIXED, size);
	}
	void	addBinaryData(const char *varname, const void *data, S32 size)
	{
		addDataFast(gMessageStringTable.getString(varname), data, MVT_FIXED, size);
	}

	void	addBOOLFast( const char* varname, BOOL b);						// typed, checks storage space
	void	addBOOL( const char* varname, BOOL b);						// typed, checks storage space
	void	addS8Fast(	const char *varname, S8 s);							// typed, checks storage space
	void	addS8(	const char *varname, S8 s);							// typed, checks storage space
	void	addU8Fast(	const char *varname, U8 u);							// typed, checks storage space
	void	addU8(	const char *varname, U8 u);							// typed, checks storage space
	void	addS16Fast(	const char *varname, S16 i);						// typed, checks storage space
	void	addS16(	const char *varname, S16 i);						// typed, checks storage space
	void	addU16Fast(	const char *varname, U16 i);						// typed, checks storage space
	void	addU16(	const char *varname, U16 i);						// typed, checks storage space
	void	addF32Fast(	const char *varname, F32 f);						// typed, checks storage space
	void	addF32(	const char *varname, F32 f);						// typed, checks storage space
	void	addS32Fast(	const char *varname, S32 s);						// typed, checks storage space
	void	addS32(	const char *varname, S32 s);						// typed, checks storage space
	virtual void addU32Fast(	const char *varname, U32 u);						// typed, checks storage space
	void	addU32(	const char *varname, U32 u);						// typed, checks storage space
	void	addU64Fast(	const char *varname, U64 lu);						// typed, checks storage space
	void	addU64(	const char *varname, U64 lu);						// typed, checks storage space
	void	addF64Fast(	const char *varname, F64 d);						// typed, checks storage space
	void	addF64(	const char *varname, F64 d);						// typed, checks storage space
	void	addVector3Fast(	const char *varname, const LLVector3& vec);		// typed, checks storage space
	void	addVector3(	const char *varname, const LLVector3& vec);		// typed, checks storage space
	void	addVector4Fast(	const char *varname, const LLVector4& vec);		// typed, checks storage space
	void	addVector4(	const char *varname, const LLVector4& vec);		// typed, checks storage space
	void	addVector3dFast( const char *varname, const LLVector3d& vec);	// typed, checks storage space
	void	addVector3d( const char *varname, const LLVector3d& vec);	// typed, checks storage space
	void	addQuatFast( const char *varname, const LLQuaternion& quat);	// typed, checks storage space
	void	addQuat( const char *varname, const LLQuaternion& quat);	// typed, checks storage space
	virtual void addUUIDFast( const char *varname, const LLUUID& uuid);			// typed, checks storage space
	void	addUUID( const char *varname, const LLUUID& uuid);			// typed, checks storage space
	void	addIPAddrFast( const char *varname, const U32 ip);			// typed, checks storage space
	void	addIPAddr( const char *varname, const U32 ip);			// typed, checks storage space
	void	addIPPortFast( const char *varname, const U16 port);			// typed, checks storage space
	void	addIPPort( const char *varname, const U16 port);			// typed, checks storage space
	void	addStringFast( const char* varname, const char* s);				// typed, checks storage space
	void	addString( const char* varname, const char* s);				// typed, checks storage space
	void	addStringFast( const char* varname, const std::string& s);				// typed, checks storage space
	void	addString( const char* varname, const std::string& s);				// typed, checks storage space

	TPACKETID getCurrentRecvPacketID() { return mCurrentRecvPacketID; }
	S32 getCurrentSendTotal() const { return mCurrentSendTotal; }

	// This method checks for current send total and returns true if
	// you need to go to the next block type or need to start a new
	// message. Specify the current blockname to check block counts,
	// otherwise the method only checks against MTU.
	BOOL isSendFull(const char* blockname = NULL);
	BOOL isSendFullFast(const char* blockname = NULL);

	BOOL	removeLastBlock();

	void	buildMessage();

	S32     zeroCode(U8 **data, S32 *data_size);
	S32		zeroCodeExpand(U8 **data, S32 *data_size);
	S32		zeroCodeAdjustCurrentSendTotal();

	// Uses ping-based retry
	virtual S32 sendReliable(const LLHost &host);

	// Uses ping-based retry
	S32	sendReliable(const U32 circuit)			{ return sendReliable(findHost(circuit)); }

	// Use this one if you DON'T want automatic ping-based retry.
	S32	sendReliable(	const LLHost &host, 
							S32 retries, 
							BOOL ping_based_retries,
							F32 timeout, 
							void (*callback)(void **,S32), 
							void ** callback_data);

	S32 sendSemiReliable(	const LLHost &host, 
							void (*callback)(void **,S32), void ** callback_data);

	// flush sends a message only if data's been pushed on it.
	S32		flushSemiReliable(	const LLHost &host, 
								void (*callback)(void **,S32), void ** callback_data);

	S32		flushReliable(	const LLHost &host );

	void    forwardMessage(const LLHost &host);
	void    forwardReliable(const LLHost &host);
	void    forwardReliable(const U32 circuit_code);

	S32		sendMessage(const LLHost &host);
	S32		sendMessage(const U32 circuit);

	BOOL	decodeData(const U8 *buffer, const LLHost &host);

	// TODO: Consolide these functions
	// TODO: Make these private, force use of typed functions.
	// If size is not 0, an error is generated if size doesn't exactly match the size of the data.
	// At all times, the number if bytes written to *datap is <= max_size.
private:
	void	getDataFast(const char *blockname, const char *varname, void *datap, S32 size = 0, S32 blocknum = 0, S32 max_size = S32_MAX);
	void	getData(const char *blockname, const char *varname, void *datap, S32 size = 0, S32 blocknum = 0, S32 max_size = S32_MAX)
	{
		getDataFast(gMessageStringTable.getString(blockname), gMessageStringTable.getString(varname), datap, size, blocknum, max_size);
	}
public:
	void	getBinaryDataFast(const char *blockname, const char *varname, void *datap, S32 size, S32 blocknum = 0, S32 max_size = S32_MAX)
	{
		getDataFast(blockname, varname, datap, size, blocknum, max_size);
	}
	void	getBinaryData(const char *blockname, const char *varname, void *datap, S32 size, S32 blocknum = 0, S32 max_size = S32_MAX)
	{
		getDataFast(gMessageStringTable.getString(blockname), gMessageStringTable.getString(varname), datap, size, blocknum, max_size);
	}

	void	getBOOLFast(	const char *block, const char *var, BOOL &data, S32 blocknum = 0);
	void	getBOOL(	const char *block, const char *var, BOOL &data, S32 blocknum = 0);
	void	getS8Fast(		const char *block, const char *var, S8 &data, S32 blocknum = 0);
	void	getS8(		const char *block, const char *var, S8 &data, S32 blocknum = 0);
	void	getU8Fast(		const char *block, const char *var, U8 &data, S32 blocknum = 0);
	void	getU8(		const char *block, const char *var, U8 &data, S32 blocknum = 0);
	void	getS16Fast(		const char *block, const char *var, S16 &data, S32 blocknum = 0);
	void	getS16(		const char *block, const char *var, S16 &data, S32 blocknum = 0);
	void	getU16Fast(		const char *block, const char *var, U16 &data, S32 blocknum = 0);
	void	getU16(		const char *block, const char *var, U16 &data, S32 blocknum = 0);
	void	getS32Fast(		const char *block, const char *var, S32 &data, S32 blocknum = 0);
	void	getS32(		const char *block, const char *var, S32 &data, S32 blocknum = 0);
	void	getF32Fast(		const char *block, const char *var, F32 &data, S32 blocknum = 0);
	void	getF32(		const char *block, const char *var, F32 &data, S32 blocknum = 0);
	virtual void getU32Fast(		const char *block, const char *var, U32 &data, S32 blocknum = 0);
	void	getU32(		const char *block, const char *var, U32 &data, S32 blocknum = 0);
	virtual void getU64Fast(		const char *block, const char *var, U64 &data, S32 blocknum = 0);
	void	getU64(		const char *block, const char *var, U64 &data, S32 blocknum = 0);
	void	getF64Fast(		const char *block, const char *var, F64 &data, S32 blocknum = 0);
	void	getF64(		const char *block, const char *var, F64 &data, S32 blocknum = 0);
	void	getVector3Fast(	const char *block, const char *var, LLVector3 &vec, S32 blocknum = 0);
	void	getVector3(	const char *block, const char *var, LLVector3 &vec, S32 blocknum = 0);
	void	getVector4Fast(	const char *block, const char *var, LLVector4 &vec, S32 blocknum = 0);
	void	getVector4(	const char *block, const char *var, LLVector4 &vec, S32 blocknum = 0);
	void	getVector3dFast(const char *block, const char *var, LLVector3d &vec, S32 blocknum = 0);
	void	getVector3d(const char *block, const char *var, LLVector3d &vec, S32 blocknum = 0);
	void	getQuatFast(	const char *block, const char *var, LLQuaternion &q, S32 blocknum = 0);
	void	getQuat(	const char *block, const char *var, LLQuaternion &q, S32 blocknum = 0);
	virtual void getUUIDFast(	const char *block, const char *var, LLUUID &uuid, S32 blocknum = 0);
	void	getUUID(	const char *block, const char *var, LLUUID &uuid, S32 blocknum = 0);
	virtual void getIPAddrFast(	const char *block, const char *var, U32 &ip, S32 blocknum = 0);
	void	getIPAddr(	const char *block, const char *var, U32 &ip, S32 blocknum = 0);
	virtual void getIPPortFast(	const char *block, const char *var, U16 &port, S32 blocknum = 0);
	void	getIPPort(	const char *block, const char *var, U16 &port, S32 blocknum = 0);
	virtual void getStringFast(	const char *block, const char *var, S32 buffer_size, char *buffer, S32 blocknum = 0);
	void	getString(	const char *block, const char *var, S32 buffer_size, char *buffer, S32 blocknum = 0);


	// Utility functions to generate a replay-resistant digest check
	// against the shared secret. The window specifies how much of a
	// time window is allowed - 1 second is good for tight
	// connections, but multi-process windows might want to be upwards
	// of 5 seconds. For generateDigest, you want to pass in a
	// character array of at least MD5HEX_STR_SIZE so that the hex
	// digest and null termination will fit.
	bool generateDigestForNumberAndUUIDs(char* digest, const U32 number, const LLUUID &id1, const LLUUID &id2) const;
	bool generateDigestForWindowAndUUIDs(char* digest, const S32 window, const LLUUID &id1, const LLUUID &id2) const;
	bool isMatchingDigestForWindowAndUUIDs(const char* digest, const S32 window, const LLUUID &id1, const LLUUID &id2) const;

	bool generateDigestForNumber(char* digest, const U32 number) const;
	bool generateDigestForWindow(char* digest, const S32 window) const;
	bool isMatchingDigestForWindow(const char* digest, const S32 window) const;

	void	showCircuitInfo();
	LLString getCircuitInfoString();

	virtual U32 getOurCircuitCode();
	
	void	enableCircuit(const LLHost &host, BOOL trusted);
	void	disableCircuit(const LLHost &host);
	
	// Use this to establish trust on startup and in response to
	// DenyTrustedCircuit.
	void sendCreateTrustedCircuit(const LLHost& host, const LLUUID & id1, const LLUUID & id2);

	// Use this to inform a peer that they aren't currently trusted...
	// This now enqueues the request so that we can ensure that we only send
	// one deny per circuit per message loop so that this doesn't become a DoS.
	// The actual sending is done by reallySendDenyTrustedCircuit()
	void	sendDenyTrustedCircuit(const LLHost &host);

private:
	// A list of the circuits that need to be sent DenyTrustedCircuit messages.
	typedef std::set<LLHost> host_set_t;
	host_set_t mDenyTrustedCircuitSet;

	// Really sends the DenyTrustedCircuit message to a given host
	// related to sendDenyTrustedCircuit()
	void	reallySendDenyTrustedCircuit(const LLHost &host);


public:
	// Use this to establish trust to and from a host.  This blocks
	// until trust has been established, and probably should only be
	// used on startup.
	void	establishBidirectionalTrust(const LLHost &host, S64 frame_count = 0);

	// returns whether the given host is on a trusted circuit
	BOOL    getCircuitTrust(const LLHost &host);
	
	void	setCircuitAllowTimeout(const LLHost &host, BOOL allow);
	void	setCircuitTimeoutCallback(const LLHost &host, void (*callback_func)(const LLHost &host, void *user_data), void *user_data);

	BOOL	checkCircuitBlocked(const U32 circuit);
	BOOL	checkCircuitAlive(const U32 circuit);
	BOOL	checkCircuitAlive(const LLHost &host);
	void	setCircuitProtection(BOOL b_protect);
	U32		findCircuitCode(const LLHost &host);
	LLHost	findHost(const U32 circuit_code);
	void	sanityCheck();

	S32		getNumberOfBlocksFast(const char *blockname);
	S32		getNumberOfBlocks(const char *blockname)
	{
		return getNumberOfBlocksFast(gMessageStringTable.getString(blockname));
	}
	S32		getSizeFast(const char *blockname, const char *varname);
	S32		getSize(const char *blockname, const char *varname)
	{
		return getSizeFast(gMessageStringTable.getString(blockname), gMessageStringTable.getString(varname));
	}
	S32		getSizeFast(const char *blockname, S32 blocknum, const char *varname);		// size in bytes of variable length data
	S32		getSize(const char *blockname, S32 blocknum, const char *varname)
	{
		return getSizeFast(gMessageStringTable.getString(blockname), blocknum, gMessageStringTable.getString(varname));
	}

	void	resetReceiveCounts();				// resets receive counts for all message types to 0
	void	dumpReceiveCounts();				// dumps receive count for each message type to llinfos
	void	dumpCircuitInfo();					// Circuit information to llinfos

	BOOL	isClear() const;					// returns mbSClear;
	S32 	flush(const LLHost &host);

	U32		getListenPort( void ) const;

	void startLogging();					// start verbose  logging
	void stopLogging();						// flush and close file
	void summarizeLogs(std::ostream& str);	// log statistics

	S32		getReceiveSize() const				{ return mReceiveSize; }
	S32		getReceiveCompressedSize() const	{ return mIncomingCompressedSize; }
	S32		getReceiveBytes() const;

	S32		getUnackedListSize() const			{ return mUnackedListSize; }

	const char* getCurrentSMessageName() const { return mCurrentSMessageName; }
	const char* getCurrentSBlockName() const { return mCurrentSBlockName; }

	// friends
	friend std::ostream&	operator<<(std::ostream& s, LLMessageSystem &msg);

	void setMaxMessageTime(const F32 seconds);	// Max time to process messages before warning and dumping (neg to disable)
	void setMaxMessageCounts(const S32 num);	// Max number of messages before dumping (neg to disable)
	
	// statics
public:
	static U64 getMessageTimeUsecs(const BOOL update = FALSE);	// Get the current message system time in microseconds
	static F64 getMessageTimeSeconds(const BOOL update = FALSE); // Get the current message system time in seconds

	static void setTimeDecodes( BOOL b )		
		{ LLMessageSystem::mTimeDecodes = b; }

	static void setTimeDecodesSpamThreshold( F32 seconds ) 
		{ LLMessageSystem::mTimeDecodesSpamThreshold = seconds; }

	// message handlers internal to the message systesm
	//static void processAssignCircuitCode(LLMessageSystem* msg, void**);
	static void processAddCircuitCode(LLMessageSystem* msg, void**);
	static void processUseCircuitCode(LLMessageSystem* msg, void**);

	void setMessageBans(const LLSD& trusted, const LLSD& untrusted);
	
private:
	// data used in those internal handlers

	// The mCircuitCodes is a map from circuit codes to session
	// ids. This allows us to verify sessions on connect.
	typedef std::map<U32, LLUUID> code_session_map_t;
	code_session_map_t mCircuitCodes;

	// Viewers need to track a process session in order to make sure
	// that no one gives them a bad circuit code.
	LLUUID mSessionID;

private:
	void	addTemplate(LLMessageTemplate *templatep);
	void		clearReceiveState();
	BOOL		decodeTemplate( const U8* buffer, S32 buffer_size, LLMessageTemplate** msg_template );

	void		logMsgFromInvalidCircuit( const LLHost& sender, BOOL recv_reliable );
	void		logTrustedMsgFromUntrustedCircuit( const LLHost& sender );
	void		logValidMsg(LLCircuitData *cdp, const LLHost& sender, BOOL recv_reliable, BOOL recv_resent, BOOL recv_acks );
	void		logRanOffEndOfPacket( const LLHost& sender );

private:
	class LLMessageCountInfo
	{
	public:
		U32 mMessageNum;
		U32 mMessageBytes;
		BOOL mInvalid;
	};

	LLMessagePollInfo						*mPollInfop;

	U8										mEncodedRecvBuffer[MAX_BUFFER_SIZE];
	U8										mTrueReceiveBuffer[MAX_BUFFER_SIZE];
	S32										mTrueReceiveSize;

	// Must be valid during decode
	S32										mReceiveSize;
	TPACKETID                               mCurrentRecvPacketID;       // packet ID of current receive packet (for reporting)
	LLMessageTemplate						*mCurrentRMessageTemplate;
	LLMsgData								*mCurrentRMessageData;
	S32									    mIncomingCompressedSize;		// original size of compressed msg (0 if uncomp.)
	LLHost									mLastSender;

	// send message storage
	LLMsgData								*mCurrentSMessageData;
	LLMessageTemplate						*mCurrentSMessageTemplate;
	LLMsgBlkData							*mCurrentSDataBlock;
	char									*mCurrentSMessageName;
	char									*mCurrentSBlockName;

	BOOL									mbError;
	S32 mErrorCode;

	BOOL									mbSBuilt;	// is send message built?
	BOOL									mbSClear;	// is the send message clear?

	F64										mResendDumpTime; // The last time we dumped resends

	LLMessageCountInfo mMessageCountList[MAX_MESSAGE_COUNT_NUM];
	S32 mNumMessageCounts;
	F32 mReceiveTime;
	F32 mMaxMessageTime; // Max number of seconds for processing messages
	S32 mMaxMessageCounts; // Max number of messages to process before dumping.
	F64 mMessageCountTime;

	F64 mCurrentMessageTimeSeconds; // The current "message system time" (updated the first call to checkMessages after a resetReceiveCount

	// message system exceptions
	typedef std::pair<msg_exception_callback, void*> exception_t;
	typedef std::map<EMessageException, exception_t> callbacks_t;
	callbacks_t mExceptionCallbacks;

	// stuff for logging
	LLTimer mMessageSystemTimer;

	static F32 mTimeDecodesSpamThreshold;  // If mTimeDecodes is on, all this many seconds for each msg decode before spamming
	static BOOL mTimeDecodes;  // Measure time for all message decodes if TRUE;

	void init(); // ctor shared initialisation.
};


// external hook into messaging system
extern LLMessageSystem	*gMessageSystem;
//extern const char* MESSAGE_LOG_FILENAME;

void encrypt_template(const char *src_name, const char *dest_name);
BOOL decrypt_template(const char *src_name, const char *dest_name);

// Must specific overall system version, which is used to determine
// if a patch is available in the message template checksum verification.
// Return TRUE if able to initialize system.
BOOL start_messaging_system(
	const std::string& template_name,
	U32 port, 
	S32 version_major,
	S32 version_minor,
	S32 version_patch,
	BOOL b_dump_prehash_file,
	const std::string& secret);

void end_messaging_system();

void null_message_callback(LLMessageSystem *msg, void **data);

//
// Inlines
//

static inline void *htonmemcpy(void *vs, const void *vct, EMsgVariableType type, size_t n)
{
	char *s = (char *)vs;
	const char *ct = (const char *)vct;
#ifdef LL_BIG_ENDIAN
	S32 i, length;
#endif
	switch(type)
	{
	case MVT_FIXED:
	case MVT_VARIABLE:
	case MVT_U8:
	case MVT_S8:
	case MVT_BOOL:
	case MVT_LLUUID:
	case MVT_IP_ADDR:	// these two are swizzled in the getters and setters
	case MVT_IP_PORT:	// these two are swizzled in the getters and setters
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 

	case MVT_U16:
	case MVT_S16:
		if (n != 2)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		*(s + 1) = *(ct);
		*(s) = *(ct + 1);
		return(vs);
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_U32:
	case MVT_S32:
	case MVT_F32:
		if (n != 4)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		*(s + 3) = *(ct);
		*(s + 2) = *(ct + 1);
		*(s + 1) = *(ct + 2);
		*(s) = *(ct + 3);
		return(vs);
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_U64:
	case MVT_S64:
	case MVT_F64:
		if (n != 8)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		*(s + 7) = *(ct);
		*(s + 6) = *(ct + 1);
		*(s + 5) = *(ct + 2);
		*(s + 4) = *(ct + 3);
		*(s + 3) = *(ct + 4);
		*(s + 2) = *(ct + 5);
		*(s + 1) = *(ct + 6);
		*(s) = *(ct + 7);
		return(vs);
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_LLVector3:
	case MVT_LLQuaternion:  // We only send x, y, z and infer w (we set x, y, z to ensure that w >= 0)
		if (n != 12)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		htonmemcpy(s + 8, ct + 8, MVT_F32, 4);
		htonmemcpy(s + 4, ct + 4, MVT_F32, 4);
		return(htonmemcpy(s, ct, MVT_F32, 4));
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_LLVector3d:
		if (n != 24)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		htonmemcpy(s + 16, ct + 16, MVT_F64, 8);
		htonmemcpy(s + 8, ct + 8, MVT_F64, 8);
		return(htonmemcpy(s, ct, MVT_F64, 8));
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_LLVector4:
		if (n != 16)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		htonmemcpy(s + 12, ct + 12, MVT_F32, 4);
		htonmemcpy(s + 8, ct + 8, MVT_F32, 4);
		htonmemcpy(s + 4, ct + 4, MVT_F32, 4);
		return(htonmemcpy(s, ct, MVT_F32, 4));
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_U16Vec3:
		if (n != 6)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		htonmemcpy(s + 4, ct + 4, MVT_U16, 2);
		htonmemcpy(s + 2, ct + 2, MVT_U16, 2);
		return(htonmemcpy(s, ct, MVT_U16, 2));
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_U16Quat:
		if (n != 8)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		htonmemcpy(s + 6, ct + 6, MVT_U16, 2);
		htonmemcpy(s + 4, ct + 4, MVT_U16, 2);
		htonmemcpy(s + 2, ct + 2, MVT_U16, 2);
		return(htonmemcpy(s, ct, MVT_U16, 2));
#else
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
#endif

	case MVT_S16Array:
		if (n % 2)
		{
			llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
		}
#ifdef LL_BIG_ENDIAN
		length = n % 2;
		for (i = 1; i < length; i++)
		{
			htonmemcpy(s + i*2, ct + i*2, MVT_S16, 2);
		}
		return(htonmemcpy(s, ct, MVT_S16, 2));
#else
		return(memcpy(s,ct,n));
#endif

	default:
		return(memcpy(s,ct,n));	/* Flawfinder: ignore */ 
	}
}

inline void *ntohmemcpy(void *s, const void *ct, EMsgVariableType type, size_t n)
{
	return(htonmemcpy(s,ct,type, n));
}


inline const LLHost& LLMessageSystem::getSender() const
{
	return mLastSender;
}

inline U32 LLMessageSystem::getSenderIP() const
{
	return mLastSender.getAddress();
}

inline U32 LLMessageSystem::getSenderPort() const
{
	return mLastSender.getPort();
}

inline void LLMessageSystem::addS8Fast(const char *varname, S8 s)
{
	addDataFast(varname, &s, MVT_S8, sizeof(s));
}

inline void LLMessageSystem::addS8(const char *varname, S8 s)
{
	addDataFast(gMessageStringTable.getString(varname), &s, MVT_S8, sizeof(s));
}

inline void LLMessageSystem::addU8Fast(const char *varname, U8 u)
{
	addDataFast(varname, &u, MVT_U8, sizeof(u));
}

inline void LLMessageSystem::addU8(const char *varname, U8 u)
{
	addDataFast(gMessageStringTable.getString(varname), &u, MVT_U8, sizeof(u));
}

inline void LLMessageSystem::addS16Fast(const char *varname, S16 i)
{
	addDataFast(varname, &i, MVT_S16, sizeof(i));
}

inline void LLMessageSystem::addS16(const char *varname, S16 i)
{
	addDataFast(gMessageStringTable.getString(varname), &i, MVT_S16, sizeof(i));
}

inline void LLMessageSystem::addU16Fast(const char *varname, U16 i)
{
	addDataFast(varname, &i, MVT_U16, sizeof(i));
}

inline void LLMessageSystem::addU16(const char *varname, U16 i)
{
	addDataFast(gMessageStringTable.getString(varname), &i, MVT_U16, sizeof(i));
}

inline void LLMessageSystem::addF32Fast(const char *varname, F32 f)
{
	addDataFast(varname, &f, MVT_F32, sizeof(f));
}

inline void LLMessageSystem::addF32(const char *varname, F32 f)
{
	addDataFast(gMessageStringTable.getString(varname), &f, MVT_F32, sizeof(f));
}

inline void LLMessageSystem::addS32Fast(const char *varname, S32 s)
{
	addDataFast(varname, &s, MVT_S32, sizeof(s));
}

inline void LLMessageSystem::addS32(const char *varname, S32 s)
{
	addDataFast(gMessageStringTable.getString(varname), &s, MVT_S32, sizeof(s));
}

inline void LLMessageSystem::addU32Fast(const char *varname, U32 u)
{
	addDataFast(varname, &u, MVT_U32, sizeof(u));
}

inline void LLMessageSystem::addU32(const char *varname, U32 u)
{
	addDataFast(gMessageStringTable.getString(varname), &u, MVT_U32, sizeof(u));
}

inline void LLMessageSystem::addU64Fast(const char *varname, U64 lu)
{
	addDataFast(varname, &lu, MVT_U64, sizeof(lu));
}

inline void LLMessageSystem::addU64(const char *varname, U64 lu)
{
	addDataFast(gMessageStringTable.getString(varname), &lu, MVT_U64, sizeof(lu));
}

inline void LLMessageSystem::addF64Fast(const char *varname, F64 d)
{
	addDataFast(varname, &d, MVT_F64, sizeof(d));
}

inline void LLMessageSystem::addF64(const char *varname, F64 d)
{
	addDataFast(gMessageStringTable.getString(varname), &d, MVT_F64, sizeof(d));
}

inline void LLMessageSystem::addIPAddrFast(const char *varname, U32 u)
{
	addDataFast(varname, &u, MVT_IP_ADDR, sizeof(u));
}

inline void LLMessageSystem::addIPAddr(const char *varname, U32 u)
{
	addDataFast(gMessageStringTable.getString(varname), &u, MVT_IP_ADDR, sizeof(u));
}

inline void LLMessageSystem::addIPPortFast(const char *varname, U16 u)
{
	u = htons(u);
	addDataFast(varname, &u, MVT_IP_PORT, sizeof(u));
}

inline void LLMessageSystem::addIPPort(const char *varname, U16 u)
{
	u = htons(u);
	addDataFast(gMessageStringTable.getString(varname), &u, MVT_IP_PORT, sizeof(u));
}

inline void LLMessageSystem::addBOOLFast(const char* varname, BOOL b)
{
	// Can't just cast a BOOL (actually a U32) to a U8.
	// In some cases the low order bits will be zero.
	U8 temp = (b != 0);
	addDataFast(varname, &temp, MVT_BOOL, sizeof(temp));
}

inline void LLMessageSystem::addBOOL(const char* varname, BOOL b)
{
	// Can't just cast a BOOL (actually a U32) to a U8.
	// In some cases the low order bits will be zero.
	U8 temp = (b != 0);
	addDataFast(gMessageStringTable.getString(varname), &temp, MVT_BOOL, sizeof(temp));
}

inline void LLMessageSystem::addStringFast(const char* varname, const char* s)
{
	if (s)
		addDataFast( varname, (void *)s, MVT_VARIABLE, (S32)strlen(s) + 1);  /* Flawfinder: ignore */  
	else
		addDataFast( varname, NULL, MVT_VARIABLE, 0); 
}

inline void LLMessageSystem::addString(const char* varname, const char* s)
{
	if (s)
		addDataFast( gMessageStringTable.getString(varname), (void *)s, MVT_VARIABLE, (S32)strlen(s) + 1);  /* Flawfinder: ignore */ 
	else
		addDataFast( gMessageStringTable.getString(varname), NULL, MVT_VARIABLE, 0); 
}

inline void LLMessageSystem::addStringFast(const char* varname, const std::string& s)
{
	if (s.size())
		addDataFast( varname, (void *)s.c_str(), MVT_VARIABLE, (S32)(s.size()) + 1); 
	else
		addDataFast( varname, NULL, MVT_VARIABLE, 0); 
}

inline void LLMessageSystem::addString(const char* varname, const std::string& s)
{
	if (s.size())
		addDataFast( gMessageStringTable.getString(varname), (void *)s.c_str(), MVT_VARIABLE, (S32)(s.size()) + 1); 
	else
		addDataFast( gMessageStringTable.getString(varname), NULL, MVT_VARIABLE, 0); 
}


//-----------------------------------------------------------------------------
// Retrieval aliases
//-----------------------------------------------------------------------------
inline void LLMessageSystem::getS8Fast(const char *block, const char *var, S8 &u, S32 blocknum)
{
	getDataFast(block, var, &u, sizeof(S8), blocknum);
}

inline void LLMessageSystem::getS8(const char *block, const char *var, S8 &u, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &u, sizeof(S8), blocknum);
}

inline void LLMessageSystem::getU8Fast(const char *block, const char *var, U8 &u, S32 blocknum)
{
	getDataFast(block, var, &u, sizeof(U8), blocknum);
}

inline void LLMessageSystem::getU8(const char *block, const char *var, U8 &u, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &u, sizeof(U8), blocknum);
}

inline void LLMessageSystem::getBOOLFast(const char *block, const char *var, BOOL &b, S32 blocknum )
{
	U8 value;
	getDataFast(block, var, &value, sizeof(U8), blocknum);
	b = (BOOL) value;
}

inline void LLMessageSystem::getBOOL(const char *block, const char *var, BOOL &b, S32 blocknum )
{
	U8 value;
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &value, sizeof(U8), blocknum);
	b = (BOOL) value;
}

inline void LLMessageSystem::getS16Fast(const char *block, const char *var, S16 &d, S32 blocknum)
{
	getDataFast(block, var, &d, sizeof(S16), blocknum);
}

inline void LLMessageSystem::getS16(const char *block, const char *var, S16 &d, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &d, sizeof(S16), blocknum);
}

inline void LLMessageSystem::getU16Fast(const char *block, const char *var, U16 &d, S32 blocknum)
{
	getDataFast(block, var, &d, sizeof(U16), blocknum);
}

inline void LLMessageSystem::getU16(const char *block, const char *var, U16 &d, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &d, sizeof(U16), blocknum);
}

inline void LLMessageSystem::getS32Fast(const char *block, const char *var, S32 &d, S32 blocknum)
{
	getDataFast(block, var, &d, sizeof(S32), blocknum);
}

inline void LLMessageSystem::getS32(const char *block, const char *var, S32 &d, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &d, sizeof(S32), blocknum);
}

inline void LLMessageSystem::getU32Fast(const char *block, const char *var, U32 &d, S32 blocknum)
{
	getDataFast(block, var, &d, sizeof(U32), blocknum);
}

inline void LLMessageSystem::getU32(const char *block, const char *var, U32 &d, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &d, sizeof(U32), blocknum);
}

inline void LLMessageSystem::getU64Fast(const char *block, const char *var, U64 &d, S32 blocknum)
{
	getDataFast(block, var, &d, sizeof(U64), blocknum);
}

inline void LLMessageSystem::getU64(const char *block, const char *var, U64 &d, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &d, sizeof(U64), blocknum);
}


inline void LLMessageSystem::getIPAddrFast(const char *block, const char *var, U32 &u, S32 blocknum)
{
	getDataFast(block, var, &u, sizeof(U32), blocknum);
}

inline void LLMessageSystem::getIPAddr(const char *block, const char *var, U32 &u, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &u, sizeof(U32), blocknum);
}

inline void LLMessageSystem::getIPPortFast(const char *block, const char *var, U16 &u, S32 blocknum)
{
	getDataFast(block, var, &u, sizeof(U16), blocknum);
	u = ntohs(u);
}

inline void LLMessageSystem::getIPPort(const char *block, const char *var, U16 &u, S32 blocknum)
{
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), &u, sizeof(U16), blocknum);
	u = ntohs(u);
}


inline void LLMessageSystem::getStringFast(const char *block, const char *var, S32 buffer_size, char *s, S32 blocknum )
{
	s[0] = '\0';
	getDataFast(block, var, s, 0, blocknum, buffer_size);
	s[buffer_size - 1] = '\0';
}

inline void LLMessageSystem::getString(const char *block, const char *var, S32 buffer_size, char *s, S32 blocknum )
{
	s[0] = '\0';
	getDataFast(gMessageStringTable.getString(block), gMessageStringTable.getString(var), s, 0, blocknum, buffer_size);
	s[buffer_size - 1] = '\0';
}

//-----------------------------------------------------------------------------
// Transmission aliases
//-----------------------------------------------------------------------------
//inline S32 LLMessageSystem::sendMessage(U32 ip, U32 port, BOOL zero_code)
//{
//	return sendMessage(LLHost(ip, port), zero_code);
//}

//inline S32 LLMessageSystem::sendMessage(const char *ip_str, U32 port, BOOL zero_code)
//{
//	return sendMessage(LLHost(ip_str, port), zero_code);
//}

inline S32 LLMessageSystem::sendMessage(const U32 circuit)//, BOOL zero_code)
{
	return sendMessage(findHost(circuit));//, zero_code);
}

#endif