Fix endianness of addr, minor fixes

This commit is contained in:
Stephen Seo 2019-03-04 19:05:07 +09:00
parent 9134079b8d
commit f8950fa08e
3 changed files with 12 additions and 10 deletions

View file

@ -37,7 +37,8 @@ static const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread";
#define UDPC_BAD_MODE_SEND_INTERVAL (1.0f/10.0f)
#define UDPC_TIMEOUT_SECONDS 10.0f
#define UDPC_HEARTBEAT_PKT_INTERVAL (15.0f/100.0f)
#define UDPC_INIT_PKT_INTERVAL 5.0f
#define UDPC_INIT_PKT_INTERVAL 5
#define UDPC_INIT_PKT_INTERVAL_F ((float)UDPC_INIT_PKT_INTERVAL)
#define UDPC_PKT_PROTOCOL_ID 1357924680
#define UDPC_ID_CONNECT 0x80000000

View file

@ -320,7 +320,8 @@ void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t
};
timespec_get(&cd.received, TIME_UTC);
// only set "received" to now, since "sent" will be set after sending packet
cd.sent = cd.received;
cd.sent.tv_sec -= UDPC_INIT_PKT_INTERVAL + 1;
UDPC_HashMap_insert(ctx->conMap, addr, &cd);
}
@ -778,7 +779,7 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
if((cd->flags & 0x8) != 0)
{
// initiate connection to server
if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL)
if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL_F)
{
return;
}
@ -1235,7 +1236,7 @@ char* UDPC_INTERNAL_atostr(UDPC_Context *ctx, uint32_t addr)
int index = 0;
for(int x = 0; x < 4; ++x)
{
unsigned char temp = (addr >> (24 - x * 8)) & 0xFF;
unsigned char temp = (addr >> (x * 8)) & 0xFF;
if(temp >= 100)
{
@ -1293,7 +1294,7 @@ uint32_t UDPC_strtoa(const char *addrStr)
}
else if(*addrStr == '.' && temp <= 0xFF && index < 3)
{
addr |= (temp << (24 - 8 * index++));
addr |= (temp << (8 * index++));
temp = 0;
}
else
@ -1305,7 +1306,7 @@ uint32_t UDPC_strtoa(const char *addrStr)
if(index == 3 && temp <= 0xFF)
{
addr |= temp;
addr |= temp << 24;
return addr;
}
else

View file

@ -246,7 +246,7 @@ void TEST_ATOSTR()
{
UDPC_Context ctx;
ASSERT_EQ_MEM(
UDPC_INTERNAL_atostr(&ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
UDPC_INTERNAL_atostr(&ctx, (0xFF << 24) | (0x1 << 16) | (0x1E << 8) | 0xAC),
"172.30.1.255",
13);
UNITTEST_REPORT(ATOSTR);
@ -387,9 +387,9 @@ void TEST_HASHMAP()
void TEST_STRTOA()
{
ASSERT_EQ(0x01020304, UDPC_strtoa("1.2.3.4"));
ASSERT_EQ(0x7F000001, UDPC_strtoa("127.0.0.1"));
ASSERT_EQ(0xC0A801FF, UDPC_strtoa("192.168.1.255"));
ASSERT_EQ(0x04030201, UDPC_strtoa("1.2.3.4"));
ASSERT_EQ(0x0100007F, UDPC_strtoa("127.0.0.1"));
ASSERT_EQ(0xFF01A8C0, UDPC_strtoa("192.168.1.255"));
ASSERT_EQ(0, UDPC_strtoa("1.2.3.4.5"));
ASSERT_EQ(0, UDPC_strtoa("100.20.30"));
ASSERT_EQ(0, UDPC_strtoa("200.400.30.50"));