A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpconnection.cpp Source File

tcpconnection.cpp

00001 #include "lwip/arch.h"
00002 
00003 #include "tcpconnection.h"
00004 //#include "tcplistener.h"
00005 
00006 using namespace std;
00007 using namespace mbed;
00008 
00009 err_t tcp_connection_connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) {
00010   TCPConnection *connection = static_cast<TCPConnection *>(arg);
00011   LWIP_UNUSED_ARG(pcb);
00012   if(connection) {
00013     return (connection->connected)(err);
00014   }
00015   return ERR_OK;
00016 }
00017 
00018 err_t tcp_connection_sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space) {
00019   TCPConnection *connection = static_cast<TCPConnection *>(arg);
00020   LWIP_UNUSED_ARG(pcb);
00021   return (connection->sent)(space);
00022 }
00023 
00024 err_t tcp_connection_recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
00025   TCPConnection *connection = static_cast<TCPConnection *>(arg);
00026   LWIP_UNUSED_ARG(pcb);
00027   return (connection->recv)(p, err);
00028 }
00029 
00030 err_t tcp_connection_poll_callback(void *arg, struct tcp_pcb *pcb) {
00031   TCPConnection *connection = static_cast<TCPConnection *>(arg);
00032   LWIP_UNUSED_ARG(pcb);
00033   return (connection->poll)();
00034 }
00035 
00036 void tcp_connection_error_callback(void *arg, err_t err) {
00037   TCPConnection *connection = static_cast<TCPConnection *>(arg);
00038   (connection->err)(err);
00039 }
00040 
00041 TCPConnection::TCPConnection(struct ip_addr ip, u16_t port) :  _parent(NULL) {
00042   this->_ipaddr = ip;
00043   this->_port   = port;
00044   this->_pcb    = tcp_new();
00045   tcp_arg(this->_pcb, static_cast<void *>(this));
00046 }
00047 
00048 TCPConnection::TCPConnection(TCPListener *parent, struct tcp_pcb *pcb)
00049   : TCPItem(pcb), _parent(parent), _ipaddr(pcb->remote_ip), _port(pcb->remote_port) {
00050   tcp_arg(this->_pcb, static_cast<void *>(this));
00051   printf("TCPConnection\n");
00052   connected(ERR_OK);
00053 }
00054 
00055 TCPConnection::~TCPConnection() {
00056   tcp_arg(this->_pcb, NULL);
00057   tcp_sent(this->_pcb, NULL);
00058   tcp_recv(this->_pcb, NULL);
00059   tcp_poll(this->_pcb, NULL, 255);
00060   tcp_accept(this->_pcb, NULL);
00061   tcp_err(this->_pcb, NULL);
00062   tcp_close(this->_pcb);
00063 }
00064 
00065 err_t TCPConnection::write(void *msg, u32_t len) {
00066   return tcp_write(this->_pcb, msg, len, 1);
00067 }
00068     
00069 void TCPConnection::recved(u32_t len) {
00070   tcp_recved(this->_pcb, len);
00071 }
00072 
00073 err_t TCPConnection::connected(err_t err) {
00074   printf("Connected\n");
00075   tcp_recv(this->_pcb, tcp_connection_recv_callback);
00076   tcp_sent(this->_pcb, tcp_connection_sent_callback);
00077   tcp_poll(this->_pcb, tcp_connection_poll_callback, 100); // addjust time (in twice a sec)
00078   tcp_err(this->_pcb, tcp_connection_error_callback);
00079   return ERR_OK;                                                    
00080 }
00081 
00082 void TCPConnection::connect() {
00083   tcp_connect(this->_pcb, &this->_ipaddr, this->_port, tcp_connection_connected_callback); 
00084 }