This repository has been archived on 2024-11-16. You can view files and clone it, but cannot push or open issues or pull requests.

103 lines
2.7 KiB
C
Raw Normal View History

2021-01-02 10:52:05 +08:00
#pragma once
#include "CoreMinimal.h"
2021-01-03 18:36:45 +08:00
#include "Misc/DateTime.h"
2021-01-02 10:52:05 +08:00
#include "RSHWNetworkType.h"
#include "Components/ActorComponent.h"
2021-01-02 10:52:05 +08:00
#include "RSHWNetworkServer.generated.h"
class FSocket;
2021-01-03 18:36:45 +08:00
class FKCPWrap;
2021-01-02 10:52:05 +08:00
UCLASS(BlueprintType, hidecategories = ("Cooking", "ComponentReplication"), meta = (BlueprintSpawnableComponent))
class RSHWNETWORK_API URSHWNetworkServer : public UActorComponent
2021-01-02 10:52:05 +08:00
{
GENERATED_BODY()
public:
URSHWNetworkServer(const FObjectInitializer& ObjectInitializer);
public:
2021-01-02 10:52:05 +08:00
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_OneParam(FLoginSignature, URSHWNetworkServer, OnLogin, int32, ClientID);
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_TwoParams(FRecvSignature, URSHWNetworkServer, OnRecv, int32, ClientID, const TArray<uint8>&, Data);
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_OneParam(FUnloginSignature, URSHWNetworkServer, OnUnlogin, int32, ClientID);
2021-01-02 10:52:05 +08:00
public:
UPROPERTY(BlueprintAssignable, Category = "RSHW|Network")
FLoginSignature OnLogin;
2021-01-02 10:52:05 +08:00
UPROPERTY(BlueprintAssignable, Category = "RSHW|Network")
FRecvSignature OnRecv;
2021-01-02 10:52:05 +08:00
UPROPERTY(BlueprintAssignable, Category = "RSHW|Network")
FUnloginSignature OnUnlogin;
2021-01-02 10:52:05 +08:00
public:
2021-01-02 10:52:05 +08:00
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
2021-01-02 10:52:05 +08:00
bool Send(int32 ClientID, const TArray<uint8>& Data);
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RSHW|Network")
2021-01-02 10:52:05 +08:00
int32 Port = 25565;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RSHW|Network")
FTimespan Heartbeat = FTimespan::FromSeconds(1.0);
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RSHW|Network")
FTimespan TimeoutLimit = FTimespan::FromSeconds(8.0);
2021-01-02 10:52:05 +08:00
2021-01-03 18:36:45 +08:00
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RSHW|Network")
int32 KCPLogMask = 0;
2021-01-02 10:52:05 +08:00
private:
FSocket* SocketPtr;
TArray<uint8> SendBuffer;
TArray<uint8> RecvBuffer;
TArray<uint8> DataBuffer;
int32 NextRegistrationID;
struct FPreRegistrationInfo
{
FDateTime Time;
FRSHWNetworkPass Pass;
};
TMap<FString, FPreRegistrationInfo> PreRegistration;
struct FRegistrationInfo
{
FRSHWNetworkPass Pass;
FDateTime RecvTime;
FDateTime Heartbeat;
TSharedPtr<FInternetAddr> Addr;
2021-01-03 18:36:45 +08:00
TSharedPtr<FKCPWrap> KCPUnit;
2021-01-02 10:52:05 +08:00
};
TMap<int32, FRegistrationInfo> Registration;
2021-01-03 18:36:45 +08:00
int32 UDPSend(int32 ClientID, const uint8* Data, int32 Count);
2021-01-02 10:52:05 +08:00
private:
//~ Begin UActorComponent Interface
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void Activate(bool bReset) override;
virtual void Deactivate() override;
//~ End UActorComponent Interface
2021-01-02 10:52:05 +08:00
//~ Begin UObject Interface
virtual void BeginDestroy() override;
//~ End UObject Interface
};