Jump to content
  • Advertisement
Sign in to follow this  
roby65

[C++] Bandwidth calculating?

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i need to send files over the internet using udp (like p2p), and i need to know bandwidth, or at least the number of bytes sent... My idea is: int idx=0; while(idx!=NUM_PARTS) { send(part[idx]); idx++; } but at a certain point packets will be dropped....and i want to know how many bytes are sent....is there any way?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by roby65
but at a certain point packets will be dropped....and i want to know how many bytes are sent....is there any way?


You could have the receiving side tell you how much data it receives per unit of time.

Share this post


Link to post
Share on other sites
- For each packet you send, put it in queue, up to a maximum of N packets
- Whenever peer reports successful reception of a packet (via ack, for example), remove that packet from queue

This is convenient ack management scheme. At any given time, there can be at most N packets on the wire. This approach also allows you to handle resends, since you always know which packets are outstanding.


If you need to limit the bandwidth, have a counter of bytes sent (quota). Increment it by the size of packet each time you send something over the wire. Every T ms, decrement the counter. If packet you wish to send would exceed some quota, wait until quota drops.

Something like:

enum { MTU = 1400 };
std::map<int, PacketPtr> outstanding;
int counter;
int quota;
char * content;
int content_len;
int offset;

...
bool send()
// did we send entire content
if (offset >= content_len && outstanding.empty()) return true;

// outstanding queue is full
if (outstanding.size() > N) return false;

// did we exceed the bandwidth quota
if (MTU > quota) return false;

int to_send = offset + MTU;
if (to_send > content_len) to_send = to_send - content_len;

PacketPtr p = new Packet(counter, &content[offset], to_send);
outstanding.insert(std::make_pair(counter, p));
counter++;
offset += packet.size();
quota -= packet.size();

socket.send(p);
return false;
}



On receive, and on update, you update the quota appropriately, check if some packets need to be resent, remove acked packets from the queue.

Quota is somewhat trivial to represent as bytes per T, where T is the resolution of your timer. If it's 3 kb/sec, and you update every 50ms, you just increase quota by 150 on each tick.

How exactly you check for time depends on the model you use, blocking, non-blocking, async, number of clients....

The choice of queue structure also depends. You could have priority queue, if you need to handle multiple types of traffic. Or some very clever hash table. But the above is just the general idea.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!