aboutsummaryrefslogtreecommitdiff
path: root/src/zenhorde/hordetransport.cpp
blob: 69766e73e02804653409da7466d177ac7c92d8ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright Epic Games, Inc. All Rights Reserved.

#include "hordetransport.h"

#include <zencore/logging.h>
#include <zencore/trace.h>

ZEN_THIRD_PARTY_INCLUDES_START
#include <asio.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

#if ZEN_PLATFORM_WINDOWS
#	undef SendMessage
#endif

namespace zen::horde {

// ComputeTransport base

bool
ComputeTransport::SendMessage(const void* Data, size_t Size)
{
	const uint8_t* Ptr		 = static_cast<const uint8_t*>(Data);
	size_t		   Remaining = Size;

	while (Remaining > 0)
	{
		const size_t Sent = Send(Ptr, Remaining);
		if (Sent == 0)
		{
			return false;
		}
		Ptr += Sent;
		Remaining -= Sent;
	}

	return true;
}

bool
ComputeTransport::RecvMessage(void* Data, size_t Size)
{
	uint8_t* Ptr	   = static_cast<uint8_t*>(Data);
	size_t	 Remaining = Size;

	while (Remaining > 0)
	{
		const size_t Received = Recv(Ptr, Remaining);
		if (Received == 0)
		{
			return false;
		}
		Ptr += Received;
		Remaining -= Received;
	}

	return true;
}

// TcpComputeTransport - ASIO pimpl

struct TcpComputeTransport::Impl
{
	asio::io_context	  IoContext;
	asio::ip::tcp::socket Socket;

	Impl() : Socket(IoContext) {}
};

// Uses ASIO in synchronous mode only — no async operations or io_context::run().
// The io_context is only needed because ASIO sockets require one to be constructed.
TcpComputeTransport::TcpComputeTransport(const MachineInfo& Info)
: m_Impl(std::make_unique<Impl>())
, m_Log(zen::logging::Get("horde.transport"))
{
	ZEN_TRACE_CPU("TcpComputeTransport::Connect");

	asio::error_code Ec;

	const asio::ip::address Address = asio::ip::make_address(Info.GetConnectionAddress(), Ec);
	if (Ec)
	{
		ZEN_WARN("invalid address '{}': {}", Info.GetConnectionAddress(), Ec.message());
		m_HasErrors = true;
		return;
	}

	const asio::ip::tcp::endpoint Endpoint(Address, Info.GetConnectionPort());

	m_Impl->Socket.connect(Endpoint, Ec);
	if (Ec)
	{
		ZEN_WARN("failed to connect to Horde compute [{}:{}]: {}", Info.GetConnectionAddress(), Info.GetConnectionPort(), Ec.message());
		m_HasErrors = true;
		return;
	}

	// Disable Nagle's algorithm for lower latency
	m_Impl->Socket.set_option(asio::ip::tcp::no_delay(true), Ec);
}

TcpComputeTransport::~TcpComputeTransport()
{
	Close();
}

bool
TcpComputeTransport::IsValid() const
{
	return m_Impl && m_Impl->Socket.is_open() && !m_HasErrors && !m_IsClosed;
}

size_t
TcpComputeTransport::Send(const void* Data, size_t Size)
{
	if (!IsValid())
	{
		return 0;
	}

	asio::error_code Ec;
	const size_t	 Sent = m_Impl->Socket.send(asio::buffer(Data, Size), 0, Ec);

	if (Ec)
	{
		m_HasErrors = true;
		return 0;
	}

	return Sent;
}

size_t
TcpComputeTransport::Recv(void* Data, size_t Size)
{
	if (!IsValid())
	{
		return 0;
	}

	asio::error_code Ec;
	const size_t	 Received = m_Impl->Socket.receive(asio::buffer(Data, Size), 0, Ec);

	if (Ec)
	{
		return 0;
	}

	return Received;
}

void
TcpComputeTransport::MarkComplete()
{
}

void
TcpComputeTransport::Close()
{
	if (!m_IsClosed && m_Impl && m_Impl->Socket.is_open())
	{
		asio::error_code Ec;
		m_Impl->Socket.shutdown(asio::ip::tcp::socket::shutdown_both, Ec);
		m_Impl->Socket.close(Ec);
	}
	m_IsClosed = true;
}

}  // namespace zen::horde