Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit608c695

Browse files
committed
Added networking classes
1 parent0f84e93 commit608c695

File tree

5 files changed

+349
-0
lines changed

5 files changed

+349
-0
lines changed

‎api/Client.h‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Client.h - Base class that provides Client
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#pragma once
21+
22+
#include"Stream.h"
23+
#include"IPAddress.h"
24+
25+
classClient :publicStream {
26+
27+
public:
28+
virtualintconnect(IPAddress ip,uint16_t port) =0;
29+
virtualintconnect(constchar *host,uint16_t port) =0;
30+
virtualsize_twrite(uint8_t) =0;
31+
virtualsize_twrite(constuint8_t *buf,size_t size) =0;
32+
virtualintavailable() = 0;
33+
virtualintread() = 0;
34+
virtualintread(uint8_t *buf,size_t size) = 0;
35+
virtualintpeek() = 0;
36+
virtualvoidflush() = 0;
37+
virtualvoidstop() = 0;
38+
virtualuint8_tconnected() = 0;
39+
virtualoperatorbool() = 0;
40+
protected:
41+
uint8_t*rawIPAddress(IPAddress& addr) {return addr.raw_address(); };
42+
};
43+

‎api/IPAddress.cpp‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
IPAddress.cpp - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include"IPAddress.h"
21+
#include"Print.h"
22+
23+
IPAddress::IPAddress()
24+
{
25+
_address.dword =0;
26+
}
27+
28+
IPAddress::IPAddress(uint8_t first_octet,uint8_t second_octet,uint8_t third_octet,uint8_t fourth_octet)
29+
{
30+
_address.bytes[0] = first_octet;
31+
_address.bytes[1] = second_octet;
32+
_address.bytes[2] = third_octet;
33+
_address.bytes[3] = fourth_octet;
34+
}
35+
36+
IPAddress::IPAddress(uint32_t address)
37+
{
38+
_address.dword = address;
39+
}
40+
41+
IPAddress::IPAddress(constuint8_t *address)
42+
{
43+
memcpy(_address.bytes, address,sizeof(_address.bytes));
44+
}
45+
46+
boolIPAddress::fromString(constchar *address)
47+
{
48+
// TODO: add support for "a", "a.b", "a.b.c" formats
49+
50+
uint16_t acc =0;// Accumulator
51+
uint8_t dots =0;
52+
53+
while (*address)
54+
{
55+
char c = *address++;
56+
if (c >='0' && c <='9')
57+
{
58+
acc = acc *10 + (c -'0');
59+
if (acc >255) {
60+
// Value out of [0..255] range
61+
returnfalse;
62+
}
63+
}
64+
elseif (c =='.')
65+
{
66+
if (dots ==3) {
67+
// Too much dots (there must be 3 dots)
68+
returnfalse;
69+
}
70+
_address.bytes[dots++] = acc;
71+
acc =0;
72+
}
73+
else
74+
{
75+
// Invalid char
76+
returnfalse;
77+
}
78+
}
79+
80+
if (dots !=3) {
81+
// Too few dots (there must be 3 dots)
82+
returnfalse;
83+
}
84+
_address.bytes[3] = acc;
85+
returntrue;
86+
}
87+
88+
IPAddress& IPAddress::operator=(constuint8_t *address)
89+
{
90+
memcpy(_address.bytes, address,sizeof(_address.bytes));
91+
return *this;
92+
}
93+
94+
IPAddress& IPAddress::operator=(uint32_t address)
95+
{
96+
_address.dword = address;
97+
return *this;
98+
}
99+
100+
bool IPAddress::operator==(constuint8_t* addr)const
101+
{
102+
returnmemcmp(addr, _address.bytes,sizeof(_address.bytes)) ==0;
103+
}
104+
105+
size_tIPAddress::printTo(Print& p)const
106+
{
107+
size_t n =0;
108+
for (int i =0; i <3; i++)
109+
{
110+
n += p.print(_address.bytes[i], DEC);
111+
n += p.print('.');
112+
}
113+
n += p.print(_address.bytes[3], DEC);
114+
return n;
115+
}
116+

‎api/IPAddress.h‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
IPAddress.h - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#pragma once
21+
22+
#include<stdint.h>
23+
#include"Printable.h"
24+
#include"String.h"
25+
26+
// A class to make it easier to handle and pass around IP addresses
27+
28+
classIPAddress :publicPrintable {
29+
private:
30+
union {
31+
uint8_t bytes[4];// IPv4 address
32+
uint32_t dword;
33+
} _address;
34+
35+
// Access the raw byte array containing the address. Because this returns a pointer
36+
// to the internal structure rather than a copy of the address this function should only
37+
// be used when you know that the usage of the returned uint8_t* will be transient and not
38+
// stored.
39+
uint8_t*raw_address() {return _address.bytes; };
40+
41+
public:
42+
// Constructors
43+
IPAddress();
44+
IPAddress(uint8_t first_octet,uint8_t second_octet,uint8_t third_octet,uint8_t fourth_octet);
45+
IPAddress(uint32_t address);
46+
IPAddress(constuint8_t *address);
47+
48+
boolfromString(constchar *address);
49+
boolfromString(const String &address) {returnfromString(address.c_str()); }
50+
51+
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
52+
// to a four-byte uint8_t array is expected
53+
operatoruint32_t()const {return _address.dword; };
54+
booloperator==(const IPAddress& addr)const {return _address.dword == addr._address.dword; };
55+
booloperator==(constuint8_t* addr)const;
56+
57+
// Overloaded index operator to allow getting and setting individual octets of the address
58+
uint8_toperator[](int index)const {return _address.bytes[index]; };
59+
uint8_t&operator[](int index) {return _address.bytes[index]; };
60+
61+
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
62+
IPAddress&operator=(constuint8_t *address);
63+
IPAddress&operator=(uint32_t address);
64+
65+
virtualsize_tprintTo(Print& p)const;
66+
67+
friendclassEthernetClass;
68+
friendclassUDP;
69+
friendclassClient;
70+
friendclassServer;
71+
friendclassDhcpClass;
72+
friendclassDNSClient;
73+
};
74+
75+
const IPAddressINADDR_NONE(0,0,0,0);
76+

‎api/Server.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Server.h - Base class that provides Server
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#pragma once
21+
22+
#include"Print.h"
23+
24+
classServer :publicPrint {
25+
public:
26+
virtualvoidbegin() = 0;
27+
};
28+

‎api/Udp.h‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Udp.cpp: Library to send/receive UDP packets.
3+
*
4+
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
5+
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
6+
* might not happen often in practice, but in larger network topologies, a UDP
7+
* packet can be received out of sequence.
8+
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
9+
* aware of it. Again, this may not be a concern in practice on small local networks.
10+
* For more information, see http://www.cafeaulait.org/course/week12/35.html
11+
*
12+
* MIT License:
13+
* Copyright (c) 2008 Bjoern Hartmann
14+
* Permission is hereby granted, free of charge, to any person obtaining a copy
15+
* of this software and associated documentation files (the "Software"), to deal
16+
* in the Software without restriction, including without limitation the rights
17+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
* copies of the Software, and to permit persons to whom the Software is
19+
* furnished to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included in
22+
* all copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
* THE SOFTWARE.
31+
*
32+
* bjoern@cs.stanford.edu 12/30/2008
33+
*/
34+
35+
#pragma once
36+
37+
#include"Stream.h"
38+
#include"IPAddress.h"
39+
40+
classUDP :publicStream {
41+
42+
public:
43+
virtualuint8_tbegin(uint16_t) =0;// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
44+
virtualvoidstop() =0;// Finish with the UDP socket
45+
46+
// Sending UDP packets
47+
48+
// Start building up a packet to send to the remote host specific in ip and port
49+
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
50+
virtualintbeginPacket(IPAddress ip,uint16_t port) =0;
51+
// Start building up a packet to send to the remote host specific in host and port
52+
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
53+
virtualintbeginPacket(constchar *host,uint16_t port) =0;
54+
// Finish off this packet and send it
55+
// Returns 1 if the packet was sent successfully, 0 if there was an error
56+
virtualintendPacket() =0;
57+
// Write a single byte into the packet
58+
virtualsize_twrite(uint8_t) =0;
59+
// Write size bytes from buffer into the packet
60+
virtualsize_twrite(constuint8_t *buffer,size_t size) =0;
61+
62+
// Start processing the next available incoming packet
63+
// Returns the size of the packet in bytes, or 0 if no packets are available
64+
virtualintparsePacket() =0;
65+
// Number of bytes remaining in the current packet
66+
virtualintavailable() =0;
67+
// Read a single byte from the current packet
68+
virtualintread() =0;
69+
// Read up to len bytes from the current packet and place them into buffer
70+
// Returns the number of bytes read, or 0 if none are available
71+
virtualintread(unsignedchar* buffer,size_t len) =0;
72+
// Read up to len characters from the current packet and place them into buffer
73+
// Returns the number of characters read, or 0 if none are available
74+
virtualintread(char* buffer,size_t len) =0;
75+
// Return the next byte from the current packet without moving on to the next byte
76+
virtualintpeek() =0;
77+
virtualvoidflush() =0;// Finish reading the current packet
78+
79+
// Return the IP address of the host who sent the current incoming packet
80+
virtual IPAddressremoteIP() =0;
81+
// Return the port of the host who sent the current incoming packet
82+
virtualuint16_tremotePort() =0;
83+
protected:
84+
uint8_t*rawIPAddress(IPAddress& addr) {return addr.raw_address(); };
85+
};
86+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp