Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DnsQuery
Dependents: WizFi310_TCP_Echo_Server_Example
Fork of NetworkSocketAPI by
SocketInterface.h@26:9774a2edad71, 2016-02-18 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Feb 18 06:16:11 2016 -0600
- Branch:
- api-changes
- Revision:
- 26:9774a2edad71
- Parent:
- 24:a5e959bdd2dd
- Child:
- 27:d7ed39727306
Moved abstract constructors to protected regions
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sam_grove | 0:d35446f60233 | 1 | /* SocketInterface Base Class |
| sam_grove | 0:d35446f60233 | 2 | * Copyright (c) 2015 ARM Limited |
| sam_grove | 0:d35446f60233 | 3 | * |
| sam_grove | 0:d35446f60233 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| sam_grove | 0:d35446f60233 | 5 | * you may not use this file except in compliance with the License. |
| sam_grove | 0:d35446f60233 | 6 | * You may obtain a copy of the License at |
| sam_grove | 0:d35446f60233 | 7 | * |
| sam_grove | 0:d35446f60233 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| sam_grove | 0:d35446f60233 | 9 | * |
| sam_grove | 0:d35446f60233 | 10 | * Unless required by applicable law or agreed to in writing, software |
| sam_grove | 0:d35446f60233 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| sam_grove | 0:d35446f60233 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| sam_grove | 0:d35446f60233 | 13 | * See the License for the specific language governing permissions and |
| sam_grove | 0:d35446f60233 | 14 | * limitations under the License. |
| sam_grove | 0:d35446f60233 | 15 | */ |
| sam_grove | 8:4b7f97a5597b | 16 | |
| Christopher Haster |
21:35ed15069189 | 17 | #ifndef SOCKET_INTERFACE_H |
| Christopher Haster |
21:35ed15069189 | 18 | #define SOCKET_INTERFACE_H |
| bridadan | 2:ce08986b18b5 | 19 | |
| sam_grove | 8:4b7f97a5597b | 20 | #include "stdint.h" |
| sam_grove | 8:4b7f97a5597b | 21 | |
| sam_grove | 8:4b7f97a5597b | 22 | |
| Christopher Haster |
21:35ed15069189 | 23 | /** Enum of socket protocols |
| sam_grove | 8:4b7f97a5597b | 24 | */ |
| sam_grove | 8:4b7f97a5597b | 25 | typedef enum { |
| sam_grove | 8:4b7f97a5597b | 26 | SOCK_TCP, /*!< Socket connection over TCP */ |
| sam_grove | 8:4b7f97a5597b | 27 | SOCK_UDP, /*!< Socket connection over UDP */ |
| sam_grove | 8:4b7f97a5597b | 28 | } socket_protocol_t; |
| sam_grove | 8:4b7f97a5597b | 29 | |
| Christopher Haster |
21:35ed15069189 | 30 | |
| Christopher Haster |
21:35ed15069189 | 31 | /** SocketInterface class |
| Christopher Haster |
21:35ed15069189 | 32 | * Common interface for implementation specific sockets created through |
| Christopher Haster |
21:35ed15069189 | 33 | * network interfaces. This class is used internally by the |
| Christopher Haster |
21:35ed15069189 | 34 | * TCPSocket and UDPSocket classes |
| sam_grove | 8:4b7f97a5597b | 35 | */ |
| Christopher Haster |
21:35ed15069189 | 36 | class SocketInterface |
| sam_grove | 8:4b7f97a5597b | 37 | { |
| sam_grove | 8:4b7f97a5597b | 38 | public: |
| Christopher Haster |
24:a5e959bdd2dd | 39 | /** Set the URL of the socket |
| Christopher Haster |
24:a5e959bdd2dd | 40 | * Performs DNS lookup if necessary |
| Christopher Haster |
24:a5e959bdd2dd | 41 | * @param url URL to connect to |
| Christopher Haster |
24:a5e959bdd2dd | 42 | */ |
| Christopher Haster |
24:a5e959bdd2dd | 43 | virtual void setURL(const char *url) = 0; |
| Christopher Haster |
24:a5e959bdd2dd | 44 | |
| Christopher Haster |
21:35ed15069189 | 45 | /** Set the IP address of the socket |
| Christopher Haster |
21:35ed15069189 | 46 | * @param ip IP address to connect to, copied internally |
| sam_grove | 8:4b7f97a5597b | 47 | */ |
| Christopher Haster |
21:35ed15069189 | 48 | virtual void setIPAddress(const char *ip); |
| sam_grove | 16:658d4943c753 | 49 | |
| Christopher Haster |
21:35ed15069189 | 50 | /** Set the port of the socket |
| Christopher Haster |
21:35ed15069189 | 51 | * @param port Port to connect to |
| sam_grove | 8:4b7f97a5597b | 52 | */ |
| Christopher Haster |
21:35ed15069189 | 53 | virtual void setPort(uint16_t port); |
| sam_grove | 8:4b7f97a5597b | 54 | |
| Christopher Haster |
21:35ed15069189 | 55 | /** Get the IP address |
| Christopher Haster |
21:35ed15069189 | 56 | * @return IP address to connect to |
| sam_grove | 8:4b7f97a5597b | 57 | */ |
| Christopher Haster |
21:35ed15069189 | 58 | virtual const char *getIPAddress(); |
| Christopher Haster |
21:35ed15069189 | 59 | |
| Christopher Haster |
21:35ed15069189 | 60 | /** Get the port |
| Christopher Haster |
21:35ed15069189 | 61 | * @return Port to connect to |
| Christopher Haster |
21:35ed15069189 | 62 | */ |
| Christopher Haster |
21:35ed15069189 | 63 | virtual uint16_t getPort(); |
| Christopher Haster |
21:35ed15069189 | 64 | |
| sam_grove | 8:4b7f97a5597b | 65 | |
| Christopher Haster |
21:35ed15069189 | 66 | /** Open a connection to the underlying address |
| Christopher Haster |
21:35ed15069189 | 67 | * Only used for TCP sockets |
| Christopher Haster |
21:35ed15069189 | 68 | * @return 0 on success |
| sam_grove | 8:4b7f97a5597b | 69 | */ |
| Christopher Haster |
21:35ed15069189 | 70 | virtual int32_t open() = 0; |
| Christopher Haster |
21:35ed15069189 | 71 | |
| Christopher Haster |
21:35ed15069189 | 72 | /** Close an open connection |
| Christopher Haster |
21:35ed15069189 | 73 | * Only used for TCP sockets |
| Christopher Haster |
21:35ed15069189 | 74 | * @return 0 on success |
| Christopher Haster |
21:35ed15069189 | 75 | */ |
| Christopher Haster |
21:35ed15069189 | 76 | virtual int32_t close() = 0; |
| sam_grove | 8:4b7f97a5597b | 77 | |
| Christopher Haster |
21:35ed15069189 | 78 | /** Send data |
| Christopher Haster |
21:35ed15069189 | 79 | * @param data Buffer of data to send |
| Christopher Haster |
21:35ed15069189 | 80 | * @param len Size of data to send |
| Christopher Haster |
21:35ed15069189 | 81 | * @param timeout_ms Maximum amount of time to wait |
| Christopher Haster |
21:35ed15069189 | 82 | * @return 0 on success |
| sam_grove | 8:4b7f97a5597b | 83 | */ |
| Christopher Haster |
22:4fca633c0633 | 84 | virtual int32_t send(const void *data, uint32_t len, uint32_t timeout_ms) = 0; |
| sam_grove | 8:4b7f97a5597b | 85 | |
| Christopher Haster |
21:35ed15069189 | 86 | /** In client or server mode receive data |
| Christopher Haster |
21:35ed15069189 | 87 | * @param data a buffer to store the data in |
| Christopher Haster |
21:35ed15069189 | 88 | * @param amount The amount of data to receive |
| Christopher Haster |
21:35ed15069189 | 89 | * @param timeout_ms The longest time to wait for the data |
| Christopher Haster |
21:35ed15069189 | 90 | * @return Number of bytes sent or a negative value on failure |
| sam_grove | 8:4b7f97a5597b | 91 | */ |
| Christopher Haster |
22:4fca633c0633 | 92 | virtual int32_t recv(void *data, uint32_t len, uint32_t timeout_ms) = 0; |
| Christopher Haster |
21:35ed15069189 | 93 | |
| Christopher Haster |
26:9774a2edad71 | 94 | protected: |
| Christopher Haster |
26:9774a2edad71 | 95 | SocketInterface(); |
| Christopher Haster |
26:9774a2edad71 | 96 | virtual ~SocketInterface(); |
| Christopher Haster |
26:9774a2edad71 | 97 | |
| Christopher Haster |
21:35ed15069189 | 98 | private: |
| Christopher Haster |
21:35ed15069189 | 99 | char *_ip_address; |
| sarahmarshy | 13:f84e69b3fdd3 | 100 | uint16_t _port; |
| sam_grove | 8:4b7f97a5597b | 101 | }; |
| sam_grove | 8:4b7f97a5597b | 102 | |
| bridadan | 1:291a9d61e58a | 103 | #endif |
