Skip to content

Commit

Permalink
added server.accept()
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Jan 8, 2022
1 parent 04f088b commit 0c532f7
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
103 changes: 103 additions & 0 deletions examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Advanced WiFi Chat Server
A more advanced server that distributes any incoming messages
to all connected clients but the client the message comes from.
To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Circuit:
* WiFi 101 Shield attached
*/

#include <SPI.h>
#include <WiFi101.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

// telnet defaults to port 23
WiFiServer server(23);

WiFiClient clients[8];

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the WiFi module:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi 101 Shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// start the server:
server.begin();

Serial.print("Chat server address:");
Serial.println(WiFi.localIP());
}

void loop() {
// check for any new client connecting, and say hello (before any incoming data)
WiFiClient newClient = server.accept();
if (newClient) {
for (byte i=0; i < 8; i++) {
if (!clients[i]) {
Serial.print("We have a new client #");
Serial.println(i);
newClient.print("Hello, client number: ");
newClient.println(i);
// Once we "accept", the client is no longer tracked by WiFiServer
// so we must store it into our list of clients
clients[i] = newClient;
break;
}
}
}

// check for incoming data from all clients
for (byte i=0; i < 8; i++) {
if (clients[i] && clients[i].available() > 0) {
// read bytes from a client
byte buffer[80];
int count = clients[i].read(buffer, 80);
// write the bytes to all other connected clients
for (byte j=0; j < 8; j++) {
if (j != i && clients[j].connected()) {
clients[j].write(buffer, count);
}
}
}
}

// stop any clients which disconnect
for (byte i=0; i < 8; i++) {
if (clients[i] && !clients[i].connected()) {
Serial.print("disconnect client #");
Serial.println(i);
clients[i].stop();
}
}

}
2 changes: 2 additions & 0 deletions examples/WiFiAdvancedChatServer/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
19 changes: 18 additions & 1 deletion src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ WiFiClient WiFiServer::available(uint8_t* status)
}

for (SOCKET s = 0; s < TCP_SOCK_MAX; s++) {
if (WiFiSocket.hasParent(_socket, s) && WiFiSocket.available(s)) {
if (WiFiSocket.hasParent(_socket, s) && !WiFiSocket.isAccepted(s) && WiFiSocket.available(s)) {
return WiFiClient(s);
}
}
Expand All @@ -101,6 +101,23 @@ WiFiClient WiFiServer::available(uint8_t* status)
return WiFiClient();
}

WiFiClient WiFiServer::accept()
{
if (_socket != -1 && !WiFiSocket.listening(_socket)) {
_socket = -1;
}

if (_socket != -1) {
SOCKET child = WiFiSocket.accepted(_socket);

if (child > -1) {
WiFiSocket.setAccepted(child, true);
return WiFiClient(child);
}
}
return WiFiClient();
}

uint8_t WiFiServer::status() {
// Deprecated.
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WiFiServer : public Server {
public:
WiFiServer(uint16_t);
WiFiClient available(uint8_t* status = NULL);
WiFiClient accept();
void begin();
uint8_t beginSSL();
virtual size_t write(uint8_t);
Expand Down
12 changes: 11 additions & 1 deletion src/utility/WiFiSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ SOCKET WiFiSocketClass::accepted(SOCKET sock)
for (SOCKET s = 0; s < TCP_SOCK_MAX; s++) {
if (_info[s].parent == sock && _info[s].state == SOCKET_STATE_ACCEPTED) {
_info[s].state = SOCKET_STATE_CONNECTED;

_info[s].accepted = false;
_info[s].recvMsg.s16BufferSize = 0;
recv(s, NULL, 0, 0);

Expand Down Expand Up @@ -519,4 +519,14 @@ int WiFiSocketClass::fillRecvBuffer(SOCKET sock)
return 1;
}

void WiFiSocketClass::setAccepted(SOCKET sock, bool b)
{
_info[sock].accepted = b;
}

bool WiFiSocketClass::isAccepted(SOCKET sock)
{
return _info[sock].accepted;
}

WiFiSocketClass WiFiSocket;
4 changes: 4 additions & 0 deletions src/utility/WiFiSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class WiFiSocketClass {
SOCKET accepted(SOCKET sock);
int hasParent(SOCKET sock, SOCKET child);

void setAccepted(SOCKET sock, bool b);
bool isAccepted(SOCKET sock);

static void eventCallback(SOCKET sock, uint8 u8Msg, void *pvMsg);

private:
Expand All @@ -69,6 +72,7 @@ class WiFiSocketClass {
int length;
} buffer;
struct sockaddr _lastSendtoAddr;
bool accepted;
} _info[MAX_SOCKET];
};

Expand Down

0 comments on commit 0c532f7

Please sign in to comment.