Skip to content

Commit

Permalink
Merge pull request #10 from spark/feature/ack-server-pings
Browse files Browse the repository at this point in the history
Ack server pings for faster closure of half-open sockets
  • Loading branch information
Zachary Crockett committed Mar 24, 2014
2 parents ab713dd + 64ae668 commit 71e2b5f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ CoAPCode::Enum CoAP::code(const unsigned char *message)
default: return CoAPCode::ERROR;
}
}

CoAPType::Enum CoAP::type(const unsigned char *message)
{
switch (message[0] & 0x30)
{
case 0x00: return CoAPType::CON;
case 0x10: return CoAPType::NON;
default:
case 0x20: return CoAPType::ACK;
case 0x30: return CoAPType::RESET;
}
}
13 changes: 12 additions & 1 deletion src/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace CoAPMessageType {
KEY_CHANGE,
SIGNAL_START,
SIGNAL_STOP,
EMPTY,
EMPTY_ACK,
PING,
ERROR
};
}
Expand All @@ -49,8 +50,18 @@ namespace CoAPCode {
};
}

namespace CoAPType {
enum Enum {
CON,
NON,
ACK,
RESET
};
}

class CoAP
{
public:
static CoAPCode::Enum code(const unsigned char *message);
static CoAPType::Enum type(const unsigned char *message);
};
28 changes: 23 additions & 5 deletions src/spark_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ CoAPMessageType::Enum
case 'u': return CoAPMessageType::UPDATE_BEGIN;
case 'c': return CoAPMessageType::CHUNK;
default: break;
} break;
} break;
case CoAPCode::PUT:
switch (path)
{
Expand All @@ -297,10 +297,13 @@ CoAPMessageType::Enum
if (buf[8]) return CoAPMessageType::SIGNAL_START;
else return CoAPMessageType::SIGNAL_STOP;
default: break;
} break;
} break;
case CoAPCode::EMPTY:
return CoAPMessageType::EMPTY;
break;
switch (CoAP::type(buf))
{
case CoAPType::CON: return CoAPMessageType::PING;
default: return CoAPMessageType::EMPTY_ACK;
} break;
default:
break;
}
Expand Down Expand Up @@ -449,7 +452,10 @@ int SparkProtocol::variable_value(unsigned char *buf,
bool SparkProtocol::send_event(const char *event_name, const char *data,
int ttl, EventType::Enum event_type)
{
static system_tick_t recent_event_ticks[5] = { -1000, -1000, -1000, -1000, -1000 };
static system_tick_t recent_event_ticks[5] = {
(system_tick_t) -1000, (system_tick_t) -1000,
(system_tick_t) -1000, (system_tick_t) -1000,
(system_tick_t) -1000 };
static int evt_tick_idx = 0;

system_tick_t now = recent_event_ticks[evt_tick_idx] = callback_millis();
Expand Down Expand Up @@ -953,6 +959,18 @@ bool SparkProtocol::handle_received_message(void)
descriptor.ota_upgrade_status_sent();
break;

case CoAPMessageType::PING:
*msg_to_send = 0;
*(msg_to_send + 1) = 16;
empty_ack(msg_to_send + 2, queue[2], queue[3]);
if (0 > blocking_send(msg_to_send, 18))
{
// error
return false;
}
break;

case CoAPMessageType::EMPTY_ACK:
case CoAPMessageType::ERROR:
default:
; // drop it on the floor
Expand Down

0 comments on commit 71e2b5f

Please sign in to comment.