FabGL
ESP32 Display Controller and Graphics Library
netutils.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3. Feel free to use FabGL in free software and hardware:
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 
38 #include "lwip/err.h"
39 #include "lwip/sockets.h"
40 #include "lwip/sys.h"
41 #include "lwip/netdb.h"
42 #include "lwip/dns.h"
43 
44 #include "fabglconf.h"
45 #include "fabutils.h"
46 
47 
48 
49 namespace fabgl {
50 
51 
52 // debug
53 void printIP(in_addr ip);
54 
55 
58 // hostToIP
59 //
60 // host can be a hostname or an IP
61 
62 in_addr hostToIP(char const * host);
63 
64 
65 
68 // Socket
69 // The lwip socket wrapper
70 
71 
72 class Socket {
73 
74 public:
75 
76  Socket()
77  : m_socket(-1), m_connected(false), m_remoteAddress(), m_littleEndian(false)
78  {}
79 
80  bool connect(in_addr host, uint16_t port);
81 
82  // ret -1 = error, ret 0 = disconnected
83  int read(void * buffer, size_t length);
84 
85  // ret -1 = error
86  int read(void * buffer, size_t maxLength, in_addr * sourceAddress, uint16_t * sourcePort);
87 
88  uint8_t readByte();
89  uint16_t readWord();
90  uint32_t readDWord();
91 
92  void writeByte(uint8_t value);
93  void writeWord(uint16_t value);
94  void writeDWord(uint32_t value);
95 
96  void readDiscard(size_t length);
97 
98  void writeFillBytes(size_t length, uint8_t value = 0);
99 
100  // ret -1 = error, ret 0 = disconnected
101  int write(void const * buffer, size_t length);
102 
103  // ret -1 = error, ret 0 = disconnected
104  int write(char const * str);
105 
106  // like printf
107  int writeFmt(char const * format, ...);
108 
109  void close();
110 
111  bool isConnected() { return m_connected; }
112 
113  void setNoDelay(bool value);
114 
115  int getSocket() { return m_socket; }
116 
117  // from now Socket will use "sendto" instead of "send"
118  void setRemoteAddress(in_addr remoteAddress, uint16_t remotePort);
119 
120  // timeOut in ms (0 = no timeout)
121  void setTimeOut(int timeOut);
122 
123  int getLastError();
124 
125  void setLittleEndian(bool value) { m_littleEndian = value; }
126 
127 
128 private:
129 
130  uint16_t checkEndiannessWord(uint16_t value);
131  uint32_t checkEndiannessDWord(uint32_t value);
132 
133  int m_socket;
134  bool m_connected;
135  sockaddr_in m_remoteAddress; // used by sendTo
136  bool m_littleEndian;
137 };
138 
139 
140 } // fabgl namespace
This file contains some utility classes and functions.
Definition: canvas.cpp:36
This file contains FabGL library configuration settings, like number of supported colors...