EthernetInterface, TCP Echo Server, support multi session

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by mbed official

mbed RTOS により、マルチセッションに対応した TCP Echo Server です。

複数のTCP接続を受付け、処理することができます。

accept の後に fork (スレッドを生成)しています。

Revision:
7:0a3cf89d5b93
Parent:
3:36fd3cfad85a
Child:
8:46437b951a2f
diff -r 5dae237341f7 -r 0a3cf89d5b93 main.cpp
--- a/main.cpp	Tue Jun 04 16:07:06 2013 +0100
+++ b/main.cpp	Fri Nov 01 05:35:31 2013 +0000
@@ -1,34 +1,78 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "EthernetInterface.h"
 
-#define ECHO_SERVER_PORT   7
+#define ECHO_SERVER_PORT   10000
+
+#define THREAD_MAX 10
+Thread *threads[THREAD_MAX];
+
+void child (void const *arg) {
+    int i;
+    char buf[256];
+    TCPSocketConnection *client = (TCPSocketConnection*)arg;
 
-int main (void) {
-    EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
-    
+    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());
+}
+
+void parent () {
+    int i, t = 0;
+    TCPSocketConnection *client;
+
+    for (i = 0; i < THREAD_MAX; i ++) {
+        threads[i] = NULL;
+    }
+
     TCPSocketServer server;
     server.bind(ECHO_SERVER_PORT);
     server.listen();
-    
-    while (true) {
-        printf("\nWait for new connection...\n");
-        TCPSocketConnection client;
-        server.accept(client);
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
-        
-        printf("Connection from: %s\n", client.get_address());
-        char buffer[256];
-        while (true) {
-            int n = client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
-            
-            client.send_all(buffer, n);
-            if (n <= 0) break;
+
+    printf("Wait for new connection...\r\n");
+    for (;;) {
+        if (t >= 0) {
+            client = new TCPSocketConnection;
+            server.accept(*client);
+            // fork child process
+            threads[t] = new Thread(child, (void*)client);
+            printf("Forked %d\r\n", t);
         }
         
-        client.close();
+        t = -1;
+        for (i = 0; i < THREAD_MAX; i ++) {
+            if (threads[i]) {
+                if (threads[i]->get_state() == Thread::Inactive) {
+                    // terminate process
+//                    delete threads[i];
+//                    threads[i] = NULL;
+                    printf("Terminated %d\r\n", i);
+                }
+            }
+            if (threads[i] == NULL) {
+                if (t < 0) t = i; // empty thread
+            }
+        }
     }
 }
+
+
+int main () {
+    EthernetInterface eth;
+    printf("TCP Echo Server with Fork...\r\n");
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+
+    parent();
+}
\ No newline at end of file