Unreal-RedNetwork/Source/RedNetwork/Public/RedNetworkClient.h

107 lines
2.7 KiB
C
Raw Normal View History

2021-01-02 02:52:05 +00:00
#pragma once
#include "CoreMinimal.h"
#include "Misc/DateTime.h"
#include "UObject/Object.h"
2021-01-05 11:48:45 +00:00
#include "RedNetworkType.h"
#include "RedNetworkClient.generated.h"
2021-01-02 02:52:05 +00:00
2021-01-03 10:36:45 +00:00
class FKCPWrap;
2021-01-02 02:52:05 +00:00
class FInternetAddr;
UCLASS(BlueprintType)
2021-01-05 11:48:45 +00:00
class REDNETWORK_API URedNetworkClient : public UObject, public FTickableGameObject
2021-01-02 02:52:05 +00:00
{
GENERATED_BODY()
public:
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE(FLoginSignature, URedNetworkClient, OnLogin);
2021-01-07 14:00:11 +00:00
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_TwoParams(FRecvSignature, URedNetworkClient, OnRecv, uint8, Channel, const TArray<uint8>&, Data);
2021-01-05 11:48:45 +00:00
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE(FUnloginSignature, URedNetworkClient, OnUnlogin);
2021-01-02 02:52:05 +00:00
public:
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintAssignable, Category = "Red|Network")
FLoginSignature OnLogin;
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintAssignable, Category = "Red|Network")
FRecvSignature OnRecv;
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintAssignable, Category = "Red|Network")
FUnloginSignature OnUnlogin;
2021-01-02 02:52:05 +00:00
public:
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UFUNCTION(BlueprintCallable, Category = "Red|Network")
bool IsActive() const { return bIsActive; }
2021-01-05 11:48:45 +00:00
UFUNCTION(BlueprintCallable, Category = "Red|Network")
void Activate(bool bReset = false);
2021-01-05 11:48:45 +00:00
UFUNCTION(BlueprintCallable, Category = "Red|Network")
void Deactivate();
2021-01-05 11:48:45 +00:00
UFUNCTION(BlueprintCallable, Category = "Red|Network")
bool IsLogged() const { return ClientPass.IsValid(); }
2021-01-05 11:48:45 +00:00
UFUNCTION(BlueprintCallable, Category = "Red|Network")
2021-01-07 14:00:11 +00:00
bool Send(uint8 Channel, const TArray<uint8>& Data);
2021-01-02 02:52:05 +00:00
public:
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Red|Network")
FString ServerAddr = TEXT("127.0.0.1:25565");
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Red|Network")
FTimespan Heartbeat = FTimespan::FromSeconds(1.0);
2021-01-02 02:52:05 +00:00
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Red|Network")
2021-01-02 02:52:05 +00:00
FTimespan TimeoutLimit = FTimespan::FromSeconds(8.0);
2021-01-05 11:48:45 +00:00
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Red|Network")
2021-01-03 10:36:45 +00:00
int32 KCPLogMask = 0;
2021-01-02 02:52:05 +00:00
private:
bool bIsActive = false;
TSharedPtr<FInternetAddr> ServerAddrPtr;
2021-01-02 02:52:05 +00:00
FSocket* SocketPtr;
TArray<uint8> SendBuffer;
TArray<uint8> RecvBuffer;
2021-01-05 11:48:45 +00:00
FRedNetworkPass ClientPass;
2021-01-02 02:52:05 +00:00
FDateTime LastRecvTime;
FDateTime LastHeartbeat;
2021-01-07 14:00:11 +00:00
TArray<TSharedPtr<FKCPWrap>> KCPUnits;
FDateTime NowTime;
void UpdateKCP();
void SendHeartbeat();
void HandleSocketRecv();
void HandleLoginRecv(const FRedNetworkPass& SourcePass);
void HandleKCPRecv();
void HandleTimeout();
2021-01-03 10:36:45 +00:00
2021-01-07 14:00:11 +00:00
void EnsureChannelCreated(uint8 Channel);
2021-01-03 13:20:03 +00:00
public:
2021-01-02 02:52:05 +00:00
//~ Begin FTickableGameObject Interface
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override { return !IsTemplate() && IsActive(); }
virtual TStatId GetStatId() const override { return GetStatID(); }
//~ End FTickableGameObject Interface
2021-01-02 02:52:05 +00:00
//~ Begin UObject Interface
virtual void BeginDestroy() override;
//~ End UObject Interface
};