#pragma once #include "CoreMinimal.h" #include "UObject/Object.h" #include "RSHWNetworkType.h" #include "UObject/Interface.h" #include "RSHWNetworkServer.generated.h" class FSocket; UINTERFACE() class RSHWNETWORK_API URSHWNetworkServerHandler : public UInterface { GENERATED_BODY() }; class RSHWNETWORK_API IRSHWNetworkServerHandler { GENERATED_BODY() public: virtual void OnLogin(int32 ClientID) { } virtual void OnRecv(int32 ClientID, const TArray& Data) { } virtual void OnUnlogin(int32 ClientID) { } }; UCLASS() class RSHWNETWORK_API URSHWNetworkServer : public UObject, public FTickableGameObject { GENERATED_BODY() public: bool IsRunning() const { return bIsRunning; } bool SetHandler(TScriptInterface InHandlerObject); TScriptInterface GetHandler() const { return HandlerObject; } bool SetBindPort(int32 InPort = 25565); int32 GetBindPort() const { return Port; } bool Send(int32 ClientID, const TArray& Data); bool RunServer(); void StopServer(); public: FTimespan TimeoutLimit = FTimespan::FromSeconds(8.0); private: bool bIsRunning = false; int32 Port = 25565; UPROPERTY() TScriptInterface HandlerObject; private: FSocket* SocketPtr; TArray SendBuffer; TArray RecvBuffer; TArray DataBuffer; int32 NextRegistrationID; struct FPreRegistrationInfo { FDateTime Time; FRSHWNetworkPass Pass; }; TMap PreRegistration; struct FRegistrationInfo { FRSHWNetworkPass Pass; FDateTime RecvTime; FDateTime Heartbeat; TSharedPtr Addr; }; TMap Registration; void ResetRunningData(); private: //~ Begin UObject Interface virtual void BeginDestroy() override; //~ End UObject Interface //~ Begin FTickableGameObject Interface virtual void Tick(float DeltaTime) override; virtual bool IsTickable() const override { return !IsTemplate() && bIsRunning; } virtual TStatId GetStatId() const override { return GetStatID(); } //~ End FTickableGameObject Interface };