ソースの整理中ですが、利用はできます。

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:7766f6712673 1 /* Copyright (C) 2012 mbed.org, MIT License
yueee_yt 0:7766f6712673 2 *
yueee_yt 0:7766f6712673 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
yueee_yt 0:7766f6712673 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
yueee_yt 0:7766f6712673 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
yueee_yt 0:7766f6712673 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:7766f6712673 7 * furnished to do so, subject to the following conditions:
yueee_yt 0:7766f6712673 8 *
yueee_yt 0:7766f6712673 9 * The above copyright notice and this permission notice shall be included in all copies or
yueee_yt 0:7766f6712673 10 * substantial portions of the Software.
yueee_yt 0:7766f6712673 11 *
yueee_yt 0:7766f6712673 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
yueee_yt 0:7766f6712673 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
yueee_yt 0:7766f6712673 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
yueee_yt 0:7766f6712673 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:7766f6712673 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
yueee_yt 0:7766f6712673 17 */
yueee_yt 0:7766f6712673 18 #include "TCPSocketConnection.h"
yueee_yt 0:7766f6712673 19 #include <cstring>
yueee_yt 0:7766f6712673 20
yueee_yt 0:7766f6712673 21 using std::memset;
yueee_yt 0:7766f6712673 22 using std::memcpy;
yueee_yt 0:7766f6712673 23
yueee_yt 0:7766f6712673 24 TCPSocketConnection::TCPSocketConnection() :
yueee_yt 0:7766f6712673 25 _is_connected(false) {
yueee_yt 0:7766f6712673 26 }
yueee_yt 0:7766f6712673 27
yueee_yt 0:7766f6712673 28 int TCPSocketConnection::connect(const char* host, const int port) {
yueee_yt 0:7766f6712673 29 if (init_socket(SOCK_STREAM) < 0)
yueee_yt 0:7766f6712673 30 return -1;
yueee_yt 0:7766f6712673 31
yueee_yt 0:7766f6712673 32 if (set_address(host, port) != 0)
yueee_yt 0:7766f6712673 33 return -1;
yueee_yt 0:7766f6712673 34
yueee_yt 0:7766f6712673 35 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
yueee_yt 0:7766f6712673 36 close();
yueee_yt 0:7766f6712673 37 return -1;
yueee_yt 0:7766f6712673 38 }
yueee_yt 0:7766f6712673 39 _is_connected = true;
yueee_yt 0:7766f6712673 40
yueee_yt 0:7766f6712673 41 return 0;
yueee_yt 0:7766f6712673 42 }
yueee_yt 0:7766f6712673 43
yueee_yt 0:7766f6712673 44 bool TCPSocketConnection::is_connected(void) {
yueee_yt 0:7766f6712673 45 return _is_connected;
yueee_yt 0:7766f6712673 46 }
yueee_yt 0:7766f6712673 47
yueee_yt 0:7766f6712673 48 int TCPSocketConnection::send(char* data, int length) {
yueee_yt 0:7766f6712673 49 if ((_sock_fd < 0) || !_is_connected)
yueee_yt 0:7766f6712673 50 return -1;
yueee_yt 0:7766f6712673 51
yueee_yt 0:7766f6712673 52 if (!_blocking) {
yueee_yt 0:7766f6712673 53 TimeInterval timeout(_timeout);
yueee_yt 0:7766f6712673 54 if (wait_writable(timeout) != 0)
yueee_yt 0:7766f6712673 55 return -1;
yueee_yt 0:7766f6712673 56 }
yueee_yt 0:7766f6712673 57
yueee_yt 0:7766f6712673 58 int n = lwip_send(_sock_fd, data, length, 0);
yueee_yt 0:7766f6712673 59 _is_connected = (n != 0);
yueee_yt 0:7766f6712673 60
yueee_yt 0:7766f6712673 61 return n;
yueee_yt 0:7766f6712673 62 }
yueee_yt 0:7766f6712673 63
yueee_yt 0:7766f6712673 64 // -1 if unsuccessful, else number of bytes written
yueee_yt 0:7766f6712673 65 int TCPSocketConnection::send_all(char* data, int length) {
yueee_yt 0:7766f6712673 66 if ((_sock_fd < 0) || !_is_connected)
yueee_yt 0:7766f6712673 67 return -1;
yueee_yt 0:7766f6712673 68
yueee_yt 0:7766f6712673 69 int writtenLen = 0;
yueee_yt 0:7766f6712673 70 TimeInterval timeout(_timeout);
yueee_yt 0:7766f6712673 71 while (writtenLen < length) {
yueee_yt 0:7766f6712673 72 if (!_blocking) {
yueee_yt 0:7766f6712673 73 // Wait for socket to be writeable
yueee_yt 0:7766f6712673 74 if (wait_writable(timeout) != 0)
yueee_yt 0:7766f6712673 75 return writtenLen;
yueee_yt 0:7766f6712673 76 }
yueee_yt 0:7766f6712673 77
yueee_yt 0:7766f6712673 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
yueee_yt 0:7766f6712673 79 if (ret > 0) {
yueee_yt 0:7766f6712673 80 writtenLen += ret;
yueee_yt 0:7766f6712673 81 continue;
yueee_yt 0:7766f6712673 82 } else if (ret == 0) {
yueee_yt 0:7766f6712673 83 _is_connected = false;
yueee_yt 0:7766f6712673 84 return writtenLen;
yueee_yt 0:7766f6712673 85 } else {
yueee_yt 0:7766f6712673 86 return -1; //Connnection error
yueee_yt 0:7766f6712673 87 }
yueee_yt 0:7766f6712673 88 }
yueee_yt 0:7766f6712673 89 return writtenLen;
yueee_yt 0:7766f6712673 90 }
yueee_yt 0:7766f6712673 91
yueee_yt 0:7766f6712673 92 int TCPSocketConnection::receive(char* data, int length) {
yueee_yt 0:7766f6712673 93 if ((_sock_fd < 0) || !_is_connected)
yueee_yt 0:7766f6712673 94 return -1;
yueee_yt 0:7766f6712673 95
yueee_yt 0:7766f6712673 96 if (!_blocking) {
yueee_yt 0:7766f6712673 97 TimeInterval timeout(_timeout);
yueee_yt 0:7766f6712673 98 if (wait_readable(timeout) != 0)
yueee_yt 0:7766f6712673 99 return -1;
yueee_yt 0:7766f6712673 100 }
yueee_yt 0:7766f6712673 101
yueee_yt 0:7766f6712673 102 int n = lwip_recv(_sock_fd, data, length, 0);
yueee_yt 0:7766f6712673 103 _is_connected = (n != 0);
yueee_yt 0:7766f6712673 104
yueee_yt 0:7766f6712673 105 return n;
yueee_yt 0:7766f6712673 106 }
yueee_yt 0:7766f6712673 107
yueee_yt 0:7766f6712673 108 // -1 if unsuccessful, else number of bytes received
yueee_yt 0:7766f6712673 109 int TCPSocketConnection::receive_all(char* data, int length) {
yueee_yt 0:7766f6712673 110 if ((_sock_fd < 0) || !_is_connected)
yueee_yt 0:7766f6712673 111 return -1;
yueee_yt 0:7766f6712673 112
yueee_yt 0:7766f6712673 113 int readLen = 0;
yueee_yt 0:7766f6712673 114 TimeInterval timeout(_timeout);
yueee_yt 0:7766f6712673 115 while (readLen < length) {
yueee_yt 0:7766f6712673 116 if (!_blocking) {
yueee_yt 0:7766f6712673 117 //Wait for socket to be readable
yueee_yt 0:7766f6712673 118 if (wait_readable(timeout) != 0)
yueee_yt 0:7766f6712673 119 return readLen;
yueee_yt 0:7766f6712673 120 }
yueee_yt 0:7766f6712673 121
yueee_yt 0:7766f6712673 122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
yueee_yt 0:7766f6712673 123 if (ret > 0) {
yueee_yt 0:7766f6712673 124 readLen += ret;
yueee_yt 0:7766f6712673 125 } else if (ret == 0) {
yueee_yt 0:7766f6712673 126 _is_connected = false;
yueee_yt 0:7766f6712673 127 return readLen;
yueee_yt 0:7766f6712673 128 } else {
yueee_yt 0:7766f6712673 129 return -1; //Connnection error
yueee_yt 0:7766f6712673 130 }
yueee_yt 0:7766f6712673 131 }
yueee_yt 0:7766f6712673 132 return readLen;
yueee_yt 0:7766f6712673 133 }