I am trying to design a software in c++ that will send request bytes (following a standard **application level** protocol whose fields to be populated will be taken from a text file) using **UDP protocol**.
Now this client must be able to send these requests at very high rate..upto **2000 transactions per second** and should also receive the response if it gets within a specified timeout else don’t receive it
I will be using boost library for all the socket things but I am not sure about the design of it for such high speed application 🙁
I think I have to use a highly multi-threaded application (again Boost will be used). Am I right ? Do I have to create a seperate thread for each request ? But I think only one thread must be waiting to recieve the response else if many threads are waiting for a response how can we distinguish for which threads request we have got the response for !!
Hope that question is clear. I just need some help regarding the design points and suspected problems that I may face.
I’m halfway through a network client of my own at the moment, so perhaps I can impart some advice and some resources to look at. There are many more experienced in this area, and hopefully they’ll chime in 🙂
Firstly, you’re about boost. Once you get used to how it all hangs together,
boost::asiois a great toolkit for writing network-code. Essentially, you create anio_serviceand callrunto execute until all work is complete, orrunOneto perform a single IO action. On their own, this isn’t that helpful. The power comes from when you either runrunOnein it’s own loop:, or run the
runfunction on one (or more) threads:However, it is worth noting that
runreturns as soon as there is no work to do (so you can say goodbye to that thread). As I found out here on Stackoverflow, the trick is to ensure it always has something to do. The solution is inboost::asio::io_service::work:The above line ensures your thread won’t stop when nothing is going on. I view is as a means to keep-alive 🙂
At some point, you’re going to want to create a socket and connect it somewhere. I created a generic Socket class (and derived a text-socket from that to create buffered input). I also wanted an event-based system that worked very much like C#. I’ve outlined this stuff for you, below:
First step, we need a generic way of passing arguments around, hence,
EventArgs:eventArgs.h
Now, we need an event class which people can subscribe/unsubscribe to:
event.h
Then, it was time to create a socket class. Below is the header:
socket.h
(Some notes:
ByteVectoristypedef std::vector<unsigned char>)And, now the implementation. You’ll notice it calls another class to perform DNS resolution. I will show that afterwards. Also there are some
EventArg-derivatives I have ommitted. They are simply passed as EventArg parameters when socket events occur.socket.cpp
As I said about DNS resolution, the line
Root::instance().resolveHost(_host, _port, anonResolve);calls this to perform asynchronous DNS:Finally, I need a text-based socket that raised an event every time I line was received (which is then processed). I’ll omit the header file this time and just show the implementation file. Needless to say it declares an
EventcalledonLinewhich it fires every time a line is received in it’s entirety:Some things to note about the class above… it uses
boost::asio::postto send lines. This allows it to all occur on the threads that ASIO manages in a thread-safe way, and allows us to queue up lines to be sent as and when. This makes it very scalable.I am sure there are many more questions and maybe my code isn’t helpful. I spent several days piecing it all together and making sense of it, and I doubt it’s actually any good. hopefully some better minds will glance over it and go “HOLY CRAP, THIS