victor qiu / Mbed OS SimpleNTP

Fork of SimpleNTP by Akinori Hashimoto

Committer:
victorqiu2
Date:
Tue Aug 01 01:22:01 2017 +0000
Revision:
2:68ae27667d82
A

Who changed what in which revision?

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