John Lowe
/
WebSockets2
Embedded WebSockets Experiment
Core/TCPConnection.h@0:6dee052a3fa4, 2011-07-26 (annotated)
- Committer:
- nandgate
- Date:
- Tue Jul 26 05:30:53 2011 +0000
- Revision:
- 0:6dee052a3fa4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nandgate | 0:6dee052a3fa4 | 1 | #ifndef TCPCONNECTION_H |
nandgate | 0:6dee052a3fa4 | 2 | #define TCPCONNECTION_H |
nandgate | 0:6dee052a3fa4 | 3 | |
nandgate | 0:6dee052a3fa4 | 4 | #include "arch/cc.h" |
nandgate | 0:6dee052a3fa4 | 5 | #include "lwip/err.h" |
nandgate | 0:6dee052a3fa4 | 6 | //#include "lwip/tcp.h" |
nandgate | 0:6dee052a3fa4 | 7 | |
nandgate | 0:6dee052a3fa4 | 8 | #include "TCPItem.h" |
nandgate | 0:6dee052a3fa4 | 9 | |
nandgate | 0:6dee052a3fa4 | 10 | namespace mbed { |
nandgate | 0:6dee052a3fa4 | 11 | |
nandgate | 0:6dee052a3fa4 | 12 | class NetServer; |
nandgate | 0:6dee052a3fa4 | 13 | class TCPListener; |
nandgate | 0:6dee052a3fa4 | 14 | |
nandgate | 0:6dee052a3fa4 | 15 | class TCPConnection : public TCPItem { |
nandgate | 0:6dee052a3fa4 | 16 | public: |
nandgate | 0:6dee052a3fa4 | 17 | TCPConnection(struct ip_addr, u16_t); |
nandgate | 0:6dee052a3fa4 | 18 | TCPConnection(TCPListener *, struct tcp_pcb *); |
nandgate | 0:6dee052a3fa4 | 19 | |
nandgate | 0:6dee052a3fa4 | 20 | virtual ~TCPConnection(); |
nandgate | 0:6dee052a3fa4 | 21 | |
nandgate | 0:6dee052a3fa4 | 22 | void connect(); |
nandgate | 0:6dee052a3fa4 | 23 | |
nandgate | 0:6dee052a3fa4 | 24 | err_t write(void *, u16_t len, u8_t flags = TCP_WRITE_FLAG_COPY) const; |
nandgate | 0:6dee052a3fa4 | 25 | |
nandgate | 0:6dee052a3fa4 | 26 | void recved(u32_t len) const; |
nandgate | 0:6dee052a3fa4 | 27 | u16_t sndbuf() const { return tcp_sndbuf(_pcb); } |
nandgate | 0:6dee052a3fa4 | 28 | |
nandgate | 0:6dee052a3fa4 | 29 | void set_poll_timer(const u32_t &time) { |
nandgate | 0:6dee052a3fa4 | 30 | if(_pcb) { |
nandgate | 0:6dee052a3fa4 | 31 | _pcb->polltmr = time; |
nandgate | 0:6dee052a3fa4 | 32 | } |
nandgate | 0:6dee052a3fa4 | 33 | } |
nandgate | 0:6dee052a3fa4 | 34 | |
nandgate | 0:6dee052a3fa4 | 35 | u32_t get_poll_interval() const { |
nandgate | 0:6dee052a3fa4 | 36 | return (_pcb)? _pcb->pollinterval: 0; |
nandgate | 0:6dee052a3fa4 | 37 | } |
nandgate | 0:6dee052a3fa4 | 38 | |
nandgate | 0:6dee052a3fa4 | 39 | void set_poll_interval(const u32_t &value) { |
nandgate | 0:6dee052a3fa4 | 40 | if(_pcb) { |
nandgate | 0:6dee052a3fa4 | 41 | _pcb->pollinterval = value; |
nandgate | 0:6dee052a3fa4 | 42 | } |
nandgate | 0:6dee052a3fa4 | 43 | } |
nandgate | 0:6dee052a3fa4 | 44 | |
nandgate | 0:6dee052a3fa4 | 45 | u32_t get_poll_timer() const { |
nandgate | 0:6dee052a3fa4 | 46 | return (_pcb)? _pcb->polltmr: 0; |
nandgate | 0:6dee052a3fa4 | 47 | } |
nandgate | 0:6dee052a3fa4 | 48 | |
nandgate | 0:6dee052a3fa4 | 49 | protected: |
nandgate | 0:6dee052a3fa4 | 50 | TCPConnection(); |
nandgate | 0:6dee052a3fa4 | 51 | |
nandgate | 0:6dee052a3fa4 | 52 | /** |
nandgate | 0:6dee052a3fa4 | 53 | * Function to be called when more send buffer space is available. |
nandgate | 0:6dee052a3fa4 | 54 | * @param space the amount of bytes available |
nandgate | 0:6dee052a3fa4 | 55 | * @return ERR_OK: try to send some data by calling tcp_output |
nandgate | 0:6dee052a3fa4 | 56 | */ |
nandgate | 0:6dee052a3fa4 | 57 | virtual err_t sent(u16_t space) = 0; |
nandgate | 0:6dee052a3fa4 | 58 | |
nandgate | 0:6dee052a3fa4 | 59 | /** |
nandgate | 0:6dee052a3fa4 | 60 | * Function to be called when (in-sequence) data has arrived. |
nandgate | 0:6dee052a3fa4 | 61 | * @param p the packet buffer which arrived |
nandgate | 0:6dee052a3fa4 | 62 | * @param err an error argument (TODO: that is current always ERR_OK?) |
nandgate | 0:6dee052a3fa4 | 63 | * @return ERR_OK: try to send some data by calling tcp_output |
nandgate | 0:6dee052a3fa4 | 64 | */ |
nandgate | 0:6dee052a3fa4 | 65 | virtual err_t recv(struct pbuf *p, err_t err) = 0; |
nandgate | 0:6dee052a3fa4 | 66 | |
nandgate | 0:6dee052a3fa4 | 67 | /** |
nandgate | 0:6dee052a3fa4 | 68 | * Function to be called when a connection has been set up. |
nandgate | 0:6dee052a3fa4 | 69 | * @param pcb the tcp_pcb that now is connected |
nandgate | 0:6dee052a3fa4 | 70 | * @param err an error argument (TODO: that is current always ERR_OK?) |
nandgate | 0:6dee052a3fa4 | 71 | * @return value is currently ignored |
nandgate | 0:6dee052a3fa4 | 72 | */ |
nandgate | 0:6dee052a3fa4 | 73 | virtual err_t connected(err_t err); |
nandgate | 0:6dee052a3fa4 | 74 | |
nandgate | 0:6dee052a3fa4 | 75 | /** |
nandgate | 0:6dee052a3fa4 | 76 | * Function which is called periodically. |
nandgate | 0:6dee052a3fa4 | 77 | * The period can be adjusted in multiples of the TCP slow timer interval |
nandgate | 0:6dee052a3fa4 | 78 | * by changing tcp_pcb.polltmr. |
nandgate | 0:6dee052a3fa4 | 79 | * @return ERR_OK: try to send some data by calling tcp_output |
nandgate | 0:6dee052a3fa4 | 80 | */ |
nandgate | 0:6dee052a3fa4 | 81 | virtual err_t poll() = 0; |
nandgate | 0:6dee052a3fa4 | 82 | |
nandgate | 0:6dee052a3fa4 | 83 | /** |
nandgate | 0:6dee052a3fa4 | 84 | * Function to be called whenever a fatal error occurs. |
nandgate | 0:6dee052a3fa4 | 85 | * There is no pcb parameter since most of the times, the pcb is |
nandgate | 0:6dee052a3fa4 | 86 | * already deallocated (or there is no pcb) when this function is called. |
nandgate | 0:6dee052a3fa4 | 87 | * @param err an indication why the error callback is called: |
nandgate | 0:6dee052a3fa4 | 88 | * ERR_ABRT: aborted through tcp_abort or by a TCP timer |
nandgate | 0:6dee052a3fa4 | 89 | * ERR_RST: the connection was reset by the remote host |
nandgate | 0:6dee052a3fa4 | 90 | */ |
nandgate | 0:6dee052a3fa4 | 91 | virtual void err(err_t err) = 0; |
nandgate | 0:6dee052a3fa4 | 92 | |
nandgate | 0:6dee052a3fa4 | 93 | virtual void dnsreply(const char *hostname, struct ip_addr *addr) = 0; |
nandgate | 0:6dee052a3fa4 | 94 | |
nandgate | 0:6dee052a3fa4 | 95 | err_t dnsrequest(const char *hostname, struct ip_addr *addr) const; |
nandgate | 0:6dee052a3fa4 | 96 | |
nandgate | 0:6dee052a3fa4 | 97 | private: |
nandgate | 0:6dee052a3fa4 | 98 | static void dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg); |
nandgate | 0:6dee052a3fa4 | 99 | static err_t connected_callback(void *arg, struct tcp_pcb *pcb, err_t err); |
nandgate | 0:6dee052a3fa4 | 100 | static err_t sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space); |
nandgate | 0:6dee052a3fa4 | 101 | static err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); |
nandgate | 0:6dee052a3fa4 | 102 | static err_t poll_callback(void *arg, struct tcp_pcb *pcb); |
nandgate | 0:6dee052a3fa4 | 103 | static void error_callback(void *arg, err_t erra); |
nandgate | 0:6dee052a3fa4 | 104 | |
nandgate | 0:6dee052a3fa4 | 105 | protected: |
nandgate | 0:6dee052a3fa4 | 106 | TCPListener *_parent; |
nandgate | 0:6dee052a3fa4 | 107 | struct ip_addr _ipaddr; |
nandgate | 0:6dee052a3fa4 | 108 | u16_t _port; |
nandgate | 0:6dee052a3fa4 | 109 | |
nandgate | 0:6dee052a3fa4 | 110 | friend class NetServer; |
nandgate | 0:6dee052a3fa4 | 111 | }; |
nandgate | 0:6dee052a3fa4 | 112 | |
nandgate | 0:6dee052a3fa4 | 113 | }; |
nandgate | 0:6dee052a3fa4 | 114 | |
nandgate | 0:6dee052a3fa4 | 115 | #endif /* TCPCONNECTION_H */ |