This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TcpSocket.cpp Source File

TcpSocket.cpp

00001 ////////////////////////////////////////////////////////////////////////////////
00002 // TcpSocket.h
00003 ////////////////////////////////////////////////////////////////////////////////
00004 
00005 #include "TcpSocket.h"
00006 #include "mbed.h"
00007 
00008 namespace MiMic
00009 {
00010     #define TIMEOUT_IN_MSEC (5*1000)
00011     
00012     TcpSocket::TcpSocket()
00013     {
00014         this->_inst=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_NORMAL);
00015         if(this->_inst==NULL){
00016             mbed_die();
00017         }
00018     }
00019     TcpSocket::~TcpSocket()
00020     {
00021         NyLPC_iTcpSocket_finalize(this->_inst);
00022     }
00023     bool TcpSocket::connect (const IpAddr& i_addr,unsigned short i_port)
00024     {
00025         return NyLPC_iTcpSocket_connect(this->_inst,&(i_addr.addr.v4),i_port,TIMEOUT_IN_MSEC)?true:false;
00026     }
00027     
00028     bool TcpSocket::send(const void* i_tx,unsigned short i_tx_size)
00029     {
00030         int l,t;
00031         l=i_tx_size;
00032         while(l>0){
00033             t=NyLPC_iTcpSocket_send(this->_inst,((const char*)i_tx)+(i_tx_size-l),l,TIMEOUT_IN_MSEC);
00034             if(t<0){
00035                 return false;
00036             }
00037             l-=t;
00038         }
00039         return true;
00040     }
00041     bool TcpSocket::canRecv()
00042     {
00043         const void* rx;
00044         return NyLPC_iTcpSocket_precv(this->_inst,&rx,0)>0;
00045     }
00046     int TcpSocket::precv(const void* &i_rx)
00047     {
00048         return NyLPC_iTcpSocket_precv(this->_inst,&i_rx,TIMEOUT_IN_MSEC);
00049     }
00050     int TcpSocket::precv(const char* &i_rx)
00051     {
00052         return NyLPC_iTcpSocket_precv(this->_inst,(const void**)&i_rx,TIMEOUT_IN_MSEC);
00053     }
00054     void TcpSocket::pseek(unsigned short i_rx_seek)
00055     {
00056         NyLPC_iTcpSocket_pseek(this->_inst,i_rx_seek);
00057     }
00058     void TcpSocket::close()
00059     {
00060         return NyLPC_iTcpSocket_close(this->_inst,TIMEOUT_IN_MSEC);
00061     }
00062 }