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
lwip/Core/TCPConnection.cpp@2:360fda42fefd, 2010-01-12 (annotated)
- Committer:
- iva2k
- Date:
- Tue Jan 12 07:41:55 2010 +0000
- Revision:
- 2:360fda42fefd
- Parent:
- 0:886e4b3119ad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iva2k | 0:886e4b3119ad | 1 | #include "lwip/arch.h" |
iva2k | 0:886e4b3119ad | 2 | |
iva2k | 0:886e4b3119ad | 3 | #include "dns.h" |
iva2k | 0:886e4b3119ad | 4 | |
iva2k | 0:886e4b3119ad | 5 | #include "TCPConnection.h" |
iva2k | 0:886e4b3119ad | 6 | #include "TCPListener.h" |
iva2k | 0:886e4b3119ad | 7 | #include "NetServer.h" |
iva2k | 0:886e4b3119ad | 8 | |
iva2k | 0:886e4b3119ad | 9 | using namespace std; |
iva2k | 0:886e4b3119ad | 10 | using namespace mbed; |
iva2k | 0:886e4b3119ad | 11 | |
iva2k | 0:886e4b3119ad | 12 | void TCPConnection::dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg) { |
iva2k | 0:886e4b3119ad | 13 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 14 | if(connection) { |
iva2k | 0:886e4b3119ad | 15 | (connection->dnsreply)(name, ipaddr); |
iva2k | 0:886e4b3119ad | 16 | } |
iva2k | 0:886e4b3119ad | 17 | } |
iva2k | 0:886e4b3119ad | 18 | |
iva2k | 0:886e4b3119ad | 19 | err_t TCPConnection::connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) { |
iva2k | 0:886e4b3119ad | 20 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 21 | LWIP_UNUSED_ARG(pcb); |
iva2k | 0:886e4b3119ad | 22 | if(connection) { |
iva2k | 0:886e4b3119ad | 23 | return (connection->connected)(err); |
iva2k | 0:886e4b3119ad | 24 | } |
iva2k | 0:886e4b3119ad | 25 | return ERR_OK; |
iva2k | 0:886e4b3119ad | 26 | } |
iva2k | 0:886e4b3119ad | 27 | |
iva2k | 0:886e4b3119ad | 28 | err_t TCPConnection::sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space) { |
iva2k | 0:886e4b3119ad | 29 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 30 | LWIP_UNUSED_ARG(pcb); |
iva2k | 0:886e4b3119ad | 31 | if(connection) { |
iva2k | 0:886e4b3119ad | 32 | return (connection->sent)(space); |
iva2k | 0:886e4b3119ad | 33 | } |
iva2k | 0:886e4b3119ad | 34 | return ERR_OK; |
iva2k | 0:886e4b3119ad | 35 | } |
iva2k | 0:886e4b3119ad | 36 | |
iva2k | 0:886e4b3119ad | 37 | err_t TCPConnection::recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
iva2k | 0:886e4b3119ad | 38 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 39 | LWIP_UNUSED_ARG(pcb); |
iva2k | 0:886e4b3119ad | 40 | if(connection) { |
iva2k | 0:886e4b3119ad | 41 | return (connection->recv)(p, err); |
iva2k | 0:886e4b3119ad | 42 | } |
iva2k | 0:886e4b3119ad | 43 | return ERR_OK; |
iva2k | 0:886e4b3119ad | 44 | } |
iva2k | 0:886e4b3119ad | 45 | |
iva2k | 0:886e4b3119ad | 46 | err_t TCPConnection::poll_callback(void *arg, struct tcp_pcb *pcb) { |
iva2k | 0:886e4b3119ad | 47 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 48 | LWIP_UNUSED_ARG(pcb); |
iva2k | 0:886e4b3119ad | 49 | if(connection) { |
iva2k | 0:886e4b3119ad | 50 | return (connection->poll)(); |
iva2k | 0:886e4b3119ad | 51 | } |
iva2k | 0:886e4b3119ad | 52 | return ERR_OK; |
iva2k | 0:886e4b3119ad | 53 | } |
iva2k | 0:886e4b3119ad | 54 | |
iva2k | 0:886e4b3119ad | 55 | void TCPConnection::error_callback(void *arg, err_t erra) { |
iva2k | 0:886e4b3119ad | 56 | TCPConnection *connection = static_cast<TCPConnection *>(arg); |
iva2k | 0:886e4b3119ad | 57 | if(connection) { |
iva2k | 0:886e4b3119ad | 58 | (connection->err)(erra); |
iva2k | 0:886e4b3119ad | 59 | } |
iva2k | 0:886e4b3119ad | 60 | } |
iva2k | 0:886e4b3119ad | 61 | |
iva2k | 0:886e4b3119ad | 62 | TCPConnection::TCPConnection() : _parent(NULL), _port(0) { |
iva2k | 0:886e4b3119ad | 63 | } |
iva2k | 0:886e4b3119ad | 64 | |
iva2k | 0:886e4b3119ad | 65 | TCPConnection::TCPConnection(struct ip_addr ip, u16_t port) : _parent(NULL) { |
iva2k | 0:886e4b3119ad | 66 | this->_ipaddr = ip; |
iva2k | 0:886e4b3119ad | 67 | this->_port = port; |
iva2k | 0:886e4b3119ad | 68 | } |
iva2k | 0:886e4b3119ad | 69 | |
iva2k | 0:886e4b3119ad | 70 | TCPConnection::TCPConnection(TCPListener *parent, struct tcp_pcb *pcb) |
iva2k | 0:886e4b3119ad | 71 | : TCPItem(pcb), _parent(parent), _ipaddr(pcb->remote_ip), _port(pcb->remote_port) { |
iva2k | 0:886e4b3119ad | 72 | tcp_arg(this->_pcb, static_cast<void *>(this)); |
iva2k | 0:886e4b3119ad | 73 | connected(ERR_OK); |
iva2k | 0:886e4b3119ad | 74 | } |
iva2k | 0:886e4b3119ad | 75 | |
iva2k | 0:886e4b3119ad | 76 | TCPConnection::~TCPConnection() { |
iva2k | 0:886e4b3119ad | 77 | } |
iva2k | 0:886e4b3119ad | 78 | |
iva2k | 0:886e4b3119ad | 79 | err_t TCPConnection::write(void *msg, u16_t len, u8_t flags) const { |
iva2k | 0:886e4b3119ad | 80 | return tcp_write(this->_pcb, msg, len, flags); |
iva2k | 0:886e4b3119ad | 81 | } |
iva2k | 0:886e4b3119ad | 82 | |
iva2k | 0:886e4b3119ad | 83 | void TCPConnection::recved(u32_t len) const { |
iva2k | 0:886e4b3119ad | 84 | tcp_recved(this->_pcb, len); |
iva2k | 0:886e4b3119ad | 85 | } |
iva2k | 0:886e4b3119ad | 86 | |
iva2k | 0:886e4b3119ad | 87 | err_t TCPConnection::connected(err_t err) { |
iva2k | 0:886e4b3119ad | 88 | tcp_recv(this->_pcb, TCPConnection::recv_callback); |
iva2k | 0:886e4b3119ad | 89 | tcp_sent(this->_pcb, TCPConnection::sent_callback); |
iva2k | 0:886e4b3119ad | 90 | tcp_poll(this->_pcb, TCPConnection::poll_callback, 1); // addjust time (in twice a sec) |
iva2k | 0:886e4b3119ad | 91 | tcp_err(this->_pcb, TCPConnection::error_callback); |
iva2k | 0:886e4b3119ad | 92 | return ERR_OK; |
iva2k | 0:886e4b3119ad | 93 | } |
iva2k | 0:886e4b3119ad | 94 | |
iva2k | 0:886e4b3119ad | 95 | void TCPConnection::connect() { |
iva2k | 0:886e4b3119ad | 96 | NetServer::ready(); |
iva2k | 0:886e4b3119ad | 97 | open(); |
iva2k | 0:886e4b3119ad | 98 | tcp_connect(this->_pcb, &this->_ipaddr, this->_port, TCPConnection::connected_callback); |
iva2k | 0:886e4b3119ad | 99 | } |
iva2k | 0:886e4b3119ad | 100 | |
iva2k | 0:886e4b3119ad | 101 | err_t TCPConnection::dnsrequest(const char *hostname, struct ip_addr *addr) const { |
iva2k | 0:886e4b3119ad | 102 | return dns_gethostbyname(hostname, addr, TCPConnection::dnsreply_callback, (void *)this); |
iva2k | 0:886e4b3119ad | 103 | } |