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.ConsoleLogger;
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             _methodInfo.invoke(obj, context, buffer);
74         } catch (Throwable ex) {
75             warning(ex);
76         }
77     }
78 }