Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.44 KB | None | 0 0
  1. package hx.ws;
  2.  
  3. import haxe.Constraints;
  4. import haxe.io.Error;
  5.  
  6. @:generic
  7. class WebSocketServer
  8.     #if (haxe_ver < 4)
  9.     <T:(Constructible<SocketImpl->Void>, Handler)> {
  10.     #else
  11.     <T:Constructible<SocketImpl->Void> & Handler> {
  12.     #end
  13.    
  14.     private var _serverSocket:SocketImpl;
  15.     private var _handlers:Array<T> = [];
  16.    
  17.     private var _host:String;
  18.     private var _port:Int;
  19.     private var _maxConnections:Int;
  20.    
  21.     private var _stopServer:Bool = false;
  22.    
  23.     public function new(host:String, port:Int, maxConnections:Int = 1) {
  24.         _host = host;
  25.         _port = port;
  26.         _maxConnections = maxConnections;
  27.     }
  28.    
  29.     public function start() {
  30.         _stopServer = false;
  31.        
  32.         _serverSocket = new SocketImpl();
  33.         _serverSocket.setBlocking(false);
  34.         _serverSocket.bind(new sys.net.Host(_host), _port);
  35.         _serverSocket.listen(_maxConnections);
  36.         Log.info('Starting server - ${_host}:${_port} (maxConnections: ${_maxConnections})');
  37.        
  38.         while (true) {
  39.             if (_stopServer == true) {
  40.                 for (h in _handlers) {
  41.                     h.close();
  42.                 }
  43.                 _handlers = [];
  44.                 try {
  45.                     _serverSocket.close();
  46.                 } catch (e:Dynamic) { }
  47.                 break;
  48.             }
  49.            
  50.             try {
  51.                 var clientSocket:SocketImpl = _serverSocket.accept();
  52.                 var handler = new T(clientSocket);
  53.                 _handlers.push(handler);
  54.                 Log.debug("Adding to web server handler to list - total: " + _handlers.length, handler.id);
  55.             } catch (e:Dynamic) {
  56.                 if (e != 'Blocking' && e != Error.Blocked) {
  57.                     throw(e);
  58.                 }
  59.             }
  60.            
  61.             for (h in _handlers) {
  62.                 h.handle();
  63.             }
  64.            
  65.             var toRemove = [];
  66.             for (h in _handlers) {
  67.                 if (h.state == HandlerState.Closed) {
  68.                     toRemove.push(h);
  69.                 }
  70.             }
  71.            
  72.             for (h in toRemove) {
  73.                 _handlers.remove(h);
  74.                 Log.debug("Removing web server handler from list - total: " + _handlers.length, h.id);
  75.             }
  76.            
  77.             Sys.sleep(0.01);
  78.         }
  79.     }
  80.    
  81.     public function stop() {
  82.         _stopServer = true;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement