Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic alpha version of the UE4 plugin #485

Merged
merged 6 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions sdks/ue4/Quilkin.uplugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"FileVersion" : 1,
"Version" : 1,
"VersionName" : "1.0",
"FriendlyName" : "Quilkin",
"Description" : "",
"Category" : "Online",
"CreatedBy" : "Embark Studios",
"CreatedByURL" : "http://embark.games",
"DocsURL" : "",
"MarketplaceURL" : "",
"SupportURL" : "",
"EnabledByDefault" : true,
"CanContainContent" : false,
"IsBetaVersion" : false,
"Installed" : false,
"Modules": [
{
"Name": "Quilkin",
"Type": "Runtime",
"LoadingPhase" : "Default"
}
]
}
9 changes: 9 additions & 0 deletions sdks/ue4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Quilkin Unreal Engine Plugin
rezvaneh marked this conversation as resolved.
Show resolved Hide resolved

This is an alpha version of the Unreal engine plugin for Quilkin. Currently it
only supports adding a routing token in the following format.

```
<packet> | token | version
X bytes | 16 bytes | 1 bytes
```
3 changes: 3 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinDelegates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "QuilkinDelegates.h"

FQuilkinDelegates::FGetQuilkinRoutingToken FQuilkinDelegates::GetQuilkinRoutingToken;
3 changes: 3 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "QuilkinLog.h"

DEFINE_LOG_CATEGORY(LogQuilkin);
5 changes: 5 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "CoreMinimal.h"

DECLARE_LOG_CATEGORY_EXTERN(LogQuilkin, VeryVerbose, All);
63 changes: 63 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "QuilkinModule.h"
#include "QuilkinLog.h"

#include "Modules/ModuleManager.h"
#include "SocketSubsystemModule.h"
#include "UObject/NameTypes.h"

void FQuilkinModule::StartupModule()
{
UE_LOG(LogQuilkin, Log, TEXT("FQuilkinModule::StartupModule()"));

bool bEnabled = true;
if (GConfig)
{
GConfig->GetBool(TEXT("Quilkin.SocketSubsystem"), TEXT("Enabled"), bEnabled, GEngineIni);
}
UE_LOG(LogQuilkin, Log, TEXT("QuilkinSocketSubsystem is %s"), bEnabled ? TEXT("enabled") : TEXT("disabled"));

if (!bEnabled)
{
return;
}

FSocketSubsystemModule& SocketSubsystemModule = FModuleManager::LoadModuleChecked<FSocketSubsystemModule>("Sockets");

ISocketSubsystem* DefaultSocketSubsystem = SocketSubsystemModule.GetSocketSubsystem();
if (DefaultSocketSubsystem == nullptr)
{
UE_LOG(LogQuilkin, Log, TEXT("No default SocketSubsystem was set. Will not use Quilkin SocketSubsystem"));
return;
}
UE_LOG(LogQuilkin, Log, TEXT("Overriding default SocketSubsystem with QuilkinSocketSubsystem"));

QuilkinSocketSubsystem = MakeUnique<FQuilkinSocketSubsystem>(DefaultSocketSubsystem);
SocketSubsystemModule.RegisterSocketSubsystem(QUILKIN_SOCKETSUBSYSTEM_NAME, QuilkinSocketSubsystem.Get(), true);
}

void FQuilkinModule::ShutdownModule()
{
UE_LOG(LogQuilkin, Log, TEXT("FQuilkinModule::ShutdownModule()"));

if (!QuilkinSocketSubsystem.IsValid())
{
return;
}

FSocketSubsystemModule& SocketSubsystemModule = FModuleManager::LoadModuleChecked<FSocketSubsystemModule>("Sockets");
SocketSubsystemModule.UnregisterSocketSubsystem(QUILKIN_SOCKETSUBSYSTEM_NAME);
QuilkinSocketSubsystem.Reset();
}

bool FQuilkinModule::SupportsDynamicReloading()
{
return false;
}

bool FQuilkinModule::SupportsAutomaticShutdown()
{
// Shutdown gets called by the SocketSubsystem, if we were registered (and we don't do anything if we weren't)
return false;
}

IMPLEMENT_MODULE(FQuilkinModule, Quilkin);
19 changes: 19 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "CoreMinimal.h"
#include "Modules/ModuleInterface.h"
#include "QuilkinSocketSubsystem.h"

class FQuilkinModule : public IModuleInterface
{
public:
//~ Begin IModuleInterface interface
virtual void StartupModule() override;
virtual void ShutdownModule() override;
virtual bool SupportsDynamicReloading() override;
virtual bool SupportsAutomaticShutdown() override;
//~ End IModuleInterface Interface

private:
TUniquePtr<FQuilkinSocketSubsystem> QuilkinSocketSubsystem;
};
16 changes: 16 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinPacketHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "QuilkinPacketHandler.h"
#include "QuilkinDelegates.h"


FQuilkinPacketHandler::FQuilkinPacketHandler()
{
if (FQuilkinDelegates::GetQuilkinRoutingToken.IsBound())
{
RoutingToken = FQuilkinDelegates::GetQuilkinRoutingToken.Execute();
}
}

bool FQuilkinPacketHandler::IsEnabled()
{
return RoutingToken.Num() > 0;
}
28 changes: 28 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinPacketHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "CoreMinimal.h"

class FQuilkinPacketHandler
{
public:
FQuilkinPacketHandler();
bool IsEnabled();

FORCEINLINE const FBitWriter Handle(const uint8* Packet, int32 CountBytes)
{
// Add the current packet version.
uint8 PacketVersion = 0;
int PacketVersionNumBytes = 1;

// Reserve enough space for the token and packet version.
FBitWriter NewPacket((CountBytes + RoutingToken.Num() + PacketVersionNumBytes) * 8, true);

NewPacket.Serialize((void*)Packet, CountBytes);
NewPacket.Serialize(RoutingToken.GetData(), RoutingToken.Num());
NewPacket.Serialize(&PacketVersion, PacketVersionNumBytes);
return NewPacket;
}

private:
TArray<uint8> RoutingToken;
};
213 changes: 213 additions & 0 deletions sdks/ue4/Source/Quilkin/Private/QuilkinSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#include "QuilkinSocket.h"

FQuilkinSocket::FQuilkinSocket(FUniqueSocket WrappedSocket, ESocketType InSocketType, const FString& InSocketDescription, const FName& InSocketProtocol)
: FSocket(InSocketType, InSocketDescription, InSocketProtocol)
, Socket{MoveTemp(WrappedSocket)}
, Handler{}
{
}

FQuilkinSocket::~FQuilkinSocket()
{
}

bool FQuilkinSocket::Shutdown(ESocketShutdownMode Mode)
{
return Socket.Get()->Shutdown(Mode);
}

bool FQuilkinSocket::Close()
{
return Socket.Get()->Close();
}

bool FQuilkinSocket::Bind(const FInternetAddr& Addr)
{
return Socket.Get()->Bind(Addr);
}

bool FQuilkinSocket::Connect(const FInternetAddr& Addr)
{
return Socket.Get()->Connect(Addr);
}

bool FQuilkinSocket::Listen(int32 MaxBacklog)
{
return Socket.Get()->Listen(MaxBacklog);
}

bool FQuilkinSocket::WaitForPendingConnection(bool& bHasPendingConnection, const FTimespan& WaitTime)
{
return Socket.Get()->WaitForPendingConnection(bHasPendingConnection, WaitTime);
}

bool FQuilkinSocket::HasPendingData(uint32& PendingDataSize)
{
return Socket.Get()->HasPendingData(PendingDataSize);
}

FSocket* FQuilkinSocket::Accept(const FString& InSocketDescription)
{
return Socket.Get()->Accept(InSocketDescription);
}

FSocket* FQuilkinSocket::Accept(FInternetAddr& OutAddr, const FString& InSocketDescription)
{
return Socket.Get()->Accept(OutAddr, InSocketDescription);
}

bool FQuilkinSocket::SendTo(const uint8* Data, int32 Count, int32& BytesSent, const FInternetAddr& Destination)
{
if (!Handler.IsEnabled())
{
return Socket.Get()->SendTo(Data, Count, BytesSent, Destination);
}

FBitWriter Packet;
Packet = Handler.Handle(Data, Count);
return Socket.Get()->SendTo(Packet.GetData(), Packet.GetNumBytes(), BytesSent, Destination);
}

bool FQuilkinSocket::Send(const uint8* Data, int32 Count, int32& BytesSent)
{
if (!Handler.IsEnabled())
{
return Socket.Get()->Send(Data, Count, BytesSent);
}

FBitWriter Packet;
Packet = Handler.Handle(Data, Count);
return Socket.Get()->Send(Packet.GetData(), Packet.GetNumBytes(), BytesSent);
}

bool FQuilkinSocket::RecvFrom(uint8* Data, int32 BufferSize, int32& BytesRead, FInternetAddr& Source, ESocketReceiveFlags::Type Flags)
{
return Socket.Get()->RecvFrom(Data, BufferSize, BytesRead, Source, Flags);
}

bool FQuilkinSocket::Recv(uint8* Data, int32 BufferSize, int32& BytesRead, ESocketReceiveFlags::Type Flags)
{
return Socket.Get()->Recv(Data, BufferSize, BytesRead, Flags);
}

bool FQuilkinSocket::Wait(ESocketWaitConditions::Type Condition, FTimespan WaitTime)
{
return Socket.Get()->Wait(Condition, WaitTime);
}

ESocketConnectionState FQuilkinSocket::GetConnectionState()
{
return Socket.Get()->GetConnectionState();

}

void FQuilkinSocket::GetAddress(FInternetAddr& OutAddr)
{
return Socket.Get()->GetAddress(OutAddr);
}

bool FQuilkinSocket::GetPeerAddress(FInternetAddr& OutAddr)
{
return Socket.Get()->GetPeerAddress(OutAddr);
}

bool FQuilkinSocket::SetNonBlocking(bool bIsNonBlocking)
{
return Socket.Get()->SetNonBlocking(bIsNonBlocking);

}

bool FQuilkinSocket::SetBroadcast(bool bAllowBroadcast)
{
return Socket.Get()->SetBroadcast(bAllowBroadcast);
}

bool FQuilkinSocket::SetNoDelay(bool bIsNoDelay)
{
return Socket.Get()->SetNoDelay(bIsNoDelay);
}

bool FQuilkinSocket::JoinMulticastGroup(const FInternetAddr& GroupAddress)
{
return Socket.Get()->JoinMulticastGroup(GroupAddress);
}

bool FQuilkinSocket::JoinMulticastGroup(const FInternetAddr& GroupAddress, const FInternetAddr& InterfaceAddress)
{
return Socket.Get()->JoinMulticastGroup(GroupAddress, InterfaceAddress);
}

bool FQuilkinSocket::LeaveMulticastGroup(const FInternetAddr& GroupAddress)
{
return Socket.Get()->LeaveMulticastGroup(GroupAddress);
}

bool FQuilkinSocket::LeaveMulticastGroup(const FInternetAddr& GroupAddress, const FInternetAddr& InterfaceAddress)
{
return Socket.Get()->LeaveMulticastGroup(GroupAddress, InterfaceAddress);
}

bool FQuilkinSocket::SetMulticastLoopback(bool bLoopback)
{
return Socket.Get()->SetMulticastLoopback(bLoopback);
}

bool FQuilkinSocket::SetMulticastTtl(uint8 TimeToLive)
{
return Socket.Get()->SetMulticastTtl(TimeToLive);
}

bool FQuilkinSocket::SetMulticastInterface(const FInternetAddr& InterfaceAddress)
{
return Socket.Get()->SetMulticastInterface(InterfaceAddress);
}

bool FQuilkinSocket::SetReuseAddr(bool bAllowReuse)
{
return Socket.Get()->SetReuseAddr(bAllowReuse);
}

bool FQuilkinSocket::SetLinger(bool bShouldLinger, int32 Timeout)
{
return Socket.Get()->SetLinger(bShouldLinger, Timeout);
}

bool FQuilkinSocket::SetRecvErr(bool bUseErrorQueue)
{
return Socket.Get()->SetRecvErr(bUseErrorQueue);
}

bool FQuilkinSocket::SetSendBufferSize(int32 Size, int32& NewSize)
{
return Socket.Get()->SetSendBufferSize(Size, NewSize);
}

bool FQuilkinSocket::SetReceiveBufferSize(int32 Size, int32& NewSize)
{
return Socket.Get()->SetReceiveBufferSize(Size, NewSize);
}

int32 FQuilkinSocket::GetPortNo()
{
return Socket.Get()->GetPortNo();
}

bool FQuilkinSocket::RecvMulti(FRecvMulti& MultiData, ESocketReceiveFlags::Type Flags)
{
return Socket.Get()->RecvMulti(MultiData, Flags);
}

bool FQuilkinSocket::SetRetrieveTimestamp(bool bRetrieveTimestamp)
{
return Socket.Get()->SetRetrieveTimestamp(bRetrieveTimestamp);
}

bool FQuilkinSocket::SetIpPktInfo(bool bEnable)
{
return Socket.Get()->SetIpPktInfo(bEnable);
}

bool FQuilkinSocket::RecvFromWithPktInfo(uint8* Data, int32 BufferSize, int32& BytesRead, FInternetAddr& Source, FInternetAddr& Destination, ESocketReceiveFlags::Type Flags)
{
return Socket.Get()->RecvFromWithPktInfo(Data, BufferSize, BytesRead, Source, Destination, Flags);
}
Loading