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