ソースの整理中ですが、利用はできます。 大きなファイルはできないかもしれません。

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

Fork of giken9_HTMLServer_Sample by Yasushi TAUCHI

Committer:
yueee_yt
Date:
Wed Mar 12 06:30:30 2014 +0000
Revision:
2:6f25f8327180
Parent:
0:7766f6712673
debug comment

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 "Socket/Socket.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
yueee_yt 0:7766f6712673 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
yueee_yt 0:7766f6712673 24
yueee_yt 0:7766f6712673 25 }
yueee_yt 0:7766f6712673 26
yueee_yt 0:7766f6712673 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
yueee_yt 0:7766f6712673 28 _blocking = blocking;
yueee_yt 0:7766f6712673 29 _timeout = timeout;
yueee_yt 0:7766f6712673 30 }
yueee_yt 0:7766f6712673 31
yueee_yt 0:7766f6712673 32 int Socket::init_socket(int type) {
yueee_yt 0:7766f6712673 33 if (_sock_fd != -1)
yueee_yt 0:7766f6712673 34 return -1;
yueee_yt 0:7766f6712673 35
yueee_yt 0:7766f6712673 36 int fd = lwip_socket(AF_INET, type, 0);
yueee_yt 0:7766f6712673 37 if (fd < 0)
yueee_yt 0:7766f6712673 38 return -1;
yueee_yt 0:7766f6712673 39
yueee_yt 0:7766f6712673 40 _sock_fd = fd;
yueee_yt 0:7766f6712673 41 return 0;
yueee_yt 0:7766f6712673 42 }
yueee_yt 0:7766f6712673 43
yueee_yt 0:7766f6712673 44 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
yueee_yt 0:7766f6712673 45 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
yueee_yt 0:7766f6712673 46 }
yueee_yt 0:7766f6712673 47
yueee_yt 0:7766f6712673 48 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
yueee_yt 0:7766f6712673 49 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
yueee_yt 0:7766f6712673 50 }
yueee_yt 0:7766f6712673 51
yueee_yt 0:7766f6712673 52 int Socket::select(struct timeval *timeout, bool read, bool write) {
yueee_yt 0:7766f6712673 53 fd_set fdSet;
yueee_yt 0:7766f6712673 54 FD_ZERO(&fdSet);
yueee_yt 0:7766f6712673 55 FD_SET(_sock_fd, &fdSet);
yueee_yt 0:7766f6712673 56
yueee_yt 0:7766f6712673 57 fd_set* readset = (read ) ? (&fdSet) : (NULL);
yueee_yt 0:7766f6712673 58 fd_set* writeset = (write) ? (&fdSet) : (NULL);
yueee_yt 0:7766f6712673 59
yueee_yt 0:7766f6712673 60 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
yueee_yt 0:7766f6712673 61 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
yueee_yt 0:7766f6712673 62 }
yueee_yt 0:7766f6712673 63
yueee_yt 0:7766f6712673 64 int Socket::wait_readable(TimeInterval& timeout) {
yueee_yt 0:7766f6712673 65 return select(&timeout._time, true, false);
yueee_yt 0:7766f6712673 66 }
yueee_yt 0:7766f6712673 67
yueee_yt 0:7766f6712673 68 int Socket::wait_writable(TimeInterval& timeout) {
yueee_yt 0:7766f6712673 69 return select(&timeout._time, false, true);
yueee_yt 0:7766f6712673 70 }
yueee_yt 0:7766f6712673 71
yueee_yt 0:7766f6712673 72 int Socket::close(bool shutdown) {
yueee_yt 0:7766f6712673 73 if (_sock_fd < 0)
yueee_yt 0:7766f6712673 74 return -1;
yueee_yt 0:7766f6712673 75
yueee_yt 0:7766f6712673 76 if (shutdown)
yueee_yt 0:7766f6712673 77 lwip_shutdown(_sock_fd, SHUT_RDWR);
yueee_yt 0:7766f6712673 78 lwip_close(_sock_fd);
yueee_yt 0:7766f6712673 79 _sock_fd = -1;
yueee_yt 0:7766f6712673 80
yueee_yt 0:7766f6712673 81 return 0;
yueee_yt 0:7766f6712673 82 }
yueee_yt 0:7766f6712673 83
yueee_yt 0:7766f6712673 84 Socket::~Socket() {
yueee_yt 0:7766f6712673 85 close(); //Don't want to leak
yueee_yt 0:7766f6712673 86 }
yueee_yt 0:7766f6712673 87
yueee_yt 0:7766f6712673 88 TimeInterval::TimeInterval(unsigned int ms) {
yueee_yt 0:7766f6712673 89 _time.tv_sec = ms / 1000;
yueee_yt 0:7766f6712673 90 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
yueee_yt 0:7766f6712673 91 }