Base class for IP Based Networking Libraries

Dependencies:   DnsQuery

Dependents:   TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocket.cpp Source File

TCPSocket.cpp

00001 /* Socket
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "TCPSocket.h"
00018 #include "Timer.h"
00019 
00020 TCPSocket::TCPSocket()
00021 {
00022 }
00023 
00024 TCPSocket::TCPSocket(NetworkStack *iface)
00025 {
00026     open(iface);
00027 }
00028 
00029 int TCPSocket::open(NetworkStack *iface)
00030 {
00031     return Socket::open(iface, NSAPI_TCP);
00032 }
00033 
00034 int TCPSocket::connect(const SocketAddress &addr)
00035 {
00036     if (!_socket) {
00037         return NSAPI_ERROR_NO_SOCKET;
00038     }
00039 
00040     return _iface->socket_connect(_socket, addr);
00041 }
00042 
00043 int TCPSocket::connect(const char *host, uint16_t port)
00044 {
00045     SocketAddress addr(_iface, host, port);
00046     if (!addr) {
00047         return NSAPI_ERROR_DNS_FAILURE;
00048     }
00049 
00050     return connect(addr);
00051 }
00052 
00053 int TCPSocket::send(const void *data, unsigned size)
00054 {
00055     mbed::Timer timer;
00056     timer.start();
00057     mbed::Timeout timeout;
00058     if (_timeout >= 0) {
00059         timeout.attach_us(&Socket::wakeup, _timeout * 1000);
00060     }
00061 
00062     while (true) {
00063         if (!_socket) {
00064             return NSAPI_ERROR_NO_SOCKET;
00065         }
00066 
00067         int sent = _iface->socket_send(_socket, data, size);
00068         if (sent != NSAPI_ERROR_WOULD_BLOCK
00069             || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
00070             return sent;
00071         }
00072 
00073         __WFI();
00074     }
00075 }
00076 
00077 int TCPSocket::recv(void *data, unsigned size)
00078 {
00079     mbed::Timer timer;
00080     timer.start();
00081     mbed::Timeout timeout;
00082     if (_timeout >= 0) {
00083         timeout.attach_us(&Socket::wakeup, _timeout * 1000);
00084     }
00085 
00086     while (true) {
00087         if (!_socket) {
00088             return NSAPI_ERROR_NO_SOCKET;
00089         }
00090     
00091         int recv = _iface->socket_recv(_socket, data, size);
00092         if (recv != NSAPI_ERROR_WOULD_BLOCK
00093             || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
00094             return recv;
00095         }
00096 
00097         __WFI();
00098     }
00099 }