Advertisement
Guest User

Untitled

a guest
Sep 18th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. import std.stdio;
  2. import std.socket;
  3. import std.conv;
  4. import core.thread;
  5.  
  6. /**
  7.   This example shows how to realize a network server without threading.
  8.   Instead of Threads we use Fibers as a replacement, to not block our running Fibers
  9.   the Sockets need to be run in non-blocking mode.
  10. **/
  11.  
  12. // worker fiber for each client
  13. // waits for data and prints it out
  14. class ClientFiber : Fiber {
  15.   private Socket sock;
  16.   this(Socket s) {
  17.     this.sock = s;
  18.     super(&run);
  19.   }
  20.   private void run() {
  21.     while(true) {
  22.  
  23.       // receive and show data as a string
  24.       byte[1024] buf;
  25.       int len = sock.receive(buf);
  26.       if(len > 0) {
  27.         string s = (cast(immutable(char)*)buf)[0..len];
  28.         writeln(s);
  29.       }
  30.  
  31.       Fiber.yield(); // return to the caller
  32.       // when the fiber is called again, we continue here
  33.     }
  34.   }
  35. }
  36.  
  37. void main(string[] args)
  38. {
  39.   if(args.length == 1) // if we provide no args run a server
  40.   {
  41.     // create a socket that accepts new connections
  42.     // INET is using IP4, STREAM is a TCP connection
  43.     auto listener = new Socket(AddressFamily.INET, SocketType.STREAM);
  44.     listener.blocking(false);
  45.     listener.bind(new InternetAddress(2525));
  46.     listener.listen(10);
  47.  
  48.     // stores all fibers that need to be run
  49.     Fiber[] fibers;
  50.  
  51.     // listener for new connections
  52.     void listen() {
  53.       writeln("listen");
  54.       Socket newclient;
  55.       while(listener.isAlive()) {
  56.         newclient = listener.accept();
  57.         if(!wouldHaveBlocked()) {
  58.           Fiber clientfiber = new  ClientFiber(newclient);
  59.           fibers ~= clientfiber;
  60.           writeln(fibers.length);
  61.         }
  62.         Fiber.yield();
  63.       }
  64.     }
  65.  
  66.     // register the listener fiber
  67.     Fiber listenFiber = new Fiber(&listen);
  68.     fibers ~= listenFiber;
  69.  
  70.     // main loop of the program - runs all fibers
  71.     while(true) {
  72.       foreach(f; fibers) {
  73.         f.call();
  74.       }
  75.       Thread.sleep(100.msecs); // to not eat up all your cpu
  76.     }
  77.   }
  78.   else // if the program is run with an argument start a client
  79.   {
  80.     // connect to the server socket
  81.     auto sock = new Socket(AddressFamily.INET, SocketType.STREAM);
  82.     sock.connect(new InternetAddress("127.0.0.1", 2525));
  83.  
  84.     // send some data
  85.     sock.send("foo!");
  86.     Thread.sleep(4*1_000.msecs);
  87.     sock.send("bar");
  88.     Thread.sleep(100.msecs);
  89.  
  90.     // experiment with receiving on the client side
  91.     /*while(true) {
  92.       byte[1024] buf;
  93.       int len = sock.receive(buf);
  94.       if(len > 0) {
  95.         string s = (cast(immutable(char)*)buf)[0..len];
  96.         writeln(s);
  97.       }
  98.     }*/
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement