Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
TCPConnection.cpp
00001 #include "lwip/arch.h" 00002 00003 #include "dns.h" 00004 00005 #include "TCPConnection.h" 00006 #include "TCPListener.h" 00007 #include "NetServer.h" 00008 00009 using namespace std; 00010 using namespace mbed; 00011 00012 void TCPConnection::dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg) { 00013 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00014 if(connection) { 00015 (connection->dnsreply)(name, ipaddr); 00016 } 00017 } 00018 00019 err_t TCPConnection::connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) { 00020 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00021 LWIP_UNUSED_ARG(pcb); 00022 if(connection) { 00023 return (connection->connected)(err); 00024 } 00025 return ERR_OK; 00026 } 00027 00028 err_t TCPConnection::sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space) { 00029 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00030 LWIP_UNUSED_ARG(pcb); 00031 if(connection) { 00032 return (connection->sent)(space); 00033 } 00034 return ERR_OK; 00035 } 00036 00037 err_t TCPConnection::recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { 00038 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00039 LWIP_UNUSED_ARG(pcb); 00040 if(connection) { 00041 return (connection->recv)(p, err); 00042 } 00043 return ERR_OK; 00044 } 00045 00046 err_t TCPConnection::poll_callback(void *arg, struct tcp_pcb *pcb) { 00047 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00048 LWIP_UNUSED_ARG(pcb); 00049 if(connection) { 00050 return (connection->poll)(); 00051 } 00052 return ERR_OK; 00053 } 00054 00055 void TCPConnection::error_callback(void *arg, err_t erra) { 00056 TCPConnection *connection = static_cast<TCPConnection *>(arg); 00057 if(connection) { 00058 (connection->err)(erra); 00059 } 00060 } 00061 00062 TCPConnection::TCPConnection() : _parent(NULL), _port(0) { 00063 } 00064 00065 TCPConnection::TCPConnection(struct ip_addr ip, u16_t port) : _parent(NULL) { 00066 this->_ipaddr = ip; 00067 this->_port = port; 00068 } 00069 00070 TCPConnection::TCPConnection(TCPListener *parent, struct tcp_pcb *pcb) 00071 : TCPItem(pcb), _parent(parent), _ipaddr(pcb->remote_ip), _port(pcb->remote_port) { 00072 tcp_arg(this->_pcb, static_cast<void *>(this)); 00073 connected(ERR_OK); 00074 } 00075 00076 TCPConnection::~TCPConnection() { 00077 } 00078 00079 err_t TCPConnection::write(void *msg, u16_t len, u8_t flags) const { 00080 return tcp_write(this->_pcb, msg, len, flags); 00081 } 00082 00083 void TCPConnection::recved(u32_t len) const { 00084 tcp_recved(this->_pcb, len); 00085 } 00086 00087 err_t TCPConnection::connected(err_t err) { 00088 tcp_recv(this->_pcb, TCPConnection::recv_callback); 00089 tcp_sent(this->_pcb, TCPConnection::sent_callback); 00090 tcp_poll(this->_pcb, TCPConnection::poll_callback, 1); // addjust time (in twice a sec) 00091 tcp_err(this->_pcb, TCPConnection::error_callback); 00092 return ERR_OK; 00093 } 00094 00095 void TCPConnection::connect() { 00096 NetServer::ready(); 00097 open(); 00098 tcp_connect(this->_pcb, &this->_ipaddr, this->_port, TCPConnection::connected_callback); 00099 } 00100 00101 err_t TCPConnection::dnsrequest(const char *hostname, struct ip_addr *addr) const { 00102 return dns_gethostbyname(hostname, addr, TCPConnection::dnsreply_callback, (void *)this); 00103 }
Generated on Tue Jul 12 2022 19:17:24 by 1.7.2