wei zou / GPRSInterface

Dependents:   Seeed_GPRS_Library_HelloWorld Seeed_GPRS_Xively_HelloWorld Seeed_ARCH_GPRS_V2_Xively_HelloWorld Seeed_ARCH_GPRS_V2_ThingSpeak_HelloWorld ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

TCPSocketConnection.cpp

00001 /*
00002   TCPSocketConnection.cpp
00003   2014 Copyright (c) Seeed Technology Inc.  All right reserved.
00004 
00005   Author:lawliet zou(lawliet.zou@gmail.com)
00006   2014-2-24
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 */
00022 
00023 #include "TCPSocketConnection.h"
00024 
00025 TCPSocketConnection::TCPSocketConnection()
00026 {
00027 }
00028 
00029 int TCPSocketConnection::connect(const char* host, const int port)
00030 {
00031     if (_sock_fd < 0) {
00032         _sock_fd = gprs->new_socket();
00033         if (_sock_fd < 0) {
00034             return -1;
00035         }
00036     }
00037 
00038     if (!gprs->connect(_sock_fd, TCP, host, port)) {
00039         return false;
00040     }
00041     return true;
00042 }
00043 
00044 bool TCPSocketConnection::is_connected(void)
00045 {
00046     return gprs->is_connected(_sock_fd);
00047 }
00048 
00049 int TCPSocketConnection::send(char* data, int length)
00050 {
00051     int size = gprs->wait_writeable(_sock_fd, length);
00052     if (size < 0) {
00053         return -1;
00054     }
00055     if (size > length) {
00056         size = length;
00057     }
00058     return gprs->send(_sock_fd, data, size);
00059 }
00060 
00061 int TCPSocketConnection::send_all(char* data, int length)
00062 {
00063     return send(data,length);
00064 }
00065 
00066 int TCPSocketConnection::receive(char* data, int length)
00067 {
00068 #if 0
00069     if (size < 0) {
00070         return -1;
00071     }
00072     if(size == 0) {
00073         size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
00074     }
00075 
00076     if(size > length) {
00077         size = size - length;
00078     } else {
00079         length = size;
00080         size = -1;
00081     }
00082 #endif
00083     int size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
00084     return gprs->recv(_sock_fd, data, size>length?length:size);
00085 }
00086 
00087 int TCPSocketConnection::receive_all(char* data, int length)
00088 {
00089     return receive(data,length);
00090 }