[zephyrchat-cvs] zchat/ZephyrLib ZephyrMgr.cpp,1.35,1.36 ZephyrPacket.cpp,1.14,1.15 ZephyrPacket.h,1
Status: Alpha
Brought to you by:
akosut
|
From: <ak...@us...> - 2003-04-05 18:04:32
|
Update of /cvsroot/zephyrchat/zchat/ZephyrLib
In directory sc8-pr-cvs1:/tmp/cvs-serv19807
Modified Files:
ZephyrMgr.cpp ZephyrPacket.cpp ZephyrPacket.h
Log Message:
Time a packet out if it has not been acked after 30 seconds.
Index: ZephyrMgr.cpp
===================================================================
RCS file: /cvsroot/zephyrchat/zchat/ZephyrLib/ZephyrMgr.cpp,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -d -r1.35 -r1.36
--- ZephyrMgr.cpp 28 Mar 2003 22:49:55 -0000 1.35
+++ ZephyrMgr.cpp 5 Apr 2003 18:04:28 -0000 1.36
@@ -29,6 +29,12 @@
*/
static const CFAbsoluteTime kZChatDistantFuture = CFAbsoluteTimeGetCurrent() + 31556925974.7;
+/* How often to resend a Zephyr packet */
+const CFTimeInterval kZChatPacketResendInterval = 1.0;
+
+/* How long to try to send a Zephyr packet before timing out */
+const CFTimeInterval kZChatPacketTimeout = 30.0;
+
namespace {
class ZephyrMgrFragmentCallback : public ZephyrSendCallback {
private:
@@ -189,7 +195,7 @@
CFRunLoopTimerContext context = { 0, this, NULL, NULL, NULL };
mResendTimer = CFRunLoopTimerCreate(kCFAllocatorDefault,
kZChatDistantFuture,
- 1.0, 0, 0, ResendCallback, &context);
+ kZChatPacketResendInterval, 0, 0, ResendCallback, &context);
CFRunLoopAddTimer(mRunLoop, mResendTimer, kCFRunLoopCommonModes);
// Send the host manager boot to the server.
@@ -346,15 +352,42 @@
* Resend any packets that have not yet been acked
*/
void zephyrlib::ZephyrMgr::ResendPackets() {
- hash_map<zpUID, ZephyrPacketStore>::iterator i;
+ fprintf(stderr, "ZephyrMgr::ResendPackets()\n");
+
+ hash_map<zpUID, ZephyrPacketStore>::iterator i;
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
/* Find packets that are at least a second old */
for (i = mSent.begin(); i != mSent.end(); ++i) {
ZephyrPacket &packet = i->second.packet;
- if (packet.mTimestamp + 1.0 < now) {
+ if (packet.mTimestamp + packet.mResendInterval < now) {
+ if (packet.mResendInterval > kZChatPacketTimeout) {
+ // Packet has been waiting too long: time it out
+ ZephyrCallback *callback = i->second.callback;
+ mSent.erase(i);
+
+ /* If this is the last packet in the sent list, don't schedule this timer again.
+ * Otherwise, come back immediately so we can finish looking at packets.
+ */
+ if (mSent.empty()) {
+ CFRunLoopTimerSetNextFireDate(mResendTimer, kZChatDistantFuture);
+ } else {
+ CFRunLoopTimerSetNextFireDate(mResendTimer, now);
+ }
+
+ if (callback != NULL) {
+ callback->Failure(ZephyrTimeoutError);
+ callback->Release();
+ }
+
+ // Failure() might have deleted us. So it's not safe to call anything. Besides,
+ // our iterator may be invalid, thanks to the empty() call. Return immediately and try
+ // again later.
+ return;
+ }
+
// Change the time to now and resend
- packet.mTimestamp = now;
+ packet.mResendInterval += kZChatPacketResendInterval;
DeliverPacket(packet, i->second.callback, false);
}
}
@@ -818,8 +851,9 @@
mSent[inPacket.GetUID()] = zps;
/* If no resend timer is scheduled, schedule one for a second from now */
- if (CFRunLoopTimerGetNextFireDate(mResendTimer) + 1.0 > CFAbsoluteTimeGetCurrent()) {
- CFRunLoopTimerSetNextFireDate(mResendTimer, CFAbsoluteTimeGetCurrent() + 1.0);
+ if (CFRunLoopTimerGetNextFireDate(mResendTimer) + kZChatPacketResendInterval
+ > CFAbsoluteTimeGetCurrent()) {
+ CFRunLoopTimerSetNextFireDate(mResendTimer, CFAbsoluteTimeGetCurrent() + kZChatPacketResendInterval);
}
}
}
Index: ZephyrPacket.cpp
===================================================================
RCS file: /cvsroot/zephyrchat/zchat/ZephyrLib/ZephyrPacket.cpp,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- ZephyrPacket.cpp 21 Mar 2003 20:37:24 -0000 1.14
+++ ZephyrPacket.cpp 5 Apr 2003 18:04:28 -0000 1.15
@@ -41,7 +41,8 @@
const string &inOpcode,
const string &inRecipient,
const vector<string> &inBody)
-throw (ZephyrStatus) : mBody(inBody), mBodyComplete(true), mTimestamp(CFAbsoluteTimeGetCurrent()) {
+throw (ZephyrStatus) : mBody(inBody), mBodyComplete(true), mTimestamp(CFAbsoluteTimeGetCurrent()),
+ mResendInterval(0.0) {
KClientKey sessionKey;
// GrP Add the realm to recipent and convert "*" to ""
@@ -198,7 +199,7 @@
* Construct a packet received from the server
*/
zephyrlib::ZephyrPacket::ZephyrPacket(const char *inPacket, string::size_type inLength) throw (ZephyrStatus)
- : mTimestamp(CFAbsoluteTimeGetCurrent()) {
+ : mTimestamp(CFAbsoluteTimeGetCurrent()), mResendInterval(0.0) {
/* First, split the packet up into a vector */
vector<string> fields = ZephyrUtils::FromZephyrPacket(inPacket, inLength, &mBodyComplete);
if (fields.size() <= zfNumfields) throw ZephyrUnexpectedPacketError;
@@ -220,7 +221,8 @@
*/
zephyrlib::ZephyrPacket::ZephyrPacket(const ZephyrPacket &inPacket)
: mHeaders(inPacket.mHeaders), mBody(inPacket.mBody),
- mBodyComplete(inPacket.mBodyComplete), mTimestamp(inPacket.mTimestamp) {}
+ mBodyComplete(inPacket.mBodyComplete), mTimestamp(inPacket.mTimestamp),
+ mResendInterval(inPacket.mResendInterval) {}
/* ZephyrPacket::ZephyrPacket
* --------------------------
@@ -228,7 +230,8 @@
* for acks.
*/
zephyrlib::ZephyrPacket::ZephyrPacket(const vector<string> &inHeaders, const vector<string> &inBody)
- : mHeaders(inHeaders), mBody(inBody), mBodyComplete(true), mTimestamp(CFAbsoluteTimeGetCurrent()) {}
+ : mHeaders(inHeaders), mBody(inBody), mBodyComplete(true), mTimestamp(CFAbsoluteTimeGetCurrent()),
+ mResendInterval(0.0) {}
/* ZephyrPacket::GetKind
* ---------------------
Index: ZephyrPacket.h
===================================================================
RCS file: /cvsroot/zephyrchat/zchat/ZephyrLib/ZephyrPacket.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- ZephyrPacket.h 21 Mar 2003 20:37:24 -0000 1.11
+++ ZephyrPacket.h 5 Apr 2003 18:04:28 -0000 1.12
@@ -57,6 +57,7 @@
bool mBodyComplete;
CFAbsoluteTime mTimestamp;
+ CFTimeInterval mResendInterval;
/* Header utilities */
void PushHeaderString(string inStr) { mHeaders.push_back(inStr); }
|