NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.h@69:f034af6af872, 2016-03-31 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Mar 31 00:56:13 2016 -0500
- Revision:
- 69:f034af6af872
- Parent:
- 68:a52251517491
- Child:
- 70:aa343098aa61
Merged api changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
25:ed7b2a52e8ac | 1 | /* Socket |
Christopher Haster |
25:ed7b2a52e8ac | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
25:ed7b2a52e8ac | 3 | * |
Christopher Haster |
25:ed7b2a52e8ac | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
25:ed7b2a52e8ac | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
25:ed7b2a52e8ac | 6 | * You may obtain a copy of the License at |
Christopher Haster |
25:ed7b2a52e8ac | 7 | * |
Christopher Haster |
25:ed7b2a52e8ac | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
25:ed7b2a52e8ac | 9 | * |
Christopher Haster |
25:ed7b2a52e8ac | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
25:ed7b2a52e8ac | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
25:ed7b2a52e8ac | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
25:ed7b2a52e8ac | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
25:ed7b2a52e8ac | 14 | * limitations under the License. |
Christopher Haster |
25:ed7b2a52e8ac | 15 | */ |
Christopher Haster |
25:ed7b2a52e8ac | 16 | |
Christopher Haster |
25:ed7b2a52e8ac | 17 | #ifndef SOCKET_H |
Christopher Haster |
25:ed7b2a52e8ac | 18 | #define SOCKET_H |
Christopher Haster |
25:ed7b2a52e8ac | 19 | |
Christopher Haster |
25:ed7b2a52e8ac | 20 | #include "NetworkInterface.h" |
Christopher Haster |
25:ed7b2a52e8ac | 21 | |
Christopher Haster |
25:ed7b2a52e8ac | 22 | /** Abstract socket class |
Christopher Haster |
25:ed7b2a52e8ac | 23 | * API for handling general sockets. Supports IP address operations |
Christopher Haster |
25:ed7b2a52e8ac | 24 | * and sending/recieving data. |
Christopher Haster |
25:ed7b2a52e8ac | 25 | */ |
Christopher Haster |
25:ed7b2a52e8ac | 26 | class Socket |
Christopher Haster |
25:ed7b2a52e8ac | 27 | { |
Christopher Haster |
25:ed7b2a52e8ac | 28 | public: |
Christopher Haster |
53:26b5f1c69822 | 29 | ~Socket(); |
Christopher Haster |
53:26b5f1c69822 | 30 | |
Christopher Haster |
58:1caa187fa5af | 31 | /** Open a connection to the underlying address |
Christopher Haster |
58:1caa187fa5af | 32 | * @param address URL or IP address to connect to |
Christopher Haster |
58:1caa187fa5af | 33 | * @param port Port to connect to |
Christopher Haster |
27:d7ed39727306 | 34 | * @return 0 on success |
Christopher Haster |
25:ed7b2a52e8ac | 35 | */ |
Christopher Haster |
58:1caa187fa5af | 36 | int32_t open(const char *address, uint16_t port); |
Christopher Haster |
32:2c5fc105fc50 | 37 | |
Christopher Haster |
32:2c5fc105fc50 | 38 | /** Close an open connection |
Christopher Haster |
32:2c5fc105fc50 | 39 | * @return 0 on success |
Christopher Haster |
32:2c5fc105fc50 | 40 | */ |
Christopher Haster |
32:2c5fc105fc50 | 41 | int32_t close(); |
Christopher Haster |
32:2c5fc105fc50 | 42 | |
Christopher Haster |
25:ed7b2a52e8ac | 43 | /** Send data over the socket |
Christopher Haster |
25:ed7b2a52e8ac | 44 | * @param data Buffer of data to send |
Christopher Haster |
48:b3bbe28a7963 | 45 | * @param size Size of data to send |
geky | 68:a52251517491 | 46 | * @return 0 on success |
Christopher Haster |
25:ed7b2a52e8ac | 47 | */ |
Christopher Haster |
48:b3bbe28a7963 | 48 | int32_t send(const void *data, uint32_t size); |
Christopher Haster |
25:ed7b2a52e8ac | 49 | |
Christopher Haster |
25:ed7b2a52e8ac | 50 | /** Recieve data over the socket |
Christopher Haster |
25:ed7b2a52e8ac | 51 | * @param data Buffer to store recieved data |
Christopher Haster |
48:b3bbe28a7963 | 52 | * @param size Size of provided buffer |
geky | 68:a52251517491 | 53 | * @param blocking If true wait for data, otherwise return 0 if no data is available |
Christopher Haster |
48:b3bbe28a7963 | 54 | * @return Number of bytes recieved or a negative value on failure |
Christopher Haster |
25:ed7b2a52e8ac | 55 | */ |
Christopher Haster |
48:b3bbe28a7963 | 56 | int32_t recv(void *data, uint32_t size, bool blocking = true); |
Christopher Haster |
25:ed7b2a52e8ac | 57 | |
Christopher Haster |
58:1caa187fa5af | 58 | /** Gets the IP address |
Christopher Haster |
58:1caa187fa5af | 59 | * @return IP address to connect to |
Christopher Haster |
58:1caa187fa5af | 60 | */ |
Christopher Haster |
58:1caa187fa5af | 61 | const char *getIPAddress() const; |
Christopher Haster |
58:1caa187fa5af | 62 | |
Christopher Haster |
58:1caa187fa5af | 63 | /** Gets the port |
Christopher Haster |
58:1caa187fa5af | 64 | * @return Port to connect to |
Christopher Haster |
58:1caa187fa5af | 65 | */ |
Christopher Haster |
58:1caa187fa5af | 66 | uint16_t getPort() const; |
Christopher Haster |
58:1caa187fa5af | 67 | |
Christopher Haster |
58:1caa187fa5af | 68 | /** Returns status of socket |
Christopher Haster |
58:1caa187fa5af | 69 | * @return true if connected |
Christopher Haster |
58:1caa187fa5af | 70 | */ |
Christopher Haster |
58:1caa187fa5af | 71 | bool isConnected(); |
Christopher Haster |
58:1caa187fa5af | 72 | |
Christopher Haster |
25:ed7b2a52e8ac | 73 | protected: |
Christopher Haster |
57:3c873fab4207 | 74 | Socket(NetworkInterface *iface, ns_protocol_t proto); |
Christopher Haster |
25:ed7b2a52e8ac | 75 | |
Christopher Haster |
25:ed7b2a52e8ac | 76 | private: |
Christopher Haster |
25:ed7b2a52e8ac | 77 | NetworkInterface *_iface; |
Christopher Haster |
57:3c873fab4207 | 78 | ns_protocol_t _proto; |
Christopher Haster |
25:ed7b2a52e8ac | 79 | SocketInterface *_socket; |
Christopher Haster |
27:d7ed39727306 | 80 | |
Christopher Haster |
42:49893d13c432 | 81 | char _ip_address[NS_IP_SIZE]; |
Christopher Haster |
28:163fbe3263f4 | 82 | uint16_t _port; |
Christopher Haster |
25:ed7b2a52e8ac | 83 | }; |
Christopher Haster |
25:ed7b2a52e8ac | 84 | |
Christopher Haster |
25:ed7b2a52e8ac | 85 | #endif |