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