Advertisement
Guest User

Rama Code

a guest
May 29th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "ASocketerTwo.h"
  4. #include "TimerManager.h"
  5. #include <string>
  6.  
  7.  
  8. //// Sets default values
  9. AASocketerTwo::AASocketerTwo()
  10. {
  11. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  12. PrimaryActorTick.bCanEverTick = true;
  13. }
  14. //
  15. //// Called when the game starts or when spawned
  16. void AASocketerTwo::BeginPlay()
  17. {
  18. Super::BeginPlay();
  19. // ***** You MUST use a local IP or loopback IP otherwise ListenSocket returns nullptr ******
  20. if (!StartTCPReceiver("RamaSocketListener", "10.80.12.148", 8309))
  21. {
  22. UE_LOG(LogTemp, Warning, TEXT("TCP Socket Listener Failed!"));
  23. return;
  24. }
  25. }
  26. //
  27. //// Called every frame
  28. void AASocketerTwo::Tick(float DeltaTime)
  29. {
  30. Super::Tick(DeltaTime);
  31. }
  32. //
  33. //Rama's Start TCP Receiver
  34. bool AASocketerTwo::StartTCPReceiver(
  35. const FString& YourChosenSocketName,
  36. const FString& TheIP,
  37. const int32 ThePort
  38. )
  39. {
  40. //Rama's CreateTCPConnectionListener
  41. ListenerSocket = CreateTCPConnectionListener(YourChosenSocketName, TheIP, ThePort);
  42.  
  43. //Not created?
  44. if (!ListenerSocket)
  45. {
  46. UE_LOG(LogTemp, Error, TEXT("StartTCPReceiver>> Listen socket could not be created! ~> %s %d"), *TheIP, ThePort);
  47. return false;
  48. }
  49. UE_LOG(LogTemp, Warning, TEXT("StartTCPReceiver>> Listen socket created! ~> %s %d"), *TheIP, ThePort);
  50.  
  51. //TODO: Start the Listener! //thread this eventually
  52. GetWorldTimerManager().SetTimer(ConnectionListenTimer, this, &AASocketerTwo::TCPConnectionListener, 0.01f, true);
  53.  
  54. return true;
  55. }
  56. //Format IP String as Number Parts
  57. bool AASocketerTwo::FormatIP4ToNumber(const FString& TheIP, uint8(&Out)[4])
  58. {
  59. //IP Formatting
  60. TheIP.Replace(TEXT(" "), TEXT(""));
  61. const TCHAR* delim;
  62. delim = TEXT(".");
  63.  
  64. TArray<FString> Parts;
  65. TheIP.ParseIntoArray(Parts, delim, true);
  66. if (Parts.Num() != 4)
  67. return false;
  68.  
  69. for (int32 i = 0; i < 4; ++i)
  70. {
  71. Out[i] = FCString::Atoi(*Parts[i]);
  72. }
  73. return true;
  74. }
  75. //Rama's Create TCP Connection Listener
  76. FSocket* AASocketerTwo::CreateTCPConnectionListener(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort, const int32 ReceiveBufferSize)
  77. {
  78. uint8 IP4Nums[4];
  79. if (!FormatIP4ToNumber(TheIP, IP4Nums))
  80. {
  81. UE_LOG(LogTemp, Error, TEXT("Invalid IP! Expecting 4 parts separated by ."));
  82. return false;
  83. }
  84. //Create Socket
  85. FIPv4Endpoint Endpoint(FIPv4Address(IP4Nums[0], IP4Nums[1], IP4Nums[2], IP4Nums[3]), ThePort);
  86. FSocket* ListenSocket = FTcpSocketBuilder(*YourChosenSocketName)
  87. .AsReusable()
  88. .BoundToEndpoint(Endpoint)
  89. .Listening(8);
  90. //Set Buffer Size
  91. int32 NewSize = 0;
  92. ListenSocket->SetReceiveBufferSize(ReceiveBufferSize, NewSize);
  93. //Done!
  94. return ListenSocket;
  95. }
  96. //Rama's TCP Connection Listener
  97. void AASocketerTwo::TCPConnectionListener()
  98. {
  99. //~~~~~~~~~~~~~
  100. //UE_LOG(LogTemp, Warning, TEXT("Checking socket..."));
  101. if (!ListenerSocket)
  102. {
  103. UE_LOG(LogTemp, Error, TEXT("Listener doesn't exist, but should."));
  104. return;
  105. }
  106.  
  107. //Remote address
  108. TSharedRef<FInternetAddr> RemoteAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
  109. bool Pending;
  110.  
  111. // handle incoming connections
  112. if (ListenerSocket->HasPendingConnection(Pending) && Pending)
  113. {
  114. UE_LOG(LogTemp, Warning, TEXT("Connection Pending..."));
  115. //Already have a Connection? destroy previous
  116. if (ConnectionSocket)
  117. {
  118. ConnectionSocket->Close();
  119. ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ConnectionSocket);
  120. }
  121. //New Connection receive!
  122. ConnectionSocket = ListenerSocket->Accept(*RemoteAddress, TEXT("TODO: Start Kick off Event"));
  123.  
  124. if (ConnectionSocket != NULL)
  125. {
  126. //Global cache of current Remote Address
  127. RemoteAddressForConnection = FIPv4Endpoint(RemoteAddress);
  128. //UE_LOG "Accepted Connection! WOOOHOOOO!!!";
  129. UE_LOG(LogTemp, Warning, TEXT("Accepted Connection!"));
  130. //can thread this too
  131. GetWorldTimerManager().SetTimer(SocketListenTimer, this, &AASocketerTwo::TCPSocketListener, 0.01f, true);
  132. }
  133. }
  134. }
  135.  
  136. FString AASocketerTwo::StringFromBinaryArray(const TArray<uint8>& BinaryArray)
  137. {
  138. //Create a string from a byte array!
  139. std::string cstr(reinterpret_cast<const char*>(BinaryArray.GetData()), BinaryArray.Num());
  140. return FString(cstr.c_str());
  141. }
  142.  
  143. //Rama's TCP Socket Listener
  144. void AASocketerTwo::TCPSocketListener()
  145. {
  146. //~~~~~~~~~~~~~
  147. if (!ConnectionSocket) return;
  148.  
  149. //Binary Array!
  150. TArray<uint8> ReceivedData;
  151. uint32 Size;
  152. while (ConnectionSocket->HasPendingData(Size))
  153. {
  154. ReceivedData.Init(0.0f, FMath::Min(Size, 65507u));
  155.  
  156. int32 Read = 0;
  157. ConnectionSocket->Recv(ReceivedData.GetData(), ReceivedData.Num(), Read);
  158.  
  159. UE_LOG(LogTemp, Warning, TEXT("Data Read! %d"), ReceivedData.Num());
  160. }
  161. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162. if (ReceivedData.Num() <= 0)
  163. {
  164. //No Data Received
  165. return;
  166. }
  167. const FString ReceivedUE4String = StringFromBinaryArray(ReceivedData);
  168. //Logging
  169. UE_LOG(LogTemp, Warning, TEXT("Total Data read! %d"), ReceivedData.Num());
  170. UE_LOG(LogTemp, Warning, TEXT("As String!!!!! ~> %s"), *ReceivedUE4String);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement