aboutsummaryrefslogtreecommitdiff
path: root/src/zenhorde/hordetransport.h
blob: b5e841d7a5a8b099601273cff1bd016179309fc5 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zenhorde/hordeclient.h>

#include <zencore/logbase.h>

#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <system_error>

namespace asio {
class io_context;
}

namespace zen::horde {

/** Handler types for async transport operations. */
using AsyncConnectHandler = std::function<void(const std::error_code&)>;
using AsyncIoHandler	  = std::function<void(const std::error_code&, size_t)>;

/** Abstract base for asynchronous compute transports.
 *
 *  All callbacks are invoked on the io_context that was provided at construction.
 *  Callers are responsible for strand serialization if needed.
 */
class AsyncComputeTransport
{
public:
	virtual ~AsyncComputeTransport() = default;

	virtual bool IsValid() const = 0;

	/** Asynchronous write of exactly Size bytes. Handler called on completion or error. */
	virtual void AsyncWrite(const void* Data, size_t Size, AsyncIoHandler Handler) = 0;

	/** Asynchronous read of exactly Size bytes into Data. Handler called on completion or error. */
	virtual void AsyncRead(void* Data, size_t Size, AsyncIoHandler Handler) = 0;

	virtual void Close() = 0;
};

/** Async TCP transport using ASIO.
 *
 *  Connects to the Horde compute endpoint and provides async send/receive.
 *  The socket is created on a caller-provided io_context (shared across agents).
 */
class AsyncTcpComputeTransport final : public AsyncComputeTransport
{
public:
	/** Construct a transport on the given io_context. Does not connect yet. */
	explicit AsyncTcpComputeTransport(asio::io_context& IoContext);
	~AsyncTcpComputeTransport() override;

	/** Asynchronously connect to the endpoint and send the nonce. */
	void AsyncConnect(const MachineInfo& Info, AsyncConnectHandler Handler);

	bool IsValid() const override;
	void AsyncWrite(const void* Data, size_t Size, AsyncIoHandler Handler) override;
	void AsyncRead(void* Data, size_t Size, AsyncIoHandler Handler) override;
	void Close() override;

private:
	LoggerRef Log() { return m_Log; }

	struct Impl;
	std::unique_ptr<Impl> m_Impl;
	LoggerRef			  m_Log;
	bool				  m_IsClosed  = false;
	bool				  m_HasErrors = false;
};

}  // namespace zen::horde