377 lines
8.4 KiB
C++
377 lines
8.4 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "CPPGameInstance.h"
|
|
#include "Networking/Public/Interfaces/IPv4/IPv4Address.h"
|
|
#include "SocketSubsystem.h"
|
|
#include "Engine/Engine.h"
|
|
#include "Serialization/JsonReader.h"
|
|
#include "Serialization/JsonSerializer.h"
|
|
#include "Misc/FileHelper.h"
|
|
#include "Misc/Paths.h"
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <Windows.h>
|
|
|
|
|
|
/*
|
|
* creates a map of all commands that come from the server
|
|
*/
|
|
void UCPPGameInstance::CreateMapCommand()
|
|
{
|
|
CommandMap.Add(TEXT("waggonvelocity"), 0);
|
|
CommandMap.Add(TEXT("switchstate"), 1);
|
|
CommandMap.Add(TEXT("lightstate"), 2);
|
|
CommandMap.Add(TEXT("createwaggon"), 3);
|
|
CommandMap.Add(TEXT("teleportwaggon"), 4);
|
|
CommandMap.Add(TEXT("connectwagon"), 5);
|
|
CommandMap.Add(TEXT("destroywaggon"), 6);
|
|
CommandMap.Add(TEXT("init"), 99);
|
|
|
|
}
|
|
|
|
|
|
int32 UCPPGameInstance::getNumberFunction(FString Command)
|
|
{
|
|
|
|
auto numberComand = CommandMap.Find(Command);
|
|
return *numberComand;
|
|
}
|
|
|
|
void UCPPGameInstance::createwaggon_Implementation(const FString& value, int32 id)
|
|
{
|
|
}
|
|
|
|
void UCPPGameInstance::waggonvelocity_Implementation(int32 id, float speed, int32 idspline,
|
|
float offset)
|
|
{
|
|
}
|
|
|
|
|
|
void UCPPGameInstance::teleportwaggon_Implementation(int32 id, int32 idspline, float offset)
|
|
{
|
|
}
|
|
void UCPPGameInstance::destroywaggon_Implementation(int32 id)
|
|
{
|
|
}
|
|
|
|
void UCPPGameInstance::connectwagon_Implementation(int32 id, int32 idspline)
|
|
{
|
|
}
|
|
|
|
|
|
bool UCPPGameInstance::StartWorkingAtSocket()
|
|
{
|
|
|
|
MySocket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
|
|
MySocket->SetNonBlocking(true);
|
|
|
|
// set address ad port socket
|
|
//FString address = TEXT("192.168.1.123");
|
|
FIPv4Address ip;
|
|
//int32 port = 6000;
|
|
FIPv4Address::Parse(adress, ip);
|
|
//FIPv4Address ip(192, 168, 1, 165);
|
|
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
|
|
addr->SetIp(ip.Value);
|
|
addr->SetPort(port);
|
|
bool connected = MySocket->Connect(*addr);
|
|
|
|
/*FIPv4Address ip(127, 0, 0, 1);
|
|
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
|
|
addr->SetIp(ip.Value);
|
|
addr->SetPort(6000);
|
|
bool connected = MySocket->Connect(*addr);*/
|
|
|
|
if (connected)
|
|
{
|
|
// if connected = true make 1 byte and send to socket
|
|
UE_LOG(LogTemp, Warning, TEXT("CONNCET"));
|
|
|
|
uint8 b1 = 1;
|
|
int32 BytesSent = 0;
|
|
int32 size = 1;
|
|
|
|
bool successful = MySocket->Send(&b1, size, BytesSent);
|
|
|
|
if (successful)
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("PUSHED 1 byte"));
|
|
|
|
int32 BufferSize = 1;
|
|
int32 BytesRead = 0;
|
|
uint8 Response = NULL;
|
|
|
|
// wait response from server
|
|
bool read = false;
|
|
while (!read)
|
|
{
|
|
MySocket->Recv(&Response, BufferSize, BytesRead);
|
|
if (Response)
|
|
{
|
|
read = true;
|
|
}
|
|
}
|
|
|
|
// if response = 3 server is running
|
|
if (Response == 3)
|
|
{
|
|
size = 8;
|
|
// revers bytes at number 4
|
|
int64 next_size = (int64)4 << 56;
|
|
bool successful2 = MySocket->Send((uint8*)&next_size, size, BytesSent);
|
|
if (successful2)
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("PUSHED 4 INT 64"));
|
|
size = 4;
|
|
int32 b15 = 15;
|
|
bool successful3 = MySocket->Send((uint8*)&b15, size, BytesSent);
|
|
if (successful3)
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("PUSHED 15 INT 32"));
|
|
return true;
|
|
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("NO PUSHED 15 INT 32"));
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("NO PUSHED 4 INT 64"));
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("Response != 3"));
|
|
//return false;
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT(" NO PUSHED 1 byte"));
|
|
return false;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("NOT Connecting"));
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void UCPPGameInstance::cnctwagon_Implementation(int32 ideven, int32 idodd)
|
|
{
|
|
}
|
|
|
|
|
|
bool UCPPGameInstance::SaveTxt(FString SaveTextB, FString FileNameB)
|
|
{
|
|
return FFileHelper::SaveStringToFile(SaveTextB, *(FPaths::ProjectDir() + FileNameB));
|
|
}
|
|
|
|
bool UCPPGameInstance::LoadTxt(FString FileNameA, FString& SaveTextA)
|
|
{
|
|
return FFileHelper::LoadFileToString(SaveTextA, *(FPaths::ProjectDir() + FileNameA));
|
|
}
|
|
|
|
bool UCPPGameInstance::SaveTxtFull(FString SaveText, FString FullFileName)
|
|
{
|
|
return FFileHelper::SaveStringToFile(SaveText, *FullFileName);
|
|
}
|
|
|
|
bool UCPPGameInstance::LoadTxtFull(FString FullFileName, FString& SaveText)
|
|
{
|
|
return FFileHelper::LoadFileToString(SaveText, *FullFileName);
|
|
}
|
|
|
|
void UCPPGameInstance::runSelf(FString argument)
|
|
{
|
|
FPlatformProcess::CreateProc(FPlatformProcess::ExecutableName(false), *argument, true, false, false, NULL, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
void UCPPGameInstance::SetWinPos(int posX, int posY)
|
|
{
|
|
SetWindowPos(
|
|
GetActiveWindow(),
|
|
HWND_TOP,
|
|
posX,
|
|
posY,
|
|
0,
|
|
0,
|
|
SWP_NOSIZE
|
|
);
|
|
}
|
|
|
|
void UCPPGameInstance::SetWinSize(int sizeX, int sizeY)
|
|
{
|
|
SetWindowPos(
|
|
GetActiveWindow(),
|
|
HWND_TOP,
|
|
0,
|
|
0,
|
|
sizeX,
|
|
sizeY,
|
|
SWP_NOMOVE
|
|
);
|
|
}
|
|
|
|
/*
|
|
* the function listening socket server
|
|
* Parameters
|
|
* @param NumStatus status number that will be sent to the server
|
|
* 0 - wait initializing
|
|
* 1 - initialize client
|
|
* 2 - initializing at all clients
|
|
* 3 - in process working
|
|
* 4 - end working
|
|
*/
|
|
void UCPPGameInstance::listening(int32 NumberStatus)
|
|
{
|
|
// push char 105
|
|
char i = '\151';
|
|
int32 size = 1;
|
|
int32 BytesSent = 0;
|
|
MySocket->Send((uint8*)&i, size, BytesSent);
|
|
|
|
|
|
int32 BufferSize = 1;
|
|
int32 BytesRead = 0;
|
|
uint8 Response = NULL;
|
|
|
|
|
|
bool read = false;
|
|
while (!read)
|
|
{
|
|
MySocket->Recv(&Response, BufferSize, BytesRead);
|
|
if (Response == 3)
|
|
{
|
|
//LAstServResp = Response;
|
|
break;
|
|
}
|
|
}
|
|
|
|
SendStatus(NumberStatus);
|
|
}
|
|
|
|
|
|
void UCPPGameInstance::SendStatus(int32 NumberStatus)
|
|
{
|
|
|
|
int32 BytesSentStatus = 0;
|
|
FString status = FString::Printf(TEXT("{\"status\":%d}"), NumberStatus); // create string status client
|
|
|
|
MySocket->Send((uint8*)TCHAR_TO_UTF8(*status), status.Len(), BytesSentStatus);
|
|
|
|
int32 BytesRead = 0;
|
|
TArray<uint8> Response;
|
|
uint32 BufferSize = 0;
|
|
bool read = false;
|
|
FString jsonString;
|
|
|
|
// get json from server and make string json
|
|
while (!read)
|
|
{
|
|
read = MySocket->HasPendingData(BufferSize);
|
|
if (read)
|
|
{
|
|
Response.SetNumUninitialized(BufferSize);
|
|
MySocket->Recv(Response.GetData(), Response.Num(), BytesRead);
|
|
|
|
|
|
for (int i = 0; i < Response.Num(); i++)
|
|
{
|
|
//const TCHAR c = Response[i];
|
|
jsonString.AppendChar(Response[i]);
|
|
}
|
|
|
|
//GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Yellow, jsonString);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// make json object and parsing
|
|
JsonObject = MakeShareable(new FJsonObject);
|
|
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(jsonString);
|
|
bool b1 = FJsonSerializer::Deserialize(Reader, JsonObject);
|
|
bool b2 = JsonObject.IsValid();
|
|
SaveTxt(jsonString, "JSON.json");
|
|
/*if ( JsonObject.IsValid() && NumberStatus == 3)
|
|
{
|
|
// get array all commands
|
|
TArray<TSharedPtr<FJsonValue>> JsonObjectArray = JsonObject->GetArrayField(TEXT("comandarray"));
|
|
|
|
|
|
for (int32 indx = 0; indx < JsonObjectArray.Num(); indx++)
|
|
{
|
|
|
|
TSharedPtr<FJsonValue> value = JsonObjectArray[indx];
|
|
TSharedPtr<FJsonObject> ObjectComand = value->AsObject();
|
|
|
|
//get name command
|
|
FString NameComand = ObjectComand->GetStringField(TEXT("comand"));
|
|
|
|
int32 NumberComand = getNumberFunction(NameComand);
|
|
|
|
switch (NumberComand)
|
|
{
|
|
case 0://waggonvelocity
|
|
{
|
|
waggonvelocity(ObjectComand->GetNumberField(TEXT("id")),
|
|
ObjectComand->GetNumberField(TEXT("speed")),
|
|
ObjectComand->GetNumberField(TEXT("idspline")),
|
|
ObjectComand->GetNumberField(TEXT("offset")));
|
|
break;
|
|
}
|
|
case 1://switchstate
|
|
|
|
break;
|
|
case 2://lightstate
|
|
|
|
break;
|
|
case 3://createwaggon
|
|
{
|
|
FString typeTrain = ObjectComand->GetStringField(TEXT("value"));
|
|
createwaggon(typeTrain,ObjectComand->GetNumberField(TEXT("id")));
|
|
break;
|
|
}
|
|
case 4://teleportwaggon
|
|
{
|
|
teleportwaggon(ObjectComand->GetNumberField(TEXT("id")),
|
|
ObjectComand->GetNumberField(TEXT("idspline")),
|
|
ObjectComand->GetNumberField(TEXT("offset")));
|
|
break;
|
|
}
|
|
case 5://connectwagon
|
|
{
|
|
cnctwagon(ObjectComand->GetNumberField(TEXT("ideven")),
|
|
ObjectComand->GetNumberField(TEXT("idodd")));
|
|
//cnctwagon(ObjectComand->GetNumberField(TEXT("ideven")),
|
|
//ObjectComand->GetNumberField(TEXT("idodd")));
|
|
|
|
break;
|
|
}
|
|
case 6:
|
|
{
|
|
destroywaggon(ObjectComand->GetNumberField(TEXT("id")));
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}else
|
|
{
|
|
return;
|
|
}*/
|
|
} |