Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
Revision 8:46437b951a2f, committed 2013-11-07
- Comitter:
- okini3939
- Date:
- Thu Nov 07 01:44:20 2013 +0000
- Parent:
- 7:0a3cf89d5b93
- Commit message:
- re-use thread
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Nov 01 05:35:31 2013 +0000
+++ b/main.cpp Thu Nov 07 01:44:20 2013 +0000
@@ -4,36 +4,54 @@
#define ECHO_SERVER_PORT 10000
-#define THREAD_MAX 10
+#define THREAD_MAX 5
Thread *threads[THREAD_MAX];
+Thread *xthread;
void child (void const *arg) {
int i;
char buf[256];
TCPSocketConnection *client = (TCPSocketConnection*)arg;
- printf("Connection from %s\r\n", client->get_address());
-// client->set_blocking(false, 1500); // Timeout after (1.5)s
for (;;) {
- i = client->receive(buf, sizeof(buf));
- if (i < 0) break;
- if (i == 0) continue;
- buf[i] = 0;
- printf("Recv '%s'\r\n", buf);
- if (client->send(buf, i) < 0) break;
+ printf("Connection from %s\r\n", client->get_address());
+// client->set_blocking(false, 1500); // Timeout after (1.5)s
+ for (;;) {
+ i = client->receive(buf, sizeof(buf));
+ if (i < 0) break;
+ if (i == 0) continue;
+ buf[i] = 0;
+ printf("Recv '%s'\r\n", buf);
+ if (client->send(buf, i) < 0) break;
+ }
+
+ client->close();
+ printf("Close %s\r\n", client->get_address());
+
+ Thread::signal_wait(1);
}
+}
- client->close();
- printf("Close %s\r\n", client->get_address());
+void closer (void const *arg) {
+ TCPSocketConnection *client = (TCPSocketConnection*)arg;
+
+ for (;;) {
+ client->close();
+ printf("Close %s\r\n", client->get_address());
+
+ Thread::signal_wait(1);
+ }
}
void parent () {
int i, t = 0;
- TCPSocketConnection *client;
+ TCPSocketConnection clients[THREAD_MAX];
+ TCPSocketConnection xclient;
for (i = 0; i < THREAD_MAX; i ++) {
threads[i] = NULL;
}
+ xthread = NULL;
TCPSocketServer server;
server.bind(ECHO_SERVER_PORT);
@@ -42,15 +60,28 @@
printf("Wait for new connection...\r\n");
for (;;) {
if (t >= 0) {
- client = new TCPSocketConnection;
- server.accept(*client);
+ server.accept(clients[t]);
// fork child process
- threads[t] = new Thread(child, (void*)client);
+ if (threads[t]) {
+ threads[t]->signal_set(1);
+ } else {
+ threads[t] = new Thread(child, (void*)&clients[t]);
+ }
printf("Forked %d\r\n", t);
+ } else {
+ server.accept(xclient);
+ // closer process
+ if (xthread) {
+ xthread->signal_set(1);
+ } else {
+ xthread = new Thread(closer, (void*)&xclient);
+ }
+ printf("Connection full\r\n");
}
-
+
t = -1;
for (i = 0; i < THREAD_MAX; i ++) {
+/*
if (threads[i]) {
if (threads[i]->get_state() == Thread::Inactive) {
// terminate process
@@ -59,8 +90,9 @@
printf("Terminated %d\r\n", i);
}
}
- if (threads[i] == NULL) {
- if (t < 0) t = i; // empty thread
+*/
+ if (threads[i] == NULL || threads[i]->get_state() == Thread::WaitingAnd) {
+ if (t < 0) t = i; // next empty thread
}
}
}
@@ -71,7 +103,7 @@
EthernetInterface eth;
printf("TCP Echo Server with Fork...\r\n");
eth.init(); //Use DHCP
- eth.connect();
+ if (eth.connect()) return -1;
printf("IP Address is %s\r\n", eth.getIPAddress());
parent();
