Example of HTTPServer with additional features: * SNTPClient, DST rules * Link status indication * Local or SDCard-based WebServer * RPC-able class * Static and Dynamic HTML page

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPConnection.h Source File

TCPConnection.h

00001 #ifndef TCPCONNECTION_H
00002 #define TCPCONNECTION_H
00003 
00004 #include "arch/cc.h"
00005 #include "lwip/err.h"
00006 //#include "lwip/tcp.h"
00007 
00008 #include "TCPItem.h"
00009 
00010 namespace mbed {
00011 
00012 class NetServer;
00013 class TCPListener;
00014 
00015 class TCPConnection : public TCPItem {
00016   public:
00017     TCPConnection(struct ip_addr, u16_t);
00018     TCPConnection(TCPListener *, struct tcp_pcb *);
00019 
00020     virtual ~TCPConnection();
00021 
00022     void connect();
00023     
00024     err_t write(void *, u16_t len, u8_t flags = TCP_WRITE_FLAG_COPY) const;
00025 
00026     void recved(u32_t len) const;
00027     u16_t sndbuf() const { return tcp_sndbuf(_pcb); }
00028     
00029     void set_poll_timer(const u32_t &time) {
00030       if(_pcb) {
00031         _pcb->polltmr = time;
00032       }
00033     }
00034     
00035     u32_t get_poll_interval() const {
00036       return (_pcb)? _pcb->pollinterval: 0;
00037     }
00038     
00039     void set_poll_interval(const u32_t &value) {
00040       if(_pcb) {
00041         _pcb->pollinterval = value;
00042       }
00043     }
00044     
00045     u32_t get_poll_timer() const {
00046       return (_pcb)? _pcb->polltmr: 0;
00047     }
00048     
00049     // [iva2k] Expose remote IP
00050     struct ip_addr get_remote_ip() const {
00051       return _ipaddr;
00052     }
00053 
00054   protected:  
00055     TCPConnection();
00056 
00057     /**
00058      * Function to be called when more send buffer space is available.
00059      * @param space the amount of bytes available
00060      * @return ERR_OK: try to send some data by calling tcp_output
00061      */
00062     virtual err_t sent(u16_t space) = 0;
00063   
00064     /**
00065      * Function to be called when (in-sequence) data has arrived.
00066      * @param p the packet buffer which arrived
00067      * @param err an error argument (TODO: that is current always ERR_OK?)
00068      * @return ERR_OK: try to send some data by calling tcp_output
00069      */
00070     virtual err_t recv(struct pbuf *p, err_t err) = 0;
00071 
00072     /**
00073      * Function to be called when a connection has been set up.
00074      * @param pcb the tcp_pcb that now is connected
00075      * @param err an error argument (TODO: that is current always ERR_OK?)
00076      * @return value is currently ignored
00077      */
00078     virtual err_t connected(err_t err);
00079 
00080     /**
00081      * Function which is called periodically.
00082      * The period can be adjusted in multiples of the TCP slow timer interval
00083      * by changing tcp_pcb.polltmr.
00084      * @return ERR_OK: try to send some data by calling tcp_output
00085      */
00086     virtual err_t poll() = 0;
00087 
00088     /**
00089      * Function to be called whenever a fatal error occurs.
00090      * There is no pcb parameter since most of the times, the pcb is
00091      * already deallocated (or there is no pcb) when this function is called.
00092      * @param err an indication why the error callback is called:
00093      *            ERR_ABRT: aborted through tcp_abort or by a TCP timer
00094      *            ERR_RST: the connection was reset by the remote host
00095      */
00096     virtual void err(err_t err) = 0;
00097     
00098     virtual void dnsreply(const char *hostname, struct ip_addr *addr) = 0;
00099 
00100     err_t dnsrequest(const char *hostname, struct ip_addr *addr) const;
00101     
00102   private:
00103     static void dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg);
00104     static err_t connected_callback(void *arg, struct tcp_pcb *pcb, err_t err);
00105     static err_t sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space);
00106     static err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
00107     static err_t poll_callback(void *arg, struct tcp_pcb *pcb);
00108     static void error_callback(void *arg, err_t erra);
00109     
00110   protected:
00111     TCPListener *_parent;
00112     struct ip_addr _ipaddr;
00113     u16_t _port;
00114 
00115   friend class NetServer;
00116 };
00117 
00118 };
00119 
00120 #endif /* TCPCONNECTION_H */