'Simplest way to make this connection handler handle multiple concurrent connections
Kubernetes provides a library for dotnet that exposes what I understand to be the entire Kubernetes API, i.e. everything that kubectl can do.
I want to be able to achieve kubectl port-forward using the Kubernetes dotnet library, and they have an example of how to do so in the repository. Unfortunately, the example can only handle a single concurrent connection.
The k8s library exposes a method `` that returns a WebSocket, which can apparently be separated into its input and output components via the included StreamDemuxer class. So using their example...
// Note this is single-threaded, it won't handle concurrent requests well...
var webSocket = await client.WebSocketNamespacedPodPortForwardAsync(pod.Metadata.Name, "default", new int[] {80}, "v4.channel.k8s.io");
var demux = new StreamDemuxer(webSocket, StreamType.PortForward);
demux.Start();
var stream = demux.GetStream((byte?)0, (byte?)0);
IPAddress ipAddress = IPAddress.Loopback;
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1212);
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(100);
Socket handler = null;
// Note this will only accept a single connection
var accept = Task.Run(() => {
while (true) {
handler = listener.Accept();
var bytes = new byte[4096];
while (true) {
int bytesRec = handler.Receive(bytes);
stream.Write(bytes, 0, bytesRec);
if (bytesRec == 0 || Encoding.ASCII.GetString(bytes,0,bytesRec).IndexOf("<EOF>") > -1) {
break;
}
}
}
});
var copy = Task.Run(() => {
var buff = new byte[4096];
while (true) {
var read = stream.Read(buff, 0, 4096);
handler.Send(buff, read, 0);
}
});
await accept;
await copy;
if (handler != null) {
handler.Close();
}
listener.Close();
Now this works perfectly fine for one connection at a time. But I want to make it handle more than one connection.
I understand that at the end of the day, I have just one Stream from which I may read and write, so the best I think I can do is to timeshare the Stream with all connected clients, where each client may interact with the Stream one at a time. I am okay with that if it is possible.
So my question is... is this possible? Can I let multiple connections share a single Stream without causing all kinds of connection issues? If so, what is the simplest path to do so?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
