Alexey Antanenko / WIZnet_Library

Dependents:   mywebserver

Fork of WIZnet_Library by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

TCPSocketConnection.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "TCPSocketConnection.h"
00020 
00021 TCPSocketConnection::TCPSocketConnection()
00022 {
00023 }
00024 
00025 int TCPSocketConnection::connect(const char* host, const int port)
00026 {  
00027     if (_sock_fd < 0) {
00028         _sock_fd = eth->new_socket();
00029         if (_sock_fd < 0) {
00030             return -1;
00031         }
00032     }
00033     if (set_address(host, port) != 0) {
00034         return -1;
00035     }    
00036     if (!eth->connect(_sock_fd, get_address(), port)) {
00037         return -1;
00038     }
00039     return 0;
00040 }
00041 
00042 bool TCPSocketConnection::is_connected(void)
00043 {
00044     return eth->is_connected(_sock_fd);
00045 }
00046 
00047 
00048 bool TCPSocketConnection::is_fin_received(void)
00049 {
00050     return eth->is_fin_received(_sock_fd);
00051 }
00052 
00053 int TCPSocketConnection::send(char* data, int length)
00054 {
00055     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
00056     if (size < 0) {
00057         return -1;
00058     }
00059     if (size > length) {
00060         size = length;
00061     }
00062     return eth->send(_sock_fd, data, size);
00063 }
00064 
00065 // -1 if unsuccessful, else number of bytes written
00066 int TCPSocketConnection::send_all(char* data, int length)
00067 {
00068     int writtenLen = 0;
00069     while (writtenLen < length) {
00070         int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
00071         if (size < 0) {
00072             return -1;
00073         }
00074         if (size > (length-writtenLen)) {
00075             size = (length-writtenLen);
00076         }
00077         int ret = eth->send(_sock_fd, data + writtenLen, size);
00078         if (ret < 0) {
00079             return -1;
00080         }
00081         writtenLen += ret;
00082     }
00083     return writtenLen;
00084 }
00085 
00086 // -1 if unsuccessful, else number of bytes received
00087 int TCPSocketConnection::receive(char* data, int length)
00088 {
00089     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
00090     if (size < 0) {
00091         return -1;
00092     }
00093     if (size > length) {
00094         size = length;
00095     }
00096     return eth->recv(_sock_fd, data, size);
00097 }
00098 
00099 // -1 if unsuccessful, else number of bytes received
00100 int TCPSocketConnection::receive_all(char* data, int length)
00101 {
00102     int readLen = 0;
00103     while (readLen < length) {
00104         int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
00105         if (size <= 0) {
00106             break;
00107         }
00108         if (size > (length - readLen)) {
00109             size = length - readLen;
00110         }
00111         int ret = eth->recv(_sock_fd, data + readLen, size);
00112         if (ret < 0) {
00113             return -1;
00114         }
00115         readLen += ret;
00116     }
00117     return readLen;
00118 }
00119