1 /* 2 * MsgTrans - Message Transport Framework for DLang. Based on TCP, WebSocket, UDP transmission protocol. 3 * 4 * Copyright (C) 2019 HuntLabs 5 * 6 * Website: https://www.msgtrans.org 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module msgtrans.channel.websocket.WebSocketChannel; 13 14 import msgtrans.executor; 15 import msgtrans.PacketParser; 16 import msgtrans.MessageBuffer; 17 import msgtrans.channel.websocket.WebSocketTransportSession; 18 19 import hunt.io.ByteBuffer; 20 import hunt.http.server; 21 import hunt.logging.ConsoleLogger; 22 import hunt.net; 23 import hunt.util.DateTime; 24 25 import std.format; 26 import std.stdio; 27 28 /** 29 * 30 */ 31 abstract class WebSocketChannel { 32 33 /** The default maximum buffer length. Default to 128 chars. */ 34 private int bufferLength = 128; 35 36 /** The default maximum Line length. Default to 1024. */ 37 private int maxLineLength = 8*1024; 38 39 protected void decode(WebSocketConnection connection, ByteBuffer buf) { 40 PacketParser parser = getParser(connection); 41 42 MessageBuffer[] msgBuffers = parser.parse(buf); 43 if(msgBuffers is null) { 44 warning("no message frame parsed."); 45 return; 46 } 47 48 foreach(MessageBuffer msg; msgBuffers) { 49 dispatchMessage(connection, msg); 50 } 51 } 52 53 protected PacketParser getParser(WebSocketConnection connection) { 54 enum string PARSER = "PacketParser"; 55 PacketParser ctx; 56 ctx = cast(PacketParser) connection.getAttribute(PARSER); 57 58 if (ctx is null) { 59 ctx = new PacketParser(bufferLength); 60 connection.setAttribute(PARSER, ctx); 61 } 62 63 return ctx; 64 } 65 66 protected void dispatchMessage(WebSocketConnection connection, MessageBuffer message ); 67 }