asio icon indicating copy to clipboard operation
asio copied to clipboard

Can tcp::socket and ssl::stream<tcp::socket> use the same set of interfaces?

Open kahotv opened this issue 3 months ago • 0 comments

In C#, there is a method like this (pseudo code):

class NetworkStream : Stream { ... }
class SslStream : AuthenticatedStream { ... }
class AuthenticatedStream : Stream { ... }

void Read(Stream local)
{
	byte[] buf = new byte[1024];
	while(true)
	{
		int n = await from.ReadAsync(buf, 0, buf.Length);
		...
	}
	
}
//Http:
var stream = new NetworkStream(socket);
//Https:
var stream = new SslStream(new NetworkStream(socket));
//Use:
Trans(stream);

In Asio, I tried this approach, but it requires writing code in the header file. Is there a better way?

template<typename StreamType>
auto Trans(StreamType& local) -> awaitable<void>
{
	char buf[1024];
	while(true)
	{
		size_t n = co_await local.async_read_some(buffer(buf), use_awaitable);
		...
	}
	
}

(I'm writing an HTTP and HTTPS proxy program, and I need to use this method.)

Thanks!

kahotv avatar Oct 25 '24 15:10 kahotv