Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igorsk 0:b56b6a05cad4 1
igorsk 0:b56b6a05cad4 2 /*
igorsk 0:b56b6a05cad4 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
igorsk 0:b56b6a05cad4 4
igorsk 0:b56b6a05cad4 5 Permission is hereby granted, free of charge, to any person obtaining a copy
igorsk 0:b56b6a05cad4 6 of this software and associated documentation files (the "Software"), to deal
igorsk 0:b56b6a05cad4 7 in the Software without restriction, including without limitation the rights
igorsk 0:b56b6a05cad4 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
igorsk 0:b56b6a05cad4 9 copies of the Software, and to permit persons to whom the Software is
igorsk 0:b56b6a05cad4 10 furnished to do so, subject to the following conditions:
igorsk 0:b56b6a05cad4 11
igorsk 0:b56b6a05cad4 12 The above copyright notice and this permission notice shall be included in
igorsk 0:b56b6a05cad4 13 all copies or substantial portions of the Software.
igorsk 0:b56b6a05cad4 14
igorsk 0:b56b6a05cad4 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
igorsk 0:b56b6a05cad4 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
igorsk 0:b56b6a05cad4 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
igorsk 0:b56b6a05cad4 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
igorsk 0:b56b6a05cad4 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
igorsk 0:b56b6a05cad4 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
igorsk 0:b56b6a05cad4 21 THE SOFTWARE.
igorsk 0:b56b6a05cad4 22 */
igorsk 0:b56b6a05cad4 23
igorsk 0:b56b6a05cad4 24 #include "NTPClient.h"
igorsk 0:b56b6a05cad4 25
igorsk 0:b56b6a05cad4 26 #include <stdio.h>
igorsk 0:b56b6a05cad4 27
igorsk 0:b56b6a05cad4 28 //#define __DEBUG
igorsk 0:b56b6a05cad4 29 #include "dbg/dbg.h"
igorsk 0:b56b6a05cad4 30
igorsk 0:b56b6a05cad4 31 #define NTP_PORT 123
igorsk 0:b56b6a05cad4 32 #define NTP_CLIENT_PORT 0//50420 //Random port
igorsk 0:b56b6a05cad4 33 #define NTP_REQUEST_TIMEOUT 15000
igorsk 0:b56b6a05cad4 34 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
igorsk 0:b56b6a05cad4 35
igorsk 0:b56b6a05cad4 36 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
igorsk 0:b56b6a05cad4 37 #define ntohs( x ) (htons(x))
igorsk 0:b56b6a05cad4 38
igorsk 0:b56b6a05cad4 39 #define htonl( x ) ( (( x << 24 ) & 0xFF000000) \
igorsk 0:b56b6a05cad4 40 | (( x << 8 ) & 0x00FF0000) \
igorsk 0:b56b6a05cad4 41 | (( x >> 8 ) & 0x0000FF00) \
igorsk 0:b56b6a05cad4 42 | (( x >> 24 ) & 0x000000FF) )
igorsk 0:b56b6a05cad4 43 #define ntohl( x ) (htonl(x))
igorsk 0:b56b6a05cad4 44
igorsk 0:b56b6a05cad4 45 NTPClient::NTPClient() : NetService(false), m_state(NTP_PING), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
igorsk 0:b56b6a05cad4 46 m_watchdog(), m_timeout(0), m_closed(true), m_host(), m_pDnsReq(NULL), m_blockingResult(NTP_PROCESSING)
igorsk 0:b56b6a05cad4 47 {
igorsk 0:b56b6a05cad4 48 setTimeout(NTP_REQUEST_TIMEOUT);
igorsk 0:b56b6a05cad4 49 DBG("\r\nNew NTPClient %p\r\n",this);
igorsk 0:b56b6a05cad4 50 }
igorsk 0:b56b6a05cad4 51
igorsk 0:b56b6a05cad4 52 NTPClient::~NTPClient()
igorsk 0:b56b6a05cad4 53 {
igorsk 0:b56b6a05cad4 54 close();
igorsk 0:b56b6a05cad4 55 }
igorsk 0:b56b6a05cad4 56
igorsk 0:b56b6a05cad4 57 //High level setup functions
igorsk 0:b56b6a05cad4 58 NTPResult NTPClient::setTime(const Host& host) //Blocking
igorsk 0:b56b6a05cad4 59 {
igorsk 0:b56b6a05cad4 60 doSetTime(host);
igorsk 0:b56b6a05cad4 61 return blockingProcess();
igorsk 0:b56b6a05cad4 62 }
igorsk 0:b56b6a05cad4 63
igorsk 0:b56b6a05cad4 64 NTPResult NTPClient::setTime(const Host& host, void (*pMethod)(NTPResult)) //Non blocking
igorsk 0:b56b6a05cad4 65 {
igorsk 0:b56b6a05cad4 66 setOnResult(pMethod);
igorsk 0:b56b6a05cad4 67 doSetTime(host);
igorsk 0:b56b6a05cad4 68 return NTP_PROCESSING;
igorsk 0:b56b6a05cad4 69 }
igorsk 0:b56b6a05cad4 70
igorsk 0:b56b6a05cad4 71 #if 0 //For doc only
igorsk 0:b56b6a05cad4 72 template<class T>
igorsk 0:b56b6a05cad4 73 NTPResult NTPClient::setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
igorsk 0:b56b6a05cad4 74 {
igorsk 0:b56b6a05cad4 75 setOnResult(pItem, pMethod);
igorsk 0:b56b6a05cad4 76 doSetTime(host);
igorsk 0:b56b6a05cad4 77 return NTP_PROCESSING;
igorsk 0:b56b6a05cad4 78 }
igorsk 0:b56b6a05cad4 79 #endif
igorsk 0:b56b6a05cad4 80
igorsk 0:b56b6a05cad4 81 void NTPClient::doSetTime(const Host& host)
igorsk 0:b56b6a05cad4 82 {
igorsk 0:b56b6a05cad4 83 init();
igorsk 0:b56b6a05cad4 84 resetTimeout();
igorsk 0:b56b6a05cad4 85 m_host = host;
igorsk 0:b56b6a05cad4 86 if(!m_host.getPort())
igorsk 0:b56b6a05cad4 87 {
igorsk 0:b56b6a05cad4 88 m_host.setPort(NTP_PORT);
igorsk 0:b56b6a05cad4 89 }
igorsk 0:b56b6a05cad4 90 if(m_host.getIp().isNull())
igorsk 0:b56b6a05cad4 91 {
igorsk 0:b56b6a05cad4 92 //DNS query required
igorsk 0:b56b6a05cad4 93 m_pDnsReq = new DNSRequest();
igorsk 0:b56b6a05cad4 94 DBG("\r\nNTPClient : DNSRequest %p\r\n", m_pDnsReq);
igorsk 0:b56b6a05cad4 95 m_pDnsReq->setOnReply(this, &NTPClient::onDNSReply);
igorsk 0:b56b6a05cad4 96 m_pDnsReq->resolve(&m_host);
igorsk 0:b56b6a05cad4 97 return;
igorsk 0:b56b6a05cad4 98 }
igorsk 0:b56b6a05cad4 99 open();
igorsk 0:b56b6a05cad4 100 }
igorsk 0:b56b6a05cad4 101
igorsk 0:b56b6a05cad4 102 void NTPClient::setOnResult( void (*pMethod)(NTPResult) )
igorsk 0:b56b6a05cad4 103 {
igorsk 0:b56b6a05cad4 104 m_pCb = pMethod;
igorsk 0:b56b6a05cad4 105 }
igorsk 0:b56b6a05cad4 106
igorsk 0:b56b6a05cad4 107 void NTPClient::close()
igorsk 0:b56b6a05cad4 108 {
igorsk 0:b56b6a05cad4 109 if(m_closed)
igorsk 0:b56b6a05cad4 110 return;
igorsk 0:b56b6a05cad4 111 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
igorsk 0:b56b6a05cad4 112 m_watchdog.stop();
igorsk 0:b56b6a05cad4 113 m_pUDPSocket->resetOnEvent();
igorsk 0:b56b6a05cad4 114 m_pUDPSocket->close();
igorsk 0:b56b6a05cad4 115 delete m_pUDPSocket;
igorsk 0:b56b6a05cad4 116 if( m_pDnsReq )
igorsk 0:b56b6a05cad4 117 {
igorsk 0:b56b6a05cad4 118 m_pDnsReq->close();
igorsk 0:b56b6a05cad4 119 delete m_pDnsReq;
igorsk 0:b56b6a05cad4 120 m_pDnsReq = NULL;
igorsk 0:b56b6a05cad4 121 }
igorsk 0:b56b6a05cad4 122 }
igorsk 0:b56b6a05cad4 123
igorsk 0:b56b6a05cad4 124 void NTPClient::poll() //Called by NetServices
igorsk 0:b56b6a05cad4 125 {
igorsk 0:b56b6a05cad4 126 if( (!m_closed) && (m_watchdog.read_ms() >= m_timeout) )
igorsk 0:b56b6a05cad4 127 {
igorsk 0:b56b6a05cad4 128 onTimeout();
igorsk 0:b56b6a05cad4 129 }
igorsk 0:b56b6a05cad4 130 }
igorsk 0:b56b6a05cad4 131
igorsk 0:b56b6a05cad4 132 void NTPClient::init() //Create and setup socket if needed
igorsk 0:b56b6a05cad4 133 {
igorsk 0:b56b6a05cad4 134 if(!m_closed) //Already opened
igorsk 0:b56b6a05cad4 135 return;
igorsk 0:b56b6a05cad4 136 m_state = NTP_PING;
igorsk 0:b56b6a05cad4 137 m_pUDPSocket = new UDPSocket;
igorsk 0:b56b6a05cad4 138 m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent);
igorsk 0:b56b6a05cad4 139 m_closed = false;
igorsk 0:b56b6a05cad4 140 DBG("NTPClient: Init OK\n");
igorsk 0:b56b6a05cad4 141 }
igorsk 0:b56b6a05cad4 142
igorsk 0:b56b6a05cad4 143 void NTPClient::open()
igorsk 0:b56b6a05cad4 144 {
igorsk 0:b56b6a05cad4 145 resetTimeout();
igorsk 0:b56b6a05cad4 146 DBG("Opening connection\n");
igorsk 0:b56b6a05cad4 147 m_state = NTP_PING;
igorsk 0:b56b6a05cad4 148 Host localhost(IpAddr(), NTP_CLIENT_PORT, "localhost"); //Any local address
igorsk 0:b56b6a05cad4 149 m_pUDPSocket->bind(localhost);
igorsk 0:b56b6a05cad4 150 set_time( 1280000000 ); //End of July 2010... just there to limit offset range
igorsk 0:b56b6a05cad4 151 process();
igorsk 0:b56b6a05cad4 152 }
igorsk 0:b56b6a05cad4 153
igorsk 0:b56b6a05cad4 154 #define MIN(a,b) ((a)<(b))?(a):(b)
igorsk 0:b56b6a05cad4 155 void NTPClient::process() //Main state-machine
igorsk 0:b56b6a05cad4 156 {
igorsk 0:b56b6a05cad4 157 int len;
igorsk 0:b56b6a05cad4 158 Host host;
igorsk 0:b56b6a05cad4 159
igorsk 0:b56b6a05cad4 160 switch(m_state)
igorsk 0:b56b6a05cad4 161 {
igorsk 0:b56b6a05cad4 162 case NTP_PING:
igorsk 0:b56b6a05cad4 163 DBG("\r\nPing\r\n");
igorsk 0:b56b6a05cad4 164 //Prepare NTP Packet:
igorsk 0:b56b6a05cad4 165 m_pkt.li = 0; //Leap Indicator : No warning
igorsk 0:b56b6a05cad4 166 m_pkt.vn = 4; //Version Number : 4
igorsk 0:b56b6a05cad4 167 m_pkt.mode = 3; //Client mode
igorsk 0:b56b6a05cad4 168 m_pkt.stratum = 0; //Not relevant here
igorsk 0:b56b6a05cad4 169 m_pkt.poll = 0; //Not significant as well
igorsk 0:b56b6a05cad4 170 m_pkt.precision = 0; //Neither this one is
igorsk 0:b56b6a05cad4 171
igorsk 0:b56b6a05cad4 172 m_pkt.rootDelay = 0; //Or this one
igorsk 0:b56b6a05cad4 173 m_pkt.rootDispersion = 0; //Or that one
igorsk 0:b56b6a05cad4 174 m_pkt.refId = 0; //...
igorsk 0:b56b6a05cad4 175
igorsk 0:b56b6a05cad4 176 m_pkt.refTm_s = 0;
igorsk 0:b56b6a05cad4 177 m_pkt.origTm_s = 0;
igorsk 0:b56b6a05cad4 178 m_pkt.rxTm_s = 0;
igorsk 0:b56b6a05cad4 179 m_pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
igorsk 0:b56b6a05cad4 180
igorsk 0:b56b6a05cad4 181 m_pkt.refTm_f = m_pkt.origTm_f = m_pkt.rxTm_f = m_pkt.txTm_f = 0;
igorsk 0:b56b6a05cad4 182
igorsk 0:b56b6a05cad4 183 #ifdef __DEBUG
igorsk 0:b56b6a05cad4 184 //Hex Dump:
igorsk 0:b56b6a05cad4 185 DBG("\r\nDump Tx:\r\n");
igorsk 0:b56b6a05cad4 186 for(int i = 0; i< sizeof(NTPPacket); i++)
igorsk 0:b56b6a05cad4 187 {
igorsk 0:b56b6a05cad4 188 DBG("%02x ", *((char*)&m_pkt + i));
igorsk 0:b56b6a05cad4 189 }
igorsk 0:b56b6a05cad4 190 DBG("\r\n\r\n");
igorsk 0:b56b6a05cad4 191 #endif
igorsk 0:b56b6a05cad4 192
igorsk 0:b56b6a05cad4 193 len = m_pUDPSocket->sendto( (char*)&m_pkt, sizeof(NTPPacket), &m_host );
igorsk 0:b56b6a05cad4 194 if(len < sizeof(NTPPacket))
igorsk 0:b56b6a05cad4 195 { onResult(NTP_PRTCL); close(); return; }
igorsk 0:b56b6a05cad4 196
igorsk 0:b56b6a05cad4 197 m_state = NTP_PONG;
igorsk 0:b56b6a05cad4 198
igorsk 0:b56b6a05cad4 199 break;
igorsk 0:b56b6a05cad4 200
igorsk 0:b56b6a05cad4 201 case NTP_PONG:
igorsk 0:b56b6a05cad4 202 DBG("\r\nPong\r\n");
igorsk 0:b56b6a05cad4 203 while( len = m_pUDPSocket->recvfrom( (char*)&m_pkt, sizeof(NTPPacket), &host ) )
igorsk 0:b56b6a05cad4 204 {
igorsk 0:b56b6a05cad4 205 if( len <= 0 )
igorsk 0:b56b6a05cad4 206 break;
igorsk 0:b56b6a05cad4 207 if( !host.getIp().isEq(m_host.getIp()) )
igorsk 0:b56b6a05cad4 208 continue; //Not our packet
igorsk 0:b56b6a05cad4 209 if( len > 0 )
igorsk 0:b56b6a05cad4 210 break;
igorsk 0:b56b6a05cad4 211 }
igorsk 0:b56b6a05cad4 212
igorsk 0:b56b6a05cad4 213 if(len == 0)
igorsk 0:b56b6a05cad4 214 return; //Wait for the next packet
igorsk 0:b56b6a05cad4 215
igorsk 0:b56b6a05cad4 216 if(len < 0)
igorsk 0:b56b6a05cad4 217 { onResult(NTP_PRTCL); close(); return; }
igorsk 0:b56b6a05cad4 218
igorsk 0:b56b6a05cad4 219 if(len < sizeof(NTPPacket)) //TODO: Accept chunks
igorsk 0:b56b6a05cad4 220 { onResult(NTP_PRTCL); close(); return; }
igorsk 0:b56b6a05cad4 221
igorsk 0:b56b6a05cad4 222 #ifdef __DEBUG
igorsk 0:b56b6a05cad4 223 //Hex Dump:
igorsk 0:b56b6a05cad4 224 DBG("\r\nDump Rx:\r\n");
igorsk 0:b56b6a05cad4 225 for(int i = 0; i< sizeof(NTPPacket); i++)
igorsk 0:b56b6a05cad4 226 {
igorsk 0:b56b6a05cad4 227 DBG("%02x ", *((char*)&m_pkt + i));
igorsk 0:b56b6a05cad4 228 }
igorsk 0:b56b6a05cad4 229 DBG("\r\n\r\n");
igorsk 0:b56b6a05cad4 230 #endif
igorsk 0:b56b6a05cad4 231
igorsk 0:b56b6a05cad4 232 if( m_pkt.stratum == 0) //Kiss of death message : Not good !
igorsk 0:b56b6a05cad4 233 {
igorsk 0:b56b6a05cad4 234 onResult(NTP_PRTCL); close(); return;
igorsk 0:b56b6a05cad4 235 }
igorsk 0:b56b6a05cad4 236
igorsk 0:b56b6a05cad4 237 //Correct Endianness
igorsk 0:b56b6a05cad4 238 m_pkt.refTm_s = ntohl( m_pkt.refTm_s );
igorsk 0:b56b6a05cad4 239 m_pkt.refTm_f = ntohl( m_pkt.refTm_f );
igorsk 0:b56b6a05cad4 240 m_pkt.origTm_s = ntohl( m_pkt.origTm_s );
igorsk 0:b56b6a05cad4 241 m_pkt.origTm_f = ntohl( m_pkt.origTm_f );
igorsk 0:b56b6a05cad4 242 m_pkt.rxTm_s = ntohl( m_pkt.rxTm_s );
igorsk 0:b56b6a05cad4 243 m_pkt.rxTm_f = ntohl( m_pkt.rxTm_f );
igorsk 0:b56b6a05cad4 244 m_pkt.txTm_s = ntohl( m_pkt.txTm_s );
igorsk 0:b56b6a05cad4 245 m_pkt.txTm_f = ntohl( m_pkt.txTm_f );
igorsk 0:b56b6a05cad4 246
igorsk 0:b56b6a05cad4 247 //Compute offset, see RFC 4330 p.13
igorsk 0:b56b6a05cad4 248 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
igorsk 0:b56b6a05cad4 249 //int32_t origTm = (int32_t) ((uint64_t) m_pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
igorsk 0:b56b6a05cad4 250 //int32_t rxTm = (int32_t) ((uint64_t) m_pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
igorsk 0:b56b6a05cad4 251 //int32_t txTm = (int32_t) ((uint64_t) m_pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
igorsk 0:b56b6a05cad4 252 // int64_t offset = ( ( ( m_pkt.rxTm_s - m_pkt.origTm_s ) + ( m_pkt.txTm_s - destTm_s ) ) << 32 + ( ( m_pkt.rxTm_f - m_pkt.origTm_f ) + ( m_pkt.txTm_f - 0 ) ) ) / 2;
igorsk 0:b56b6a05cad4 253 int64_t offset = ( (int64_t)( m_pkt.rxTm_s - m_pkt.origTm_s ) + (int64_t) ( m_pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
igorsk 0:b56b6a05cad4 254 DBG("\r\nSent @%d\r\n", m_pkt.txTm_s);
igorsk 0:b56b6a05cad4 255 DBG("\r\nOffset: %d\r\n", offset);
igorsk 0:b56b6a05cad4 256 //Set time accordingly
igorsk 0:b56b6a05cad4 257 set_time( time(NULL) + (offset /*>> 32*/) );
igorsk 0:b56b6a05cad4 258
igorsk 0:b56b6a05cad4 259 onResult(NTP_OK);
igorsk 0:b56b6a05cad4 260 close();
igorsk 0:b56b6a05cad4 261
igorsk 0:b56b6a05cad4 262 break;
igorsk 0:b56b6a05cad4 263 }
igorsk 0:b56b6a05cad4 264 }
igorsk 0:b56b6a05cad4 265
igorsk 0:b56b6a05cad4 266 void NTPClient::setTimeout(int ms)
igorsk 0:b56b6a05cad4 267 {
igorsk 0:b56b6a05cad4 268 m_timeout = ms;
igorsk 0:b56b6a05cad4 269 }
igorsk 0:b56b6a05cad4 270
igorsk 0:b56b6a05cad4 271 void NTPClient::resetTimeout()
igorsk 0:b56b6a05cad4 272 {
igorsk 0:b56b6a05cad4 273 m_watchdog.reset();
igorsk 0:b56b6a05cad4 274 m_watchdog.start();
igorsk 0:b56b6a05cad4 275 }
igorsk 0:b56b6a05cad4 276
igorsk 0:b56b6a05cad4 277 void NTPClient::onTimeout() //Connection has timed out
igorsk 0:b56b6a05cad4 278 {
igorsk 0:b56b6a05cad4 279 close();
igorsk 0:b56b6a05cad4 280 onResult(NTP_TIMEOUT);
igorsk 0:b56b6a05cad4 281 }
igorsk 0:b56b6a05cad4 282
igorsk 0:b56b6a05cad4 283 void NTPClient::onDNSReply(DNSReply r)
igorsk 0:b56b6a05cad4 284 {
igorsk 0:b56b6a05cad4 285 if(m_closed)
igorsk 0:b56b6a05cad4 286 {
igorsk 0:b56b6a05cad4 287 DBG("\r\nWARN: Discarded\r\n");
igorsk 0:b56b6a05cad4 288 return;
igorsk 0:b56b6a05cad4 289 }
igorsk 0:b56b6a05cad4 290
igorsk 0:b56b6a05cad4 291 if( r != DNS_FOUND )
igorsk 0:b56b6a05cad4 292 {
igorsk 0:b56b6a05cad4 293 DBG("\r\nCould not resolve hostname.\r\n");
igorsk 0:b56b6a05cad4 294 onResult(NTP_DNS);
igorsk 0:b56b6a05cad4 295 close();
igorsk 0:b56b6a05cad4 296 return;
igorsk 0:b56b6a05cad4 297 }
igorsk 0:b56b6a05cad4 298 DBG("\r\nDNS resolved.\r\n");
igorsk 0:b56b6a05cad4 299 m_pDnsReq->close();
igorsk 0:b56b6a05cad4 300 delete m_pDnsReq;
igorsk 0:b56b6a05cad4 301 m_pDnsReq=NULL;
igorsk 0:b56b6a05cad4 302 open();
igorsk 0:b56b6a05cad4 303 }
igorsk 0:b56b6a05cad4 304
igorsk 0:b56b6a05cad4 305 void NTPClient::onUDPSocketEvent(UDPSocketEvent e)
igorsk 0:b56b6a05cad4 306 {
igorsk 0:b56b6a05cad4 307 resetTimeout();
igorsk 0:b56b6a05cad4 308 switch(e)
igorsk 0:b56b6a05cad4 309 {
igorsk 0:b56b6a05cad4 310 case UDPSOCKET_READABLE: //The only event for now
igorsk 0:b56b6a05cad4 311 resetTimeout();
igorsk 0:b56b6a05cad4 312 process();
igorsk 0:b56b6a05cad4 313 break;
igorsk 0:b56b6a05cad4 314 }
igorsk 0:b56b6a05cad4 315 }
igorsk 0:b56b6a05cad4 316
igorsk 0:b56b6a05cad4 317 void NTPClient::onResult(NTPResult r) //Must be called by impl when the request completes
igorsk 0:b56b6a05cad4 318 {
igorsk 0:b56b6a05cad4 319 if(m_pCbItem && m_pCbMeth)
igorsk 0:b56b6a05cad4 320 (m_pCbItem->*m_pCbMeth)(r);
igorsk 0:b56b6a05cad4 321 else if(m_pCb)
igorsk 0:b56b6a05cad4 322 m_pCb(r);
igorsk 0:b56b6a05cad4 323 m_blockingResult = r; //Blocking mode
igorsk 0:b56b6a05cad4 324 }
igorsk 0:b56b6a05cad4 325
igorsk 0:b56b6a05cad4 326 NTPResult NTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available
igorsk 0:b56b6a05cad4 327 {
igorsk 0:b56b6a05cad4 328 m_blockingResult = NTP_PROCESSING;
igorsk 0:b56b6a05cad4 329 do
igorsk 0:b56b6a05cad4 330 {
igorsk 0:b56b6a05cad4 331 Net::poll();
igorsk 0:b56b6a05cad4 332 } while(m_blockingResult == NTP_PROCESSING);
igorsk 0:b56b6a05cad4 333 Net::poll(); //Necessary for cleanup
igorsk 0:b56b6a05cad4 334 return m_blockingResult;
igorsk 0:b56b6a05cad4 335 }