Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1
Bethory 0:6ad07c9019fd 2 /** \addtogroup netsocket */
Bethory 0:6ad07c9019fd 3 /** @{*/
Bethory 0:6ad07c9019fd 4 /* TCPSocket
Bethory 0:6ad07c9019fd 5 * Copyright (c) 2015 ARM Limited
Bethory 0:6ad07c9019fd 6 *
Bethory 0:6ad07c9019fd 7 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 8 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 9 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 10 *
Bethory 0:6ad07c9019fd 11 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 12 *
Bethory 0:6ad07c9019fd 13 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 14 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 16 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 17 * limitations under the License.
Bethory 0:6ad07c9019fd 18 */
Bethory 0:6ad07c9019fd 19
Bethory 0:6ad07c9019fd 20 #ifndef TCPSOCKET_H
Bethory 0:6ad07c9019fd 21 #define TCPSOCKET_H
Bethory 0:6ad07c9019fd 22
Bethory 0:6ad07c9019fd 23 #include "netsocket/Socket.h"
Bethory 0:6ad07c9019fd 24 #include "netsocket/NetworkStack.h"
Bethory 0:6ad07c9019fd 25 #include "netsocket/NetworkInterface.h"
Bethory 0:6ad07c9019fd 26 #include "rtos/EventFlags.h"
Bethory 0:6ad07c9019fd 27
Bethory 0:6ad07c9019fd 28
Bethory 0:6ad07c9019fd 29 /** TCP socket connection
Bethory 0:6ad07c9019fd 30 */
Bethory 0:6ad07c9019fd 31 class TCPSocket : public Socket {
Bethory 0:6ad07c9019fd 32 public:
Bethory 0:6ad07c9019fd 33 /** Create an uninitialized socket
Bethory 0:6ad07c9019fd 34 *
Bethory 0:6ad07c9019fd 35 * Must call open to initialize the socket on a network stack.
Bethory 0:6ad07c9019fd 36 */
Bethory 0:6ad07c9019fd 37 TCPSocket();
Bethory 0:6ad07c9019fd 38
Bethory 0:6ad07c9019fd 39 /** Create a socket on a network interface
Bethory 0:6ad07c9019fd 40 *
Bethory 0:6ad07c9019fd 41 * Creates and opens a socket on the network stack of the given
Bethory 0:6ad07c9019fd 42 * network interface.
Bethory 0:6ad07c9019fd 43 *
Bethory 0:6ad07c9019fd 44 * @param stack Network stack as target for socket
Bethory 0:6ad07c9019fd 45 */
Bethory 0:6ad07c9019fd 46 template <typename S>
Bethory 0:6ad07c9019fd 47 TCPSocket(S *stack)
Bethory 0:6ad07c9019fd 48 : _pending(0), _event_flag(0),
Bethory 0:6ad07c9019fd 49 _read_in_progress(false), _write_in_progress(false)
Bethory 0:6ad07c9019fd 50 {
Bethory 0:6ad07c9019fd 51 open(stack);
Bethory 0:6ad07c9019fd 52 }
Bethory 0:6ad07c9019fd 53
Bethory 0:6ad07c9019fd 54 /** Destroy a socket
Bethory 0:6ad07c9019fd 55 *
Bethory 0:6ad07c9019fd 56 * Closes socket if the socket is still open
Bethory 0:6ad07c9019fd 57 */
Bethory 0:6ad07c9019fd 58 virtual ~TCPSocket();
Bethory 0:6ad07c9019fd 59
Bethory 0:6ad07c9019fd 60 /** Override multicast functions to return error for TCP
Bethory 0:6ad07c9019fd 61 *
Bethory 0:6ad07c9019fd 62 */
Bethory 0:6ad07c9019fd 63 int join_multicast_group(const SocketAddress &address) { return NSAPI_ERROR_UNSUPPORTED; }
Bethory 0:6ad07c9019fd 64
Bethory 0:6ad07c9019fd 65 /** Connects TCP socket to a remote host
Bethory 0:6ad07c9019fd 66 *
Bethory 0:6ad07c9019fd 67 * Initiates a connection to a remote server specified by either
Bethory 0:6ad07c9019fd 68 * a domain name or an IP address and a port.
Bethory 0:6ad07c9019fd 69 *
Bethory 0:6ad07c9019fd 70 * @param host Hostname of the remote host
Bethory 0:6ad07c9019fd 71 * @param port Port of the remote host
Bethory 0:6ad07c9019fd 72 * @return 0 on success, negative error code on failure
Bethory 0:6ad07c9019fd 73 */
Bethory 0:6ad07c9019fd 74 nsapi_error_t connect(const char *host, uint16_t port);
Bethory 0:6ad07c9019fd 75
Bethory 0:6ad07c9019fd 76 /** Connects TCP socket to a remote host
Bethory 0:6ad07c9019fd 77 *
Bethory 0:6ad07c9019fd 78 * Initiates a connection to a remote server specified by the
Bethory 0:6ad07c9019fd 79 * indicated address.
Bethory 0:6ad07c9019fd 80 *
Bethory 0:6ad07c9019fd 81 * @param address The SocketAddress of the remote host
Bethory 0:6ad07c9019fd 82 * @return 0 on success, negative error code on failure
Bethory 0:6ad07c9019fd 83 */
Bethory 0:6ad07c9019fd 84 nsapi_error_t connect(const SocketAddress &address);
Bethory 0:6ad07c9019fd 85
Bethory 0:6ad07c9019fd 86 /** Send data over a TCP socket
Bethory 0:6ad07c9019fd 87 *
Bethory 0:6ad07c9019fd 88 * The socket must be connected to a remote host. Returns the number of
Bethory 0:6ad07c9019fd 89 * bytes sent from the buffer.
Bethory 0:6ad07c9019fd 90 *
Bethory 0:6ad07c9019fd 91 * By default, send blocks until all data is sent. If socket is set to
Bethory 0:6ad07c9019fd 92 * non-blocking or times out, a partial amount can be written.
Bethory 0:6ad07c9019fd 93 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
Bethory 0:6ad07c9019fd 94 *
Bethory 0:6ad07c9019fd 95 * @param data Buffer of data to send to the host
Bethory 0:6ad07c9019fd 96 * @param size Size of the buffer in bytes
Bethory 0:6ad07c9019fd 97 * @return Number of sent bytes on success, negative error
Bethory 0:6ad07c9019fd 98 * code on failure
Bethory 0:6ad07c9019fd 99 */
Bethory 0:6ad07c9019fd 100 nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
Bethory 0:6ad07c9019fd 101
Bethory 0:6ad07c9019fd 102 /** Receive data over a TCP socket
Bethory 0:6ad07c9019fd 103 *
Bethory 0:6ad07c9019fd 104 * The socket must be connected to a remote host. Returns the number of
Bethory 0:6ad07c9019fd 105 * bytes received into the buffer.
Bethory 0:6ad07c9019fd 106 *
Bethory 0:6ad07c9019fd 107 * By default, recv blocks until some data is received. If socket is set to
Bethory 0:6ad07c9019fd 108 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
Bethory 0:6ad07c9019fd 109 * indicate no data.
Bethory 0:6ad07c9019fd 110 *
Bethory 0:6ad07c9019fd 111 * @param data Destination buffer for data received from the host
Bethory 0:6ad07c9019fd 112 * @param size Size of the buffer in bytes
Bethory 0:6ad07c9019fd 113 * @return Number of received bytes on success, negative error
Bethory 0:6ad07c9019fd 114 * code on failure. If no data is available to be received
Bethory 0:6ad07c9019fd 115 * and the peer has performed an orderly shutdown,
Bethory 0:6ad07c9019fd 116 * recv() returns 0.
Bethory 0:6ad07c9019fd 117 */
Bethory 0:6ad07c9019fd 118 nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
Bethory 0:6ad07c9019fd 119
Bethory 0:6ad07c9019fd 120 protected:
Bethory 0:6ad07c9019fd 121 friend class TCPServer;
Bethory 0:6ad07c9019fd 122
Bethory 0:6ad07c9019fd 123 virtual nsapi_protocol_t get_proto();
Bethory 0:6ad07c9019fd 124 virtual void event();
Bethory 0:6ad07c9019fd 125
Bethory 0:6ad07c9019fd 126 volatile unsigned _pending;
Bethory 0:6ad07c9019fd 127 rtos::EventFlags _event_flag;
Bethory 0:6ad07c9019fd 128 bool _read_in_progress;
Bethory 0:6ad07c9019fd 129 bool _write_in_progress;
Bethory 0:6ad07c9019fd 130 };
Bethory 0:6ad07c9019fd 131
Bethory 0:6ad07c9019fd 132
Bethory 0:6ad07c9019fd 133 #endif
Bethory 0:6ad07c9019fd 134
Bethory 0:6ad07c9019fd 135 /** @}*/