|
@@ -21,22 +21,46 @@
|
|
|
#define ArrayList_NAME Messages
|
|
|
#include "util/ArrayList.h"
|
|
|
|
|
|
+struct Bandwidth {
|
|
|
+ uint32_t lastMessageTime;
|
|
|
+ uint32_t bandwidth;
|
|
|
+};
|
|
|
+
|
|
|
struct PeerLink_pvt
|
|
|
{
|
|
|
struct PeerLink pub;
|
|
|
struct Allocator* alloc;
|
|
|
struct EventBase* base;
|
|
|
struct ArrayList_Messages* queue;
|
|
|
+ struct Bandwidth sendBw;
|
|
|
+ struct Bandwidth recvBw;
|
|
|
Identity
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ * Calculate the number of bytes per second which was sent, using only the size of
|
|
|
+ * the current message amortized over the timewindow since the previous message was sent.
|
|
|
+ */
|
|
|
+static int instantaniousBandwidth(int messageLength, int millisecondsSinceLast)
|
|
|
+{
|
|
|
+ if (millisecondsSinceLast >= 1024) { return messageLength; }
|
|
|
+ return ( messageLength * ((1024 - millisecondsSinceLast) << 4) ) >> 4;
|
|
|
+}
|
|
|
+
|
|
|
+static void calcNextBandwidth(struct Bandwidth* bw, int messageLength, struct PeerLink_pvt* pl)
|
|
|
+{
|
|
|
+ uint64_t now = Time_currentTimeMilliseconds(pl->base);
|
|
|
+ if (now <= bw->lastMessageTime) { now = bw->lastMessageTime; }
|
|
|
+ int ibw = instantaniousBandwidth(messageLength, now - bw->lastMessageTime);
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
struct Message* PeerLink_poll(struct PeerLink* peerLink)
|
|
|
{
|
|
|
struct PeerLink_pvt* pl = Identity_check((struct PeerLink_pvt*) peerLink);
|
|
|
- struct Message* out = ArrayList_Messages_get(pl->queue, 0); //ArrayList_Messages_shift(pl->queue);
|
|
|
+ struct Message* out = ArrayList_Messages_shift(pl->queue);
|
|
|
if (out) {
|
|
|
- //Allocator_disown(pl->alloc, out->alloc);
|
|
|
+ Allocator_disown(pl->alloc, out->alloc);
|
|
|
}
|
|
|
return out;
|
|
|
}
|
|
@@ -44,7 +68,7 @@ struct Message* PeerLink_poll(struct PeerLink* peerLink)
|
|
|
int PeerLink_send(struct Message* msg, struct PeerLink* peerLink)
|
|
|
{
|
|
|
struct PeerLink_pvt* pl = Identity_check((struct PeerLink_pvt*) peerLink);
|
|
|
- //Allocator_adopt(pl->alloc, msg->alloc);
|
|
|
+ Allocator_adopt(pl->alloc, msg->alloc);
|
|
|
ArrayList_Messages_add(pl->queue, msg);
|
|
|
return pl->queue->length;
|
|
|
}
|
|
@@ -57,12 +81,10 @@ void PeerLink_recv(struct Message* msg, struct PeerLink* pl)
|
|
|
|
|
|
struct PeerLink* PeerLink_new(struct EventBase* base, struct Allocator* alloc)
|
|
|
{
|
|
|
- struct PeerLink_pvt* pl = Allocator_calloc(alloc, sizeof(struct PeerLink_pvt*), 1);
|
|
|
+ struct PeerLink_pvt* pl = Allocator_calloc(alloc, sizeof(struct PeerLink_pvt), 1);
|
|
|
Identity_set(pl);
|
|
|
- //pl->base = base;
|
|
|
+ pl->base = base;
|
|
|
pl->alloc = alloc;
|
|
|
-Allocator_realloc(alloc, pl, 0);
|
|
|
- return NULL;
|
|
|
- //pl->queue = ArrayList_Messages_new(alloc);
|
|
|
+ pl->queue = ArrayList_Messages_new(alloc);
|
|
|
return &pl->pub;
|
|
|
}
|