Preliminary main mbed library for nexpaq development
features/net/network-socket/TCPSocket.h@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* TCPSocket |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2015 ARM Limited |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 0:6c56fb4bc5f0 | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 6 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 9 | * |
nexpaq | 0:6c56fb4bc5f0 | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 0:6c56fb4bc5f0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 14 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 15 | */ |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | #ifndef TCPSOCKET_H |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define TCPSOCKET_H |
nexpaq | 0:6c56fb4bc5f0 | 19 | |
nexpaq | 0:6c56fb4bc5f0 | 20 | #include "network-socket/Socket.h" |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include "network-socket/NetworkStack.h" |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include "network-socket/NetworkInterface.h" |
nexpaq | 0:6c56fb4bc5f0 | 23 | #include "rtos/Semaphore.h" |
nexpaq | 0:6c56fb4bc5f0 | 24 | |
nexpaq | 0:6c56fb4bc5f0 | 25 | |
nexpaq | 0:6c56fb4bc5f0 | 26 | /** TCP socket connection |
nexpaq | 0:6c56fb4bc5f0 | 27 | */ |
nexpaq | 0:6c56fb4bc5f0 | 28 | class TCPSocket : public Socket { |
nexpaq | 0:6c56fb4bc5f0 | 29 | public: |
nexpaq | 0:6c56fb4bc5f0 | 30 | /** Create an uninitialized socket |
nexpaq | 0:6c56fb4bc5f0 | 31 | * |
nexpaq | 0:6c56fb4bc5f0 | 32 | * Must call open to initialize the socket on a network stack. |
nexpaq | 0:6c56fb4bc5f0 | 33 | */ |
nexpaq | 0:6c56fb4bc5f0 | 34 | TCPSocket(); |
nexpaq | 0:6c56fb4bc5f0 | 35 | |
nexpaq | 0:6c56fb4bc5f0 | 36 | /** Create a socket on a network interface |
nexpaq | 0:6c56fb4bc5f0 | 37 | * |
nexpaq | 0:6c56fb4bc5f0 | 38 | * Creates and opens a socket on the network stack of the given |
nexpaq | 0:6c56fb4bc5f0 | 39 | * network interface. |
nexpaq | 0:6c56fb4bc5f0 | 40 | * |
nexpaq | 0:6c56fb4bc5f0 | 41 | * @param stack Network stack as target for socket |
nexpaq | 0:6c56fb4bc5f0 | 42 | */ |
nexpaq | 0:6c56fb4bc5f0 | 43 | template <typename S> |
nexpaq | 0:6c56fb4bc5f0 | 44 | TCPSocket(S *stack) |
nexpaq | 0:6c56fb4bc5f0 | 45 | : _pending(0), _read_sem(0), _write_sem(0), |
nexpaq | 0:6c56fb4bc5f0 | 46 | _read_in_progress(false), _write_in_progress(false) |
nexpaq | 0:6c56fb4bc5f0 | 47 | { |
nexpaq | 0:6c56fb4bc5f0 | 48 | open(stack); |
nexpaq | 0:6c56fb4bc5f0 | 49 | } |
nexpaq | 0:6c56fb4bc5f0 | 50 | |
nexpaq | 0:6c56fb4bc5f0 | 51 | /** Destroy a socket |
nexpaq | 0:6c56fb4bc5f0 | 52 | * |
nexpaq | 0:6c56fb4bc5f0 | 53 | * Closes socket if the socket is still open |
nexpaq | 0:6c56fb4bc5f0 | 54 | */ |
nexpaq | 0:6c56fb4bc5f0 | 55 | virtual ~TCPSocket(); |
nexpaq | 0:6c56fb4bc5f0 | 56 | |
nexpaq | 0:6c56fb4bc5f0 | 57 | /** Connects TCP socket to a remote host |
nexpaq | 0:6c56fb4bc5f0 | 58 | * |
nexpaq | 0:6c56fb4bc5f0 | 59 | * Initiates a connection to a remote server specified by either |
nexpaq | 0:6c56fb4bc5f0 | 60 | * a domain name or an IP address and a port. |
nexpaq | 0:6c56fb4bc5f0 | 61 | * |
nexpaq | 0:6c56fb4bc5f0 | 62 | * @param host Hostname of the remote host |
nexpaq | 0:6c56fb4bc5f0 | 63 | * @param port Port of the remote host |
nexpaq | 0:6c56fb4bc5f0 | 64 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 65 | */ |
nexpaq | 0:6c56fb4bc5f0 | 66 | int connect(const char *host, uint16_t port); |
nexpaq | 0:6c56fb4bc5f0 | 67 | |
nexpaq | 0:6c56fb4bc5f0 | 68 | /** Connects TCP socket to a remote host |
nexpaq | 0:6c56fb4bc5f0 | 69 | * |
nexpaq | 0:6c56fb4bc5f0 | 70 | * Initiates a connection to a remote server specified by the |
nexpaq | 0:6c56fb4bc5f0 | 71 | * indicated address. |
nexpaq | 0:6c56fb4bc5f0 | 72 | * |
nexpaq | 0:6c56fb4bc5f0 | 73 | * @param address The SocketAddress of the remote host |
nexpaq | 0:6c56fb4bc5f0 | 74 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 75 | */ |
nexpaq | 0:6c56fb4bc5f0 | 76 | int connect(const SocketAddress &address); |
nexpaq | 0:6c56fb4bc5f0 | 77 | |
nexpaq | 0:6c56fb4bc5f0 | 78 | /** Send data over a TCP socket |
nexpaq | 0:6c56fb4bc5f0 | 79 | * |
nexpaq | 0:6c56fb4bc5f0 | 80 | * The socket must be connected to a remote host. Returns the number of |
nexpaq | 0:6c56fb4bc5f0 | 81 | * bytes sent from the buffer. |
nexpaq | 0:6c56fb4bc5f0 | 82 | * |
nexpaq | 0:6c56fb4bc5f0 | 83 | * By default, send blocks until data is sent. If socket is set to |
nexpaq | 0:6c56fb4bc5f0 | 84 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
nexpaq | 0:6c56fb4bc5f0 | 85 | * immediately. |
nexpaq | 0:6c56fb4bc5f0 | 86 | * |
nexpaq | 0:6c56fb4bc5f0 | 87 | * @param data Buffer of data to send to the host |
nexpaq | 0:6c56fb4bc5f0 | 88 | * @param size Size of the buffer in bytes |
nexpaq | 0:6c56fb4bc5f0 | 89 | * @return Number of sent bytes on success, negative error |
nexpaq | 0:6c56fb4bc5f0 | 90 | * code on failure |
nexpaq | 0:6c56fb4bc5f0 | 91 | */ |
nexpaq | 0:6c56fb4bc5f0 | 92 | int send(const void *data, unsigned size); |
nexpaq | 0:6c56fb4bc5f0 | 93 | |
nexpaq | 0:6c56fb4bc5f0 | 94 | /** Receive data over a TCP socket |
nexpaq | 0:6c56fb4bc5f0 | 95 | * |
nexpaq | 0:6c56fb4bc5f0 | 96 | * The socket must be connected to a remote host. Returns the number of |
nexpaq | 0:6c56fb4bc5f0 | 97 | * bytes received into the buffer. |
nexpaq | 0:6c56fb4bc5f0 | 98 | * |
nexpaq | 0:6c56fb4bc5f0 | 99 | * By default, recv blocks until data is sent. If socket is set to |
nexpaq | 0:6c56fb4bc5f0 | 100 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
nexpaq | 0:6c56fb4bc5f0 | 101 | * immediately. |
nexpaq | 0:6c56fb4bc5f0 | 102 | * |
nexpaq | 0:6c56fb4bc5f0 | 103 | * @param data Destination buffer for data received from the host |
nexpaq | 0:6c56fb4bc5f0 | 104 | * @param size Size of the buffer in bytes |
nexpaq | 0:6c56fb4bc5f0 | 105 | * @return Number of received bytes on success, negative error |
nexpaq | 0:6c56fb4bc5f0 | 106 | * code on failure |
nexpaq | 0:6c56fb4bc5f0 | 107 | */ |
nexpaq | 0:6c56fb4bc5f0 | 108 | int recv(void *data, unsigned size); |
nexpaq | 0:6c56fb4bc5f0 | 109 | |
nexpaq | 0:6c56fb4bc5f0 | 110 | protected: |
nexpaq | 0:6c56fb4bc5f0 | 111 | friend class TCPServer; |
nexpaq | 0:6c56fb4bc5f0 | 112 | |
nexpaq | 0:6c56fb4bc5f0 | 113 | virtual nsapi_protocol_t get_proto(); |
nexpaq | 0:6c56fb4bc5f0 | 114 | virtual void event(); |
nexpaq | 0:6c56fb4bc5f0 | 115 | |
nexpaq | 0:6c56fb4bc5f0 | 116 | volatile unsigned _pending; |
nexpaq | 0:6c56fb4bc5f0 | 117 | rtos::Semaphore _read_sem; |
nexpaq | 0:6c56fb4bc5f0 | 118 | rtos::Semaphore _write_sem; |
nexpaq | 0:6c56fb4bc5f0 | 119 | bool _read_in_progress; |
nexpaq | 0:6c56fb4bc5f0 | 120 | bool _write_in_progress; |
nexpaq | 0:6c56fb4bc5f0 | 121 | }; |
nexpaq | 0:6c56fb4bc5f0 | 122 | |
nexpaq | 0:6c56fb4bc5f0 | 123 | |
nexpaq | 0:6c56fb4bc5f0 | 124 | #endif |