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.executor.ExecutorInfo; 13 14 import msgtrans.MessageBuffer; 15 import msgtrans.TransportContext; 16 import msgtrans.channel.TransportSession; 17 18 import hunt.logging; 19 import witchcraft; 20 21 import std.conv; 22 import std.range; 23 24 /** 25 * 26 */ 27 struct ExecutorInfo { 28 private string _id = ""; 29 private uint _messageId; 30 private Class _classInfo; 31 private Method _methodInfo; 32 33 this(uint messageId, Class classInfo, Method method) { 34 this._messageId = messageId; 35 this._classInfo = classInfo; 36 this._methodInfo = method; 37 } 38 39 string id() { 40 if (_id.empty()) 41 _id = _messageId.to!string(); 42 return _id; 43 } 44 45 uint messageId() { 46 return _messageId; 47 } 48 49 Class classInfo() { 50 return _classInfo; 51 } 52 53 Method methodInfo() { 54 return _methodInfo; 55 } 56 57 /** 58 * 59 * Params: 60 * session = 61 * buffer = 62 * args = 63 */ 64 void execute(Args...)(ref TransportContext context, MessageBuffer buffer, Args args) nothrow { 65 try { 66 string objectKey = id(); 67 TransportSession session = context.session(); 68 Object obj = session.getAttribute(objectKey); 69 if (obj is null) { 70 obj = _classInfo.create(); 71 session.setAttribute(objectKey, obj); 72 } 73 // switch(id) 74 // { 75 // case 10001: 76 // string msg = cast(string)codec.decode(ubyte[]); 77 // this.hello(ctx, msg); 78 // } 79 // string msg = cast(string)codec.decode(ubyte[]); 80 _methodInfo.invoke(obj, context, buffer); 81 } catch (Throwable ex) { 82 //warning(ex.msg); 83 //version (HUNT_DEBUG) 84 // warning(ex); 85 } 86 } 87 }