/src/actor/NpuClientActor.cpp

https://bitbucket.org/b7nw5s/neplusultra · C++ · 90 lines · 66 code · 14 blank · 10 comment · 9 complexity · 2851f42ce5420c39ad4ed0b52b29624b MD5 · raw file

  1. #include "NpuClientActor.h"
  2. #include "util/DetectOS.h"
  3. #if OS_POSIX_X11 || OS_APPLE_OSX
  4. #include <unistd.h>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <netdb.h>
  8. #else
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #include <wspiapi.h>
  12. #endif
  13. #include <memory>
  14. #include <string>
  15. #include <iostream>
  16. #include <sstream>
  17. #include <udt4/udt.h>
  18. #include "socket/NpuSocket.h"
  19. #include "NpuConfig.h"
  20. #include "NpuErrorCode.h"
  21. namespace npu {
  22. NpuClientActor::NpuClientActor(const NpuConfig& config) : NpuActor(config) {
  23. }
  24. NpuClientActor::~NpuClientActor() {
  25. }
  26. int NpuClientActor::execute() {
  27. struct addrinfo hints, *local, *remote;
  28. memset(&hints, 0, sizeof(struct addrinfo));
  29. hints.ai_flags = AI_PASSIVE;
  30. hints.ai_family = AF_INET;
  31. hints.ai_socktype = SOCK_STREAM;
  32. std::stringstream localService;
  33. localService << config::DEFAULT_PORT;
  34. if (0 != getaddrinfo(NULL, localService.str().c_str(), &hints, &local)) {
  35. std::cout << "invalid local network address" << std::endl;
  36. return ERR_INVALID_LOCAL_SOCKET;
  37. }
  38. UDTSOCKET udtClientSocket = UDT::socket(local->ai_family, local->ai_socktype, local->ai_protocol);
  39. // UDT Options
  40. //UDT::setsockopt(udtClientSocket, 0, UDT_CC, new CCCFactory<CUDPBlast>(), sizeof(CCCFactory<CUDPBlast>));
  41. //UDT::setsockopt(udtClientSocket, 0, UDT_MSS, new int(9000), sizeof(int));
  42. //UDT::setsockopt(udtClientSocket, 0, UDT_SNDBUF, new int(10000000), sizeof(int));
  43. //UDT::setsockopt(udtClientSocket, 0, UDP_SNDBUF, new int(10000000), sizeof(int));
  44. //UDT::setsockopt(udtClientSocket, 0, UDT_MAXBW, new int64_t(12500000), sizeof(int));
  45. freeaddrinfo(local);
  46. // Windows UDP issue
  47. // For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
  48. #if OS_MS_WINDOWS
  49. UDT::setsockopt(udtClientSocket, 0, UDT_MSS, new int(1052), sizeof(int));
  50. #endif
  51. std::stringstream remoteService;
  52. remoteService << config().port();
  53. if (0 != getaddrinfo(config().remoteHost().c_str(), remoteService.str().c_str(), &hints, &remote)) {
  54. std::cout << "invalid server address: " << config().remoteHost() << ":" << config().port() << std::endl;
  55. return ERR_INVALID_REMOTE_SOCKET;
  56. }
  57. // Connect to the server, with implicit bind.
  58. if (UDT::ERROR == UDT::connect(udtClientSocket, remote->ai_addr, remote->ai_addrlen)) {
  59. freeaddrinfo(remote);
  60. std::cout << "socket connect failed: " << UDT::getlasterror().getErrorMessage() << std::endl;
  61. return ERR_CONNECT_FAILED;
  62. }
  63. freeaddrinfo(remote);
  64. std::unique_ptr<NpuSocket> npuSocket(new NpuSocket(udtClientSocket));
  65. doExecute(*npuSocket);
  66. // Close the socket.
  67. if (UDT::getsockstate(udtClientSocket) == CONNECTED) {
  68. npuSocket->sendCommand(CMD_CLOSE);
  69. }
  70. UDT::close(udtClientSocket);
  71. return SUCCESS;
  72. }
  73. const std::string NpuClientActor::toString() const {
  74. return "ClientActor";
  75. }
  76. }