I currently have this code:
PACKETS = {};
function AddPacket(data)
local id = data.ID;
PACKETS[id] = {
data.IP,
data.PORT,
data.PACKET,
data.SOURCEIP,
data.SOURCEPORT,
data.PPMS};
end
function RemovePacket(id)
PACKETS[id] = nil;
end
function LoopedThread()
for k,v in pairs(PACKETS) do
for i=1, v.PPMS do
SendPacket(v.IP, v.PORT, v.PACKET, v.SOURCEIP, v.SOURCEPORT);
end
end
Sleep(1);
end
This was done in lua as an example, however I’m needing this done in C++. I’m a little confused as I cant get it to work when trying to make it, if someone could help me out or even get me started, thanks.
I already have the sockets coded.
Are you at all familiar with the Standard Template Library in C++? It probably has a bunch of container types that might help you accomplish what you’re trying.
To me, it looks like what you’re doing would be accomplished by implementing a “Packet” struct or class that contained member variables “IP”, “PORT”, and so on.
Then if you wanted to store them and access them by their ID, you could create an stl::map that maps from the ID numbers to the Packets.
Or, as is common with packets, you might eventually want to store them in a first-in, first-out manner, which can be done with a stl::queue. With the queue they come out in the same order you put them in, so if you queue up packets 1, 2, and 3 and later go to access two of them, you will get packets 1 and 2 in that order, and 3 will still be left for later.
My C++ is pretty rusty, but I’m thinking of something like this:
You could do something similar with a map, but it would be slower and less efficient. Unless you really need to look up a particular packet based on its ID, I would stick with a queue, which can be staggeringly fast.