Over the last few weeks I’ve been working on a project that I originally set up using Java. I generally loved the way Java writes and reads, it just feels very well thought out.
But I did hate one thing about Java, it’s “new IO” and “SSLEngine” packages.
nio is by far not a new concept, the component actually dates back almost 20 years at the time of writing.
The issue I had was that it does not natively support an SSL based socket, like the standard Socket in Java does. And
SSLEngine is an almost completely undocumented nightmare to work with.
My application accepts incoming sockets, and distributes a payload that it receives from an HTTP server to all the clients connected to it, using a
websocket as transport.
I was a bit torn at first, because my application was working in Java, it was just limited by the fact that the system would manage hundreds or even thousands of threads if the load scaled a little further than just development.
The original application would have two threads per client, which was just an unacceptable amount of overhead for an application intended to serve hundreds of clients.
I started my work on rewriting the application, replacing the old socket system with
nio channels and
SSLEngine. And while the application seemed to start off fine, it would soon have issues that stemmed from
SSLEngine that seemed to somehow read data that it could not handle whenever nothing was connected to it.
Eventually, the browser would start having issues and the system would become unresponsive, making the entire issue very painful.
Then I started off putting together a small test to write an HTTP Server that would support HTTPS connections using C++. In just a matter of hours I had a working prototype that actually supports receiving big POST messages without crashing the application, SSL, the connection, the cURL client or the system.
While C++ is almost completely new to me, and I have never used openssl or the
poll function, it just came together after a few hours of trial and error. The SSL handshake always works and feels consistent, which gives me an overall feeling of confidence in the workings of the application.
I am assuming that all that is now left to me, is migrating the code I had in java that was working. I’m a bit scared of the whole HTTP Server part, SHA256, JSON and HTTP Requests. But I’m overall positive about it.