SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
peer.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
19
20
21 // TODO - RakNet 4 - Add network simulator
22 // TODO - RakNet 4 - Enable disabling flow control per connections
23
24 #ifndef __RAK_PEER_H
25 #define __RAK_PEER_H
26
27 #include "ReliabilityLayer.h"
28 #include "peerinterface.h"
29 #include "BitStream.h"
30 #include "SingleProducerConsumer.h"
31 #include "SimpleMutex.h"
32 #include "DS_OrderedList.h"
33 #include "Export.h"
34 #include "string.h"
35 #include "thread.h"
36 //#include "socket.h"
37 #include "smartptr.h"
38 #include "DS_ThreadsafeAllocatingQueue.h"
39 #include "SignaledEvent.h"
40 #include "NativeFeatureIncludes.h"
41 #include "SecureHandshake.h"
42 #include "LocklessTypes.h"
43 #include "DS_Queue.h"
44
45 namespace SLNet {
47 class HuffmanEncodingTree;
48 class PluginInterface2;
49
50 // Sucks but this struct has to be outside the class. Inside and DevCPP won't let you refer to the struct as RakPeer::RemoteSystemIndex while GCC
51 // forces you to do RakPeer::RemoteSystemIndex
52 struct RemoteSystemIndex{unsigned index; RemoteSystemIndex *next;};
53 //int RAK_DLL_EXPORT SystemAddressAndIndexComp( const SystemAddress &key, const RemoteSystemIndex &data ); // GCC requires RakPeer::RemoteSystemIndex or it won't compile
54
62 class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler
63 {
64 public:
66 RakPeer();
67
69 virtual ~RakPeer();
70
71 // --------------------------------------------------------------------------------------------Major Low Level Functions - Functions needed by most users--------------------------------------------------------------------------------------------
83 StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 );
84
94 bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false );
95
98 void DisableSecurity( void );
99
105 void AddToSecurityExceptionList(const char *ip);
106
109 void RemoveFromSecurityExceptionList(const char *ip);
110
114 bool IsInSecurityExceptionList(const char *ip);
115
123 void SetMaximumIncomingConnections( unsigned short numberAllowed );
124
127 unsigned int GetMaximumIncomingConnections( void ) const;
128
131 unsigned short NumberOfConnections(void) const;
132
139 void SetIncomingPassword( const char* passwordData, int passwordDataLength );
140
144 void GetIncomingPassword( char* passwordData, int *passwordDataLength );
145
167 ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, SLNet::TimeMS timeoutTime=0 );
168
180 virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, SLNet::TimeMS timeoutTime=0);
181
182 /* /// \brief Connect to the specified network ID (Platform specific console function)
187 //bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength );*/
188
194 void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY );
195
198 bool IsActive( void ) const;
199
203 bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const;
204
208 virtual uint32_t GetNextSendReceipt(void);
209
213 virtual uint32_t IncrementNextSendReceipt(void);
214
227 uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
228
235 void SendLoopback( const char *data, const int length );
236
249 uint32_t Send( const SLNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
250
271 uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
272
279 Packet* Receive( void );
280
283 void DeallocatePacket( Packet *packet );
284
287 unsigned int GetMaximumNumberOfPeers( void ) const;
288
289 // -------------------------------------------------------------------------------------------- Connection Management Functions--------------------------------------------------------------------------------------------
296 void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY );
297
301 void CancelConnectionAttempt( const SystemAddress target );
306 ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier);
307
313 int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const;
314
320 SystemAddress GetSystemAddressFromIndex( unsigned int index );
321
325 RakNetGUID GetGUIDFromIndex( unsigned int index );
326
331 void GetSystemList(DataStructures::List<SystemAddress> &addresses, DataStructures::List<RakNetGUID> &guids) const;
332
337 void AddToBanList( const char *IP, SLNet::TimeMS milliseconds=0 );
338
341 void RemoveFromBanList( const char *IP );
342
344 void ClearBanList( void );
345
349 bool IsBanned( const char *IP );
350
354 void SetLimitIPConnectionFrequency(bool b);
355
356 // --------------------------------------------------------------------------------------------Pinging Functions - Functions dealing with the automatic ping mechanism--------------------------------------------------------------------------------------------
360 void Ping( const SystemAddress target );
361
370 bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 );
371
375 int GetAveragePing( const AddressOrGUID systemIdentifier );
376
380 int GetLastPing( const AddressOrGUID systemIdentifier ) const;
381
385 int GetLowestPing( const AddressOrGUID systemIdentifier ) const;
386
391 void SetOccasionalPing( bool doPing );
392
397 SLNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier );
398
399 // --------------------------------------------------------------------------------------------Static Data Functions - Functions dealing with API defined synchronized memory--------------------------------------------------------------------------------------------
405 void SetOfflinePingResponse( const char *data, const unsigned int length );
406
411 void GetOfflinePingResponse( char **data, unsigned int *length );
412
413 //--------------------------------------------------------------------------------------------Network Functions - Functions dealing with the network in general--------------------------------------------------------------------------------------------
419 SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const;
420
424 void SetInternalID(SystemAddress systemAddress, int index=0);
425
428 SystemAddress GetExternalID( const SystemAddress target ) const;
429
431 const RakNetGUID GetMyGUID(void) const;
432
434 SystemAddress GetMyBoundAddress(const int socketIndex=0);
435
442 const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const;
443
449 SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const;
450
455 bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const;
456
458
464 void SetTimeoutTime(SLNet::TimeMS timeMS, const SystemAddress target );
465
469 SLNet::TimeMS GetTimeoutTime( const SystemAddress target );
470
474 int GetMTUSize( const SystemAddress target ) const;
475
478 unsigned GetNumberOfAddresses( void );
479
483 const char* GetLocalIP( unsigned int index );
484
489 bool IsLocalIP( const char *ip );
490
494 void AllowConnectionResponseIPMigration( bool allow );
495
505 bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 );
506
512 void SetSplitMessageProgressInterval(int interval);
513
516 int GetSplitMessageProgressInterval(void) const;
517
522 void SetUnreliableTimeout(SLNet::TimeMS timeoutMS);
523
531 void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 );
532
533 // -------------------------------------------------------------------------------------------- Plugin Functions--------------------------------------------------------------------------------------------
537 void AttachPlugin( PluginInterface2 *plugin );
538
543 void DetachPlugin( PluginInterface2 *messageHandler );
544
545 // --------------------------------------------------------------------------------------------Miscellaneous Functions--------------------------------------------------------------------------------------------
550 void PushBackPacket( Packet *packet, bool pushAtHead );
551
556 void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress);
557
562 Packet* AllocatePacket(unsigned dataSize);
563
569 virtual RakNetSocket2* GetSocket( const SystemAddress target );
570
574 virtual void GetSockets( DataStructures::List<RakNetSocket2* > &sockets );
575 virtual void ReleaseSockets( DataStructures::List<RakNetSocket2* > &sockets );
576
578 virtual void WriteOutOfBandHeader(SLNet::BitStream *bitStream);
579
583 virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData);
584
589 virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) );
590
591 // --------------------------------------------------------------------------------------------Network Simulator Functions--------------------------------------------------------------------------------------------
600 virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance);
601
606 virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond );
607
610 virtual bool IsNetworkSimulatorActive( void );
611
612 // --------------------------------------------------------------------------------------------Statistical Functions - Functions dealing with API performance--------------------------------------------------------------------------------------------
613
620 RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 );
623 bool GetStatistics( const unsigned int index, RakNetStatistics *rns );
629 virtual void GetStatisticsList(DataStructures::List<SystemAddress> &addresses, DataStructures::List<RakNetGUID> &guids, DataStructures::List<RakNetStatistics> &statistics);
630
632 virtual unsigned int GetReceiveBufferSize(void);
633
634 // --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY--------------------------------------------------------------------------------------------
635
636
638 // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds.
639 // updateBitStream should be:
640 // BitStream updateBitStream( MAXIMUM_MTU_SIZE
641 // #if LIBCAT_SECURITY==1
642 // + cat::AuthenticatedEncryption::OVERHEAD_BYTES
643 // #endif
644 // );
645 bool RunUpdateCycle( BitStream &updateBitStream );
646
648 // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds.
649 // Call in a loop until returns false if the socket is non-blocking
650 // remotePortRakNetWasStartedOn_PS3 and extraSocketOptions are from SocketDescriptor when the socket was created
651 // bool RunRecvFromOnce( RakNetSocket *s );
652
654 bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 );
655
656 // static Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line);
657
660 struct PingAndClockDifferential
661 {
662 unsigned short pingTime;
663 SLNet::Time clockDifferential;
664 };
665
668 struct RemoteSystemStruct
669 {
670 bool isActive; // Is this structure in use?
671 SystemAddress systemAddress;
672 SystemAddress myExternalSystemAddress;
673 SystemAddress theirInternalSystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
674 ReliabilityLayer reliabilityLayer;
675 bool weInitiatedTheConnection;
676 PingAndClockDifferential pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE ];
677 SLNet::Time pingAndClockDifferentialWriteIndex;
678 unsigned short lowestPing;
679 SLNet::Time nextPingTime;
680 SLNet::Time lastReliableSend;
681 SLNet::Time connectionTime;
682 // int connectionSocketIndex; // index into connectionSockets to send back on.
683 RakNetGUID guid;
684 int MTUSize;
685 // Reference counted socket to send back on
686 RakNetSocket2* rakNetSocket;
687 SystemIndex remoteSystemIndex;
688
689 #if LIBCAT_SECURITY==1
690 // Cached answer used internally by RakPeer to prevent DoS attacks based on the connexion handshake
691 char answer[cat::EasyHandshake::ANSWER_BYTES];
692
693 // If the server has bRequireClientKey = true, then this is set to the validated public key of the connected client
694 // Valid after connectMode reaches HANDLING_CONNECTION_REQUEST
695 char client_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
696 #endif
697
698 enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, CONNECTED} connectMode;
699 };
700
701 // DS_APR
702 //void ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, SLNet::TimeUS timeRead);
703 // /DS_APR
704 protected:
705
706 friend RAK_THREAD_DECLARATION(UpdateNetworkLoop);
707 //friend RAK_THREAD_DECLARATION(RecvFromLoop);
708 friend RAK_THREAD_DECLARATION(UDTConnect);
709
710 friend bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, SLNet::TimeUS timeRead );
711 friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, SLNet::TimeUS timeRead, BitStream &updateBitStream );
712 friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, SLNet::TimeUS timeRead, BitStream &updateBitStream );
713
714 int GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const;
715 int GetIndexFromGuid( const RakNetGUID guid );
716
717 //void RemoveFromRequestedConnectionsList( const SystemAddress systemAddress );
718 // Two versions needed because some buggy compilers strip the last parameter if unused, and crashes
719 ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, SLNet::TimeMS timeoutTime, RakNetSocket2* socket );
720 ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, SLNet::TimeMS timeoutTime );
724 RemoteSystemStruct *GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const;
725 RakPeer::RemoteSystemStruct *GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const;
726 void ValidateRemoteSystemLookup(void) const;
727 RemoteSystemStruct *GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const;
729 void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize);
730 void OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, SLNet::Time incomingTimestamp );
732 void NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority );
734 unsigned int GetNumberOfRemoteInitiatedConnections( void ) const;
743 RemoteSystemStruct * AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity );
747 void ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const;
751 SLNet::Time GetBestClockDifferential( const SystemAddress systemAddress ) const;
752
753 bool IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const;
754 SystemAddress GetLoopbackAddress(void) const;
755
757 volatile bool endThreads;
759 volatile bool isMainLoopThreadActive;
760
761 // SLNet::LocklessUint32_t isRecvFromLoopThreadActive;
762
763
764 bool occasionalPing;
765
766 unsigned int maximumNumberOfPeers;
767 //05/02/06 Just using maximumNumberOfPeers instead
769 //unsigned short remoteSystemListSize;
771 unsigned int maximumIncomingConnections;
772 SLNet::BitStream offlinePingResponse;
774 // SystemAddress mySystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
775 char incomingPassword[256];
776 unsigned char incomingPasswordLength;
777
782 RemoteSystemStruct* remoteSystemList;
786 RemoteSystemStruct** activeSystemList;
787 unsigned int activeSystemListSize;
788
789 // Use a hash, with binaryAddress plus port mod length as the index
790 RemoteSystemIndex **remoteSystemLookup;
791 unsigned int RemoteSystemLookupHashIndex(const SystemAddress &sa) const;
792 void ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex);
793 void DereferenceRemoteSystem(const SystemAddress &sa);
794 RemoteSystemStruct* GetRemoteSystem(const SystemAddress &sa) const;
795 unsigned int GetRemoteSystemIndex(const SystemAddress &sa) const;
796 void ClearRemoteSystemLookup(void);
797 DataStructures::MemoryPool<RemoteSystemIndex> remoteSystemIndexPool;
798
799 void AddToActiveSystemList(unsigned int remoteSystemListIndex);
800 void RemoveFromActiveSystemList(const SystemAddress &sa);
801
802 // unsigned int LookupIndexUsingHashIndex(const SystemAddress &sa) const;
803 // unsigned int RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const;
804 // unsigned int FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const;
805
806 enum
807 {
808 // Only put these mutexes in user thread functions!
809 requestedConnectionList_Mutex,
810 offlinePingResponse_Mutex,
811 NUMBER_OF_RAKPEER_MUTEXES
812 };
813 SimpleMutex rakPeerMutexes[ NUMBER_OF_RAKPEER_MUTEXES ];
815
816 bool updateCycleIsRunning;
818
819 //DataStructures::Queue<RequestedConnectionStruct*> requestedConnectionsList;
821
822 unsigned int bytesSentPerSecond, bytesReceivedPerSecond;
823 // bool isSocketLayerBlocking;
824 // bool continualPing,isRecvfromThreadActive,isMainLoopThreadActive, endThreads, isSocketLayerBlocking;
825 unsigned int validationInteger;
826 SimpleMutex incomingQueueMutex, banListMutex; //,synchronizedMemoryQueueMutex, automaticVariableSynchronizationMutex;
827 //DataStructures::Queue<Packet *> incomingpacketSingleProducerConsumer; //, synchronizedMemorypacketSingleProducerConsumer;
828 // BitStream enumerationData;
829
830 struct BanStruct
831 {
832 char *IP;
833 SLNet::TimeMS timeout; // 0 for none
834 };
835
836 struct RequestedConnectionStruct
837 {
838 SystemAddress systemAddress;
839 SLNet::Time nextRequestTime;
840 unsigned char requestsMade;
841 char *data;
842 unsigned short dataLength;
843 char outgoingPassword[256];
844 unsigned char outgoingPasswordLength;
845 unsigned socketIndex;
846 unsigned int extraData;
847 unsigned sendConnectionAttemptCount;
848 unsigned timeBetweenSendConnectionAttemptsMS;
849 SLNet::TimeMS timeoutTime;
850 PublicKeyMode publicKeyMode;
851 RakNetSocket2* socket;
852 enum {CONNECT=1, /*PING=2, PING_OPEN_CONNECTIONS=4,*/ /*ADVERTISE_SYSTEM=2*/} actionToTake;
853
854 #if LIBCAT_SECURITY==1
855 char handshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES];
856 cat::ClientEasyHandshake *client_handshake;
857 char remote_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
858 // char remote_challenge[cat::EasyHandshake::CHALLENGE_BYTES];
859 // char random[16];
860 #endif
861 };
862 #if LIBCAT_SECURITY==1
863 bool GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey);
864 #endif
865
866 //DataStructures::List<DataStructures::List<MemoryBlock>* > automaticVariableSynchronizationList;
867 DataStructures::List<BanStruct*> banList;
868 // Threadsafe, and not thread safe
869 DataStructures::List<PluginInterface2*> pluginListTS, pluginListNTS;
870
871 DataStructures::Queue<RequestedConnectionStruct*> requestedConnectionQueue;
872 SimpleMutex requestedConnectionQueueMutex;
873
874 // void RunMutexedUpdateCycle(void);
875
876 struct BufferedCommandStruct
877 {
878 BitSize_t numberOfBitsToSend;
879 PacketPriority priority;
880 PacketReliability reliability;
881 char orderingChannel;
882 AddressOrGUID systemIdentifier;
883 bool broadcast;
884 RemoteSystemStruct::ConnectMode connectionMode;
885 NetworkID networkID;
886 bool blockingCommand; // Only used for RPC
887 char *data;
888 bool haveRakNetCloseSocket;
889 unsigned connectionSocketIndex;
890 unsigned short remotePortRakNetWasStartedOn_PS3;
891 unsigned int extraSocketOptions;
892 RakNetSocket2* socket;
893 unsigned short port;
894 uint32_t receipt;
895 enum {BCS_SEND, BCS_CLOSE_CONNECTION, BCS_GET_SOCKET, BCS_CHANGE_SYSTEM_ADDRESS,/* BCS_USE_USER_SOCKET, BCS_REBIND_SOCKET_ADDRESS, BCS_RPC, BCS_RPC_SHIFT,*/ BCS_DO_NOTHING} command;
896 };
897
898 // Single producer single consumer queue using a linked list
899 //BufferedCommandStruct* bufferedCommandReadIndex, bufferedCommandWriteIndex;
900
901 DataStructures::ThreadsafeAllocatingQueue<BufferedCommandStruct> bufferedCommands;
902
903
904 // DataStructures::ThreadsafeAllocatingQueue<RNS2RecvStruct> bufferedPackets;
905
906 DataStructures::Queue<RNS2RecvStruct*> bufferedPacketsFreePool;
907 SLNet::SimpleMutex bufferedPacketsFreePoolMutex;
908 DataStructures::Queue<RNS2RecvStruct*> bufferedPacketsQueue;
909 SLNet::SimpleMutex bufferedPacketsQueueMutex;
910
911 virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line);
912 virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line);
913 void SetupBufferedPackets(void);
914 void PushBufferedPacket(RNS2RecvStruct * p);
915 RNS2RecvStruct *PopBufferedPacket(void);
916
917 struct SocketQueryOutput
918 {
919 SocketQueryOutput() {}
920 ~SocketQueryOutput() {}
921 DataStructures::List<RakNetSocket2* > sockets;
922 };
923
924 DataStructures::ThreadsafeAllocatingQueue<SocketQueryOutput> socketQueryOutput;
925
926
927 bool AllowIncomingConnections(void) const;
928
929 void PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability );
930 // This stores the user send calls to be handled by the update thread. This way we don't have thread contention over systemAddresss
931 void CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority );
932 void SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt );
933 void SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt );
934 bool SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, SLNet::TimeUS currentTime, uint32_t receipt );
935 //bool HandleBufferedRPC(BufferedCommandStruct *bcs, SLNet::TimeMS time);
936 void ClearBufferedCommands(void);
937 void ClearBufferedPackets(void);
938 void ClearSocketQueryOutput(void);
939 void ClearRequestedConnectionList(void);
940 void AddPacketToProducer(SLNet::Packet *p);
941 unsigned int GenerateSeedFromGuid(void);
942 SLNet::Time GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const;
943 SimpleMutex securityExceptionMutex;
944
945 //DataStructures::AVLBalancedBinarySearchTree<RPCNode> rpcTree;
946 int defaultMTUSize;
947 bool trackFrequencyTable;
948
949 // Smart pointer so I can return the object to the user
950 DataStructures::List<RakNetSocket2* > socketList;
951 void DerefAllSockets(void);
952 unsigned int GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const;
953 // Used for RPC replies
954 SLNet::BitStream *replyFromTargetBS;
955 SystemAddress replyFromTargetPlayer;
956 bool replyFromTargetBroadcast;
957
958 SLNet::TimeMS defaultTimeoutTime;
959
960 // Generate and store a unique GUID
961 void GenerateGUID(void);
962 unsigned int GetSystemIndexFromGuid( const RakNetGUID input ) const;
963 RakNetGUID myGuid;
964
965 unsigned maxOutgoingBPS;
966
967 // Nobody would use the internet simulator in a final build.
968 #ifdef _DEBUG
969 double _packetloss;
970 unsigned short _minExtraPing, _extraPingVariance;
971 #endif
972
974 //unsigned int lastUserUpdateCycle;
976 bool allowConnectionResponseIPMigration;
977
978 SystemAddress firstExternalID;
979 int splitMessageProgressInterval;
980 SLNet::TimeMS unreliableTimeout;
981
982 bool (*incomingDatagramEventHandler)(RNS2RecvStruct *);
983
984 // Systems in this list will not go through the secure connection process, even when secure connections are turned on. Wildcards are accepted.
985 DataStructures::List<SLNet::RakString> securityExceptionList;
986
987 SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ];
988
989 bool allowInternalRouting;
990
991 void (*userUpdateThreadPtr)(RakPeerInterface *, void *);
992 void *userUpdateThreadData;
993
994
995 SignaledEvent quitAndDataEvents;
996 bool limitConnectionFrequencyFromTheSameIP;
997
998 SimpleMutex packetAllocationPoolMutex;
999 DataStructures::MemoryPool<Packet> packetAllocationPool;
1000
1001 SimpleMutex packetReturnMutex;
1002 DataStructures::Queue<Packet*> packetReturnQueue;
1003 Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line);
1004 Packet *AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line);
1005
1009 SimpleMutex sendReceiptSerialMutex;
1010 uint32_t sendReceiptSerial;
1011 void ResetSendReceipt(void);
1012 void OnConnectedPong(SLNet::Time sendPingTime, SLNet::Time sendPongTime, RemoteSystemStruct *remoteSystem);
1013 void CallPluginCallbacks(DataStructures::List<PluginInterface2*> &pluginList, Packet *packet);
1014
1015 #if LIBCAT_SECURITY==1
1016 // Encryption and security
1017 bool _using_security, _require_client_public_key;
1018 char my_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
1019 cat::ServerEasyHandshake *_server_handshake;
1020 cat::CookieJar *_cookie_jar;
1021 bool InitializeClientSecurity(RequestedConnectionStruct *rcs, const char *public_key);
1022 #endif
1023
1024
1025
1026
1027
1028
1029 virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct);
1030 void FillIPList(void);
1031 }
1032 // #if defined(SN_TARGET_PSP2)
1033 // __attribute__((aligned(8)))
1034 // #endif
1035 ;
1036
1037 } // namespace SLNet
1038
1039 #endif