uh stuff. i'm putting it in the readme.
This commit is contained in:
squeaksies
2018-10-30 22:18:39 -07:00
parent 0c2fc5823d
commit 08645a1dfd
230 changed files with 9892 additions and 301 deletions

View File

@@ -0,0 +1,15 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Socketer.h"
#include "Socket.h"
bool USocket::SetSocket(FSocket* Socket)
{
_Socket = Socket;
return false;
}
FSocket* USocket::GetSocket()
{
return _Socket;
}

View File

@@ -0,0 +1,22 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "Socketer.h"
#define LOCTEXT_NAMESPACE "FSocketerModule"
void FSocketerModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
}
void FSocketerModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FSocketerModule, Socketer)

View File

@@ -0,0 +1,186 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "Socketer.h"
#include "SocketerBPLibrary.h"
USocketerBPLibrary::USocketerBPLibrary(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
float USocketerBPLibrary::SocketerSampleFunction(float Param)
{
return -1;
}
USocket* USocketerBPLibrary::Connect(FString IP, int32 port, bool& success)
{
// Create an FSocket pointer to work with and an USocke pointer to return.
FSocket* MySockTemp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("Socketer Managed TCP Socket"), false);
USocket* NetSock = NewObject<USocket>();
// Create & set a variable to store the parsed ip address
FIPv4Address ipv4ip;
FIPv4Address::Parse(IP, ipv4ip);
// Now combine that with the port to create the address
TSharedRef<FInternetAddr> SockAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr(ipv4ip.Value, port);
// Attempt to connect, and store if it succeeded in a variable
bool connected = MySockTemp->Connect(*SockAddr);
// Verify it is connected
if (!connected)
{
// And if not log an error and return an error
UE_LOG(LogTemp, Error, TEXT("Could not connect"));
success = false;
return nullptr;
}
// Set the UObject wrappers its socket to the connected one
NetSock->SetSocket(MySockTemp);
success = true;
return NetSock;
}
bool USocketerBPLibrary::SendMessage(USocket * Connection, FString Message)
{
// If the passed in socket is not valid
if (!IsValid(Connection))
{
return false;
}
// Set an FSocket pointer to the socket inside of the passed in USocket
FSocket* MySocket = Connection->GetSocket();
// Check if it is not a null pointer
if (MySocket == nullptr)
{
return false;
}
// Serialize the message
TCHAR *serializedChar = Message.GetCharArray().GetData();
// Get / setup parameters
int32 size = FCString::Strlen(serializedChar);
int32 sent = 0;
// Send the message
bool successful = MySocket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);
// And check if there was an error
if (!successful)
{
UE_LOG(LogTemp, Error, TEXT("Error sending message!!"));
return false;
}
else
{
return true;
}
}
bool USocketerBPLibrary::GetMessage(USocket* Connection, FString &Message)
{
// If the passed in socket is not valid
if (!IsValid(Connection))
{
return false;
}
// Set an FSocket pointer to the socket inside of the passed in USocket
FSocket* MySocket = Connection->GetSocket();
// Check if it is not a null pointer
if (MySocket == nullptr)
{
return false;
}
// Credit to RAMA for this converter!
//Binary Array!
TArray<uint8> BinaryData;
uint32 Size;
while (MySocket->HasPendingData(Size))
{
// Be sure that the array doesn't become absolutely insanely large
BinaryData.Init(0, FMath::Min(Size, 65507u));
// Set the counter for the ammount of bytes read to 0
int32 Read = 0;
// Recieve the data from the socket and put it into the binary array
MySocket->Recv(BinaryData.GetData(), BinaryData.Num(), Read);
}
// Check if there was actually data read into the array
if (BinaryData.Num() <= 0)
{
// No data was read!
UE_LOG(LogTemp, Warning, TEXT("No data to read!"));
return false;
}
else
{
// Be sure to \0 terminate the array
BinaryData.Add(0);
// Convert it to an fstring and set the passed in message parameter
Message = FString(ANSI_TO_TCHAR(reinterpret_cast<const char*>(BinaryData.GetData())));
return true;
}
}
bool USocketerBPLibrary::HasPendingData(USocket * Connection)
{
// If the passed in socket is not valid
if (!IsValid(Connection))
{
return false;
}
// Set an FSocket pointer to the socket inside of the passed in USocket
FSocket* MySocket = Connection->GetSocket();
// Check if it is not a null pointer
if (MySocket == nullptr)
{
return false;
}
// Unused but required for the function to work
uint32 Size;
// Return if the socket has pending data
return MySocket->HasPendingData(Size);
}
bool USocketerBPLibrary::CloseConnection(USocket * Connection)
{
// If the passed in socket is not valid
if (!IsValid(Connection))
{
return false;
}
// Set an FSocket pointer to the socket inside of the passed in USocket
FSocket* MySocket = Connection->GetSocket();
// Check if it is not a null pointer
if (MySocket == nullptr)
{
return false;
}
// Attempt to close it and return if it was successful or not
return MySocket->Close();
}

View File

@@ -0,0 +1,25 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "UObject/NoExportTypes.h"
#include "Runtime/Networking/Public/Networking.h"
#include "Runtime/Sockets/Public/Sockets.h"
#include "Runtime/Sockets/Public/SocketSubsystem.h"
#include "Socket.generated.h"
/**
*
*/
UCLASS(BlueprintType)
class SOCKETER_API USocket : public UObject
{
GENERATED_BODY()
public:
bool SetSocket(FSocket* Socket);
FSocket* GetSocket();
private:
FSocket* _Socket;
};

View File

@@ -0,0 +1,14 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "ModuleManager.h"
class FSocketerModule : public IModuleInterface
{
public:
/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};

View File

@@ -0,0 +1,51 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Engine.h"
#include "Socket.h"
#include "Runtime/Networking/Public/Networking.h"
#include "Runtime/Sockets/Public/Sockets.h"
#include "Runtime/Sockets/Public/SocketSubsystem.h"
#include "SocketerBPLibrary.generated.h"
/*
* Function library class.
* Each function in it is expected to be static and represents blueprint node that can be called in any blueprint.
*
* When declaring function you can define metadata for the node. Key function specifiers will be BlueprintPure and BlueprintCallable.
* BlueprintPure - means the function does not affect the owning object in any way and thus creates a node without Exec pins.
* BlueprintCallable - makes a function which can be executed in Blueprints - Thus it has Exec pins.
* DisplayName - full name of the node, shown when you mouse over the node and in the blueprint drop down menu.
* Its lets you name the node using characters not allowed in C++ function names.
* CompactNodeTitle - the word(s) that appear on the node.
* Keywords - the list of keywords that helps you to find node when you search for it using Blueprint drop-down menu.
* Good example is "Print String" node which you can find also by using keyword "log".
* Category - the category your node will be under in the Blueprint drop-down menu.
*
* For more info on custom blueprint nodes visit documentation:
* https://wiki.unrealengine.com/Custom_Blueprint_Node_Creation
*/
UCLASS()
class USocketerBPLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Execute Sample function", Keywords = "Socketer sample test testing"), Category = "SocketerTesting")
static float SocketerSampleFunction(float Param);
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Connect to a TCP server", Keywords = "Socketer connect tcp tcpconnect socketerconnect"), Category = "Networking|Socketer")
static USocket* Connect(FString IP, int32 port, bool &success);
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Send message to the server", Keywords = "Socketer send message tcpsend tcp tcpdisconnect socketersend"), Category = "Networking|Socketer")
static bool SendMessage(USocket* Connection, FString Message);
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get buffer (converted to FText) from server", Keywords = "Socketer send message tcpsend tcp tcpdisconnect socketersend"), Category = "Networking|Socketer")
static bool GetMessage(USocket* Connection, FString &Message);
UFUNCTION(BlueprintCallable, BlueprintPure, meta = (DisplayName = "HasPendingData", Keywords = "Socketer send message tcpsend tcp tcpdisconnect socketersend"), Category = "Networking|Socketer")
static bool HasPendingData(USocket* Connection);
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Close connection to TCP server", Keywords = "Socketer disconnect close tcpclose tcp tcpdisconnect socketerdisconnect"), Category = "Networking|Socketer")
static bool CloseConnection(USocket* Connection);
};

View File

@@ -0,0 +1,60 @@
// Some copyright should be here...
using UnrealBuildTool;
public class Socketer : ModuleRules
{
public Socketer(ReadOnlyTargetRules Target) : base(Target)
{
PublicIncludePaths.AddRange(
new string[] {
"Socketer/Public"
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
"Socketer/Private",
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"Sockets",
"Networking",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}