2021-12-05 17:54:36 +00:00
# define _VARIADIC_MAX 10
# include "dServer.h"
# include "dNetCommon.h"
2023-10-21 23:31:55 +00:00
# include "Logger.h"
2022-12-06 00:08:47 +00:00
# include "dConfig.h"
2021-12-05 17:54:36 +00:00
# include "RakNetworkFactory.h"
# include "MessageIdentifiers.h"
2023-05-03 21:38:32 +00:00
# include "eConnectionType.h"
# include "eServerMessageType.h"
# include "eMasterMessageType.h"
2021-12-05 17:54:36 +00:00
2023-09-21 01:06:28 +00:00
# include "BitStreamUtils.h"
2021-12-05 17:54:36 +00:00
# include "MasterPackets.h"
# include "ZoneInstanceManager.h"
2024-01-05 00:21:03 +00:00
# include "StringifiedEnum.h"
2021-12-05 17:54:36 +00:00
//! Replica Constructor class
class ReplicaConstructor : public ReceiveConstructionInterface {
2022-07-25 02:26:51 +00:00
public :
2021-12-05 17:54:36 +00:00
ReplicaReturnResult ReceiveConstruction ( RakNet : : BitStream * inBitStream , RakNetTime timestamp , NetworkID networkID , NetworkIDObject * existingObject , SystemAddress senderId , ReplicaManager * caller ) {
return REPLICA_PROCESSING_DONE ;
}
} ConstructionCB ;
//! Replica Download Sender class
class ReplicaSender : public SendDownloadCompleteInterface {
public :
ReplicaReturnResult SendDownloadComplete ( RakNet : : BitStream * outBitStream , RakNetTime currentTime , SystemAddress senderId , ReplicaManager * caller ) {
return REPLICA_PROCESSING_DONE ;
}
} SendDownloadCompleteCB ;
//! Replica Download Receiver class
class ReplicaReceiever : public ReceiveDownloadCompleteInterface {
public :
ReplicaReturnResult ReceiveDownloadComplete ( RakNet : : BitStream * inBitStream , SystemAddress senderId , ReplicaManager * caller ) {
return REPLICA_PROCESSING_DONE ;
}
} ReceiveDownloadCompleteCB ;
2024-01-02 03:50:00 +00:00
dServer : : dServer ( const std : : string & ip , int port , int instanceID , int maxConnections , bool isInternal , bool useEncryption , Logger * logger , const std : : string masterIP , int masterPort , ServerType serverType , dConfig * config , Game : : signal_t * lastSignal , unsigned int zoneID ) {
2021-12-05 17:54:36 +00:00
mIP = ip ;
mPort = port ;
mZoneID = zoneID ;
mInstanceID = instanceID ;
mMaxConnections = maxConnections ;
mIsInternal = isInternal ;
mUseEncryption = useEncryption ;
mLogger = logger ;
mMasterIP = masterIP ;
mMasterPort = masterPort ;
mMasterConnectionActive = false ;
mNetIDManager = nullptr ;
mReplicaManager = nullptr ;
mServerType = serverType ;
2022-12-06 00:08:47 +00:00
mConfig = config ;
2024-01-02 03:50:00 +00:00
mShouldShutdown = lastSignal ;
2021-12-05 17:54:36 +00:00
//Attempt to start our server here:
mIsOkay = Startup ( ) ;
//Forcibly log to both the console and our file what ip, port and possibly zoneID / instanceID we're running on:
2023-10-21 23:31:55 +00:00
bool prevLogSetting = mLogger - > GetLogToConsole ( ) ;
2021-12-05 17:54:36 +00:00
mLogger - > SetLogToConsole ( true ) ;
if ( mIsOkay ) {
if ( zoneID = = 0 )
2024-01-05 00:21:03 +00:00
LOG ( " %s Server is listening on %s:%i with encryption: %i " , StringifiedEnum : : ToString ( serverType ) . data ( ) , ip . c_str ( ) , port , int ( useEncryption ) ) ;
2021-12-05 17:54:36 +00:00
else
2024-01-05 00:21:03 +00:00
LOG ( " %s Server is listening on %s:%i with encryption: %i, running zone %i / %i " , StringifiedEnum : : ToString ( serverType ) . data ( ) , ip . c_str ( ) , port , int ( useEncryption ) , zoneID , instanceID ) ;
2023-10-21 23:31:55 +00:00
} else { LOG ( " FAILED TO START SERVER ON IP/PORT: %s:%i " , ip . c_str ( ) , port ) ; return ; }
2021-12-05 17:54:36 +00:00
mLogger - > SetLogToConsole ( prevLogSetting ) ;
2022-12-21 16:51:27 +00:00
2021-12-05 17:54:36 +00:00
//Connect to master if we are not master:
if ( serverType ! = ServerType : : Master ) {
SetupForMasterConnection ( ) ;
2024-01-02 03:50:00 +00:00
if ( ! ConnectToMaster ( ) ) {
LOG ( " Failed ConnectToMaster! " ) ;
}
2021-12-05 17:54:36 +00:00
}
//Set up Replica if we're a world server:
if ( serverType = = ServerType : : World ) {
mNetIDManager = new NetworkIDManager ( ) ;
mNetIDManager - > SetIsNetworkIDAuthority ( true ) ;
mReplicaManager = new ReplicaManager ( ) ;
mReplicaManager - > SetAutoParticipateNewConnections ( false ) ;
mReplicaManager - > SetAutoConstructToNewParticipants ( false ) ;
mReplicaManager - > SetAutoSerializeInScope ( true ) ;
mReplicaManager - > SetReceiveConstructionCB ( & ConstructionCB ) ;
mReplicaManager - > SetDownloadCompleteCB ( & SendDownloadCompleteCB , & ReceiveDownloadCompleteCB ) ;
mPeer - > AttachPlugin ( mReplicaManager ) ;
mPeer - > SetNetworkIDManager ( mNetIDManager ) ;
}
}
dServer : : ~ dServer ( ) {
Shutdown ( ) ;
}
Packet * dServer : : ReceiveFromMaster ( ) {
if ( ! mMasterPeer ) return nullptr ;
if ( ! mMasterConnectionActive ) ConnectToMaster ( ) ;
Packet * packet = mMasterPeer - > Receive ( ) ;
if ( packet ) {
if ( packet - > length < 1 ) { mMasterPeer - > DeallocatePacket ( packet ) ; return nullptr ; }
if ( packet - > data [ 0 ] = = ID_DISCONNECTION_NOTIFICATION | | packet - > data [ 0 ] = = ID_CONNECTION_LOST ) {
2023-10-21 23:31:55 +00:00
LOG ( " Lost our connection to master, shutting DOWN! " ) ;
2021-12-05 17:54:36 +00:00
mMasterConnectionActive = false ;
//ConnectToMaster(); //We'll just shut down now
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
if ( packet - > data [ 0 ] = = ID_CONNECTION_REQUEST_ACCEPTED ) {
2023-10-21 23:31:55 +00:00
LOG ( " Established connection to master, zone (%i), instance (%i) " , this - > GetZoneID ( ) , this - > GetInstanceID ( ) ) ;
2021-12-05 17:54:36 +00:00
mMasterConnectionActive = true ;
mMasterSystemAddress = packet - > systemAddress ;
MasterPackets : : SendServerInfo ( this , packet ) ;
}
if ( packet - > data [ 0 ] = = ID_USER_PACKET_ENUM ) {
2023-05-03 21:38:32 +00:00
if ( static_cast < eConnectionType > ( packet - > data [ 1 ] ) = = eConnectionType : : MASTER ) {
switch ( static_cast < eMasterMessageType > ( packet - > data [ 3 ] ) ) {
case eMasterMessageType : : REQUEST_ZONE_TRANSFER_RESPONSE : {
2024-01-07 08:02:27 +00:00
ZoneInstanceManager : : Instance ( ) - > HandleRequestZoneTransferResponse ( packet ) ;
2021-12-05 17:54:36 +00:00
break ;
}
2023-05-03 21:38:32 +00:00
case eMasterMessageType : : SHUTDOWN :
2024-01-02 03:50:00 +00:00
* mShouldShutdown = - 2 ;
2022-12-15 12:02:38 +00:00
break ;
2021-12-05 17:54:36 +00:00
2022-12-15 12:02:38 +00:00
//When we handle these packets in World instead dServer, we just return the packet's pointer.
2021-12-05 17:54:36 +00:00
default :
return packet ;
}
}
}
mMasterPeer - > DeallocatePacket ( packet ) ;
}
return nullptr ;
}
Packet * dServer : : Receive ( ) {
return mPeer - > Receive ( ) ;
}
void dServer : : DeallocatePacket ( Packet * packet ) {
mPeer - > DeallocatePacket ( packet ) ;
}
void dServer : : DeallocateMasterPacket ( Packet * packet ) {
mMasterPeer - > DeallocatePacket ( packet ) ;
}
2024-02-27 05:43:33 +00:00
void dServer : : Send ( RakNet : : BitStream & bitStream , const SystemAddress & sysAddr , bool broadcast ) {
mPeer - > Send ( & bitStream , SYSTEM_PRIORITY , RELIABLE_ORDERED , 0 , sysAddr , broadcast ) ;
2021-12-05 17:54:36 +00:00
}
2024-02-27 05:43:33 +00:00
void dServer : : SendToMaster ( RakNet : : BitStream & bitStream ) {
2021-12-05 17:54:36 +00:00
if ( ! mMasterConnectionActive ) ConnectToMaster ( ) ;
2024-02-27 05:43:33 +00:00
mMasterPeer - > Send ( & bitStream , SYSTEM_PRIORITY , RELIABLE_ORDERED , 0 , mMasterSystemAddress , false ) ;
2021-12-05 17:54:36 +00:00
}
2023-02-19 12:29:14 +00:00
void dServer : : Disconnect ( const SystemAddress & sysAddr , eServerDisconnectIdentifiers disconNotifyID ) {
2021-12-05 17:54:36 +00:00
RakNet : : BitStream bitStream ;
2023-09-21 01:06:28 +00:00
BitStreamUtils : : WriteHeader ( bitStream , eConnectionType : : SERVER , eServerMessageType : : DISCONNECT_NOTIFY ) ;
2021-12-05 17:54:36 +00:00
bitStream . Write ( disconNotifyID ) ;
mPeer - > Send ( & bitStream , SYSTEM_PRIORITY , RELIABLE_ORDERED , 0 , sysAddr , false ) ;
mPeer - > CloseConnection ( sysAddr , true ) ;
}
bool dServer : : IsConnected ( const SystemAddress & sysAddr ) {
return mPeer - > IsConnected ( sysAddr ) ;
}
bool dServer : : Startup ( ) {
mSocketDescriptor = SocketDescriptor ( uint16_t ( mPort ) , 0 ) ;
mPeer = RakNetworkFactory : : GetRakPeerInterface ( ) ;
if ( ! mPeer ) return false ;
if ( ! mPeer - > Startup ( mMaxConnections , 10 , & mSocketDescriptor , 1 ) ) return false ;
if ( mIsInternal ) {
mPeer - > SetIncomingPassword ( " 3.25 DARKFLAME1 " , 15 ) ;
} else {
2022-12-06 00:08:47 +00:00
UpdateBandwidthLimit ( ) ;
2022-12-21 16:51:27 +00:00
UpdateMaximumMtuSize ( ) ;
2021-12-05 17:54:36 +00:00
mPeer - > SetIncomingPassword ( " 3.25 ND1 " , 8 ) ;
}
mPeer - > SetMaximumIncomingConnections ( mMaxConnections ) ;
if ( mUseEncryption ) mPeer - > InitializeSecurity ( NULL , NULL , NULL , NULL ) ;
return true ;
}
2022-12-21 16:51:27 +00:00
void dServer : : UpdateMaximumMtuSize ( ) {
auto maxMtuSize = mConfig - > GetValue ( " maximum_mtu_size " ) ;
mPeer - > SetMTUSize ( maxMtuSize . empty ( ) ? 1228 : std : : stoi ( maxMtuSize ) ) ;
}
2022-12-06 00:08:47 +00:00
void dServer : : UpdateBandwidthLimit ( ) {
auto newBandwidth = mConfig - > GetValue ( " maximum_outgoing_bandwidth " ) ;
mPeer - > SetPerConnectionOutgoingBandwidthLimit ( ! newBandwidth . empty ( ) ? std : : stoi ( newBandwidth ) : 0 ) ;
}
2021-12-05 17:54:36 +00:00
void dServer : : Shutdown ( ) {
2022-11-07 08:12:35 +00:00
if ( mPeer ) {
mPeer - > Shutdown ( 1000 ) ;
RakNetworkFactory : : DestroyRakPeerInterface ( mPeer ) ;
}
2021-12-05 17:54:36 +00:00
if ( mNetIDManager ) {
delete mNetIDManager ;
mNetIDManager = nullptr ;
}
if ( mReplicaManager ) {
delete mReplicaManager ;
mReplicaManager = nullptr ;
}
2022-11-07 08:12:35 +00:00
if ( mServerType ! = ServerType : : Master & & mMasterPeer ) {
2021-12-05 17:54:36 +00:00
mMasterPeer - > Shutdown ( 1000 ) ;
2022-11-07 08:12:35 +00:00
RakNetworkFactory : : DestroyRakPeerInterface ( mMasterPeer ) ;
2021-12-05 17:54:36 +00:00
}
}
void dServer : : SetupForMasterConnection ( ) {
mMasterSocketDescriptor = SocketDescriptor ( uint16_t ( mPort + 1 ) , 0 ) ;
mMasterPeer = RakNetworkFactory : : GetRakPeerInterface ( ) ;
2024-01-02 03:50:00 +00:00
bool ret = mMasterPeer - > Startup ( 1 , 30 , & mMasterSocketDescriptor , 1 ) ;
if ( ! ret ) LOG ( " Failed MasterPeer Startup! " ) ;
2021-12-05 17:54:36 +00:00
}
bool dServer : : ConnectToMaster ( ) {
2024-01-02 03:50:00 +00:00
//LOG("Connection to Master %s:%d", mMasterIP.c_str(), mMasterPort);
2021-12-05 17:54:36 +00:00
return mMasterPeer - > Connect ( mMasterIP . c_str ( ) , mMasterPort , " 3.25 DARKFLAME1 " , 15 ) ;
}
void dServer : : UpdateReplica ( ) {
mReplicaManager - > Update ( mPeer ) ;
}
int dServer : : GetPing ( const SystemAddress & sysAddr ) const {
return mPeer - > GetAveragePing ( sysAddr ) ;
}
int dServer : : GetLatestPing ( const SystemAddress & sysAddr ) const {
return mPeer - > GetLastPing ( sysAddr ) ;
}