36 #include <FlowScanner.h>
43 static const uint8_t
CS_PIN = 10;
49 static const uint8_t MAX_CLIENTS = 4;
50 static const uint16_t CHAT_SERVER_TCP_PORT = 2500;
56 static ChatServer clients[MAX_CLIENTS];
60 void start(uint16_t port)
68 ACTRACE(
"onConnectRequest");
70 for (
int i = 0; i < MAX_CLIENTS; i++)
72 ChatServer& sck = clients[i];
80 ACERROR(
"No more connections available");
86 write(F(
"Welcome to the chat room\n"));
87 Serial.println(F(
"New client connected"));
88 say(F(
">> Somebody joined the room\n"),
this);
98 Serial.println(F(
"Client disconnected."));
99 say(F(
">> Someone left the room\n"));
102 void say(uint16_t len,
const byte* data, ChatServer* exclude)
104 for (
int i = 0; i < MAX_CLIENTS; i++)
106 ChatServer& sck = clients[i];
109 sck.write(len, data);
114 void say(
const String& st, ChatServer* exclude = NULL)
116 say(st.length(), (byte*)st.c_str(),exclude);
119 void onReceive(uint16_t len,
const byte* data)
121 ACTRACE(
"onReceive: %d bytes",len);
123 say(len, data,
this);
129 ChatServer ChatServer::clients[MAX_CLIENTS];
135 Serial.begin(115200);
137 Serial.println(F(
"EtherDune ChatServer sample"));
138 Serial.print(F(
"Free RAM: ")); Serial.println(ACross::getFreeRam());
140 Serial.println(F(
"Press any key to start..."));
142 while (!Serial.available());
152 ACERROR(
"failed to start EtherDune");
154 ACINFO(
"waiting for link...");
158 ACINFO(
"link is up");
161 chatServer.start(CHAT_SERVER_TCP_PORT);
162 Serial.println(F(
"Chat server is up"));
163 Serial.print(F(
"Listening on TCP port "));
164 Serial.println(chatServer.localPort);
virtual void onConnectRequest()
Called when a listening socket receives a connection request.
#define IPAddress_P
helper macro to store an IP address in PROGMEM
uint16_t write(uint16_t len, const byte *data)
In the case of TCP, writes the given data buffer to the socket.
virtual void onConnect()
Fires when the socket connection is established.
static IPAddress_P gatewayIP
void listen()
Starts listening on the local port indicated by the localPort property.
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
static const uint8_t SCK_STATE_CLOSED
(both server and client) represents no connection state at all.
static IPAddress netmask
Subnet mask.
static IPAddress gatewayIP
IP address of the gateway in this network.
static const uint8_t CS_PIN
static MACAddress_P mymac
static bool isLinkUp()
Determines whether the network link is ready
Implements the TCP protocol.
static MACAddress localMAC
Ethernet MAC address.
static const uint8_t SCK_STATE_ESTABLISHED
(both server and client) represents an open connection, data received can be delivered to the user...
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
virtual void onTerminate()
Called when the socket is ready to be reused.
static IPAddress localIP
IP address of this application.
void close()
Attempts to gracefully close a connection.
virtual void onReceive(uint16_t len, const byte *data)
Called for each data packet received
static IPAddress_P netmask
virtual void onClose()
Fires when the other party closed the incoming end of the connection because it has no more data to s...