Newer
Older
SkyFrontier-Project-IADE-UE4-3D / Source / SkyFrontier / Private / MatchmakingJob.cpp
@Genexuz Genexuz on 10 Jan 2023 4 KB Cleanup
  1. #include "MatchmakingJob.h"
  2. #include "Sockets.h"
  3. #include "Interfaces/IPv4/IPv4Endpoint.h"
  4. #pragma optimize("", off)
  5. MatchmakingJob::MatchmakingJob(FString& stateToWriteInto)
  6. : currentState(stateToWriteInto)
  7. {}
  8. bool MatchmakingJob::Init()
  9. {
  10. const FIPv4Endpoint RemoteAddressForConnection = FIPv4Endpoint(FIPv4Address(127, 0, 0, 1), 1336);
  11. ConnectionSocket = ISocketSubsystem::Get()->CreateSocket("Stream", "MatchmakingServer");
  12. if(!ConnectionSocket)
  13. {
  14. return false;
  15. }
  16. const TSharedRef<FInternetAddr> Address = ISocketSubsystem::Get()->CreateInternetAddr(RemoteAddressForConnection.Address.Value, RemoteAddressForConnection.Port);
  17. return ConnectionSocket->Connect(Address.Get());
  18. }
  19. uint32 MatchmakingJob::Run()
  20. {
  21. // Let's Start Connecting To The Matchmaking Server And Send Some Requests!
  22. currentState = "Connected To matchmaking Server!";
  23. // As the system is super fast we're adding these in here to showcase the system working, you'd never use them otherwise
  24. SleepThread();
  25. #pragma region SendRequest
  26. // Let's Send A Request!
  27. // In this case we use a super simple way to send messages across, but we can add more to the string to parameterize certain things like gamemodes etc just make sure the server knows how to read the parameters
  28. // (you make those rules)
  29. // Creation of scope to reuse variable names for this example
  30. FString Serialized = FString::FromInt(CLIENT_MESSAGE_REQUESTCONNECTION);
  31. const TCHAR* serializedChar = Serialized.GetCharArray().GetData();
  32. int32 Size = FCString::Strlen(serializedChar);
  33. int32 Sent = 0;
  34. // This should be a while until you've sent all the bytes through, as this is super simple it'll always send the full message
  35. if(!ConnectionSocket->Send(reinterpret_cast<uint8*>(TCHAR_TO_UTF8(serializedChar)), Size, Sent))
  36. {
  37. // Some Error Message
  38. JobCompletedEvent.Broadcast(false, "");
  39. return 1; // Returns an exit code, you can use this to figure out where something failed or the type of failure
  40. }
  41. // Let The UI Know What's Going On
  42. currentState = "Request Sent! Waiting For Server To Find Match";
  43. SleepThread();
  44. #pragma endregion
  45. #pragma region ReceiveReply
  46. // Now The Request Went To The Server, Let's Read And Wait For Their Reply!
  47. uint8 ReceiveBuffer[128];
  48. FMemory::Memset(ReceiveBuffer, 0, sizeof(uint8) * 128);
  49. int32 BytesRead = 0;
  50. FString FullMessage = "";
  51. // Again Our Examples Are Small But Bigger Data Would Be Read In Chunks And Reconstructed
  52. // Block and wait for a message to return, then read all remaining bits of said message
  53. if(!ConnectionSocket->Recv(ReceiveBuffer, 128, BytesRead))
  54. {
  55. // Some Error Use something like FScoket::GetLastError (I dont remember the exact syntax) ESocketError Return Type
  56. JobCompletedEvent.Broadcast(false, "");
  57. return 2;
  58. }
  59. // Reinterpret the bits back into a const char* and apply it to the FString
  60. FullMessage.Append(reinterpret_cast<const char*>(ReceiveBuffer));
  61. currentState = "Ready To Connect To Server!: " + FullMessage;
  62. FPlatformProcess::Sleep(1.0f);
  63. #pragma endregion
  64. #pragma region SendConfirmation
  65. // Tell The Server We're Done!
  66. // Reusing Variables, you could and SHOULD place these into smaller easier to read functions
  67. Serialized = TEXT( "" + FString::FromInt(CLIENT_MESSAGE_CONFIRMDONE) );
  68. serializedChar = Serialized.GetCharArray().GetData();
  69. Size = FCString::Strlen(serializedChar);
  70. Sent = 0;
  71. // This should be a while until you've sent all the bytes through, as this is super simple it'll always send the full message
  72. if(!ConnectionSocket->Send(reinterpret_cast<uint8*>(TCHAR_TO_UTF8(serializedChar)), Size, Sent))
  73. {
  74. // Some Error Message
  75. JobCompletedEvent.Broadcast(false, "");
  76. return 3;
  77. }
  78. #pragma endregion
  79. JobCompletedEvent.Broadcast(true, FullMessage);
  80. return 0; // return the error! None in this case! :D
  81. }
  82. void MatchmakingJob::Exit()
  83. {
  84. FRunnable::Exit();
  85. }
  86. void MatchmakingJob::Stop()
  87. {
  88. FRunnable::Stop();
  89. }
  90. void MatchmakingJob::SleepThread()
  91. {
  92. #if !UE_BUILD_SHIPPING
  93. FPlatformProcess::Sleep(1.0f);
  94. #endif
  95. }
  96. #pragma optimize("", on)