Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Chemical_Sensor_DMA GPRS DPG_FINAL MBed_Adafruit-GPS-Library SDFileSystem Socket mbed
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 19:404594768414
- Child:
- 21:3d922bea5d77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.cpp Wed Oct 21 19:44:47 2015 +0000
@@ -0,0 +1,91 @@
+/*
+ TCPSocketConnection.cpp
+ 2014 Copyright (c) Seeed Technology Inc. All right reserved.
+
+ Author:lawliet zou(lawliet.zou@gmail.com)
+ 2014-2-24
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "TCPSocketConnection.h"
+
+TCPSocketConnection::TCPSocketConnection()
+{
+}
+
+int TCPSocketConnection::connect(const char* host, const int port)
+{
+ printf(">%s<",host);
+ if (_sock_fd < 0) {
+ _sock_fd = gprs->new_socket();
+ if (_sock_fd < 0) {
+ return -1;
+ }
+ }
+
+ if (!gprs->connect(_sock_fd, TCP, host, port)) {
+ return false;
+ }
+ return true;
+}
+
+bool TCPSocketConnection::is_connected(void)
+{
+ return gprs->is_connected(_sock_fd);
+}
+
+int TCPSocketConnection::send(char* data, int length)
+{
+ int size = gprs->wait_writeable(_sock_fd, length);
+ if (size < 0) {
+ return -1;
+ }
+ if (size > length) {
+ size = length;
+ }
+ return gprs->send(_sock_fd, data, size);
+}
+
+int TCPSocketConnection::send_all(char* data, int length)
+{
+ return send(data,length);
+}
+
+int TCPSocketConnection::receive(char* data, int length)
+{
+#if 0
+ if (size < 0) {
+ return -1;
+ }
+ if(size == 0) {
+ size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
+ }
+
+ if(size > length) {
+ size = size - length;
+ } else {
+ length = size;
+ size = -1;
+ }
+#endif
+ int size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
+ return gprs->recv(_sock_fd, data, size>length?length:size);
+}
+
+int TCPSocketConnection::receive_all(char* data, int length)
+{
+ return receive(data,length);
+}