Exemplo n.º 1
0
        private void ProcessProtocol(object state)
        {
            HoxisProtocol proto = (HoxisProtocol)state;

            OnProtocolEntry(proto);
            switch (proto.type)
            {
            case ProtocolType.Response:
                ReqHandle handle = FF.JsonToObject <ReqHandle>(proto.handle);
                // todo 消除等待
                if (proto.err != C.RESP_SUCCESS)
                {
                    OnResponseError(proto.err, proto.desc); return;
                }
                respCbTable[proto.action.method](proto.action.args);

                break;

            case ProtocolType.Synchronization:
                HoxisAgent agent = GetAgent(proto.sender.aid);
                if (agent != null)
                {
                    agent.CallBehaviour(proto.action);
                }
                break;

            case ProtocolType.Proclamation:

                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// **WITHIN THREAD**
        /// The entrance of protocol bytes
        /// Called by HoxisClient
        /// </summary>
        /// <param name="data"></param>
        public void ProtocolEntry(byte[] data)
        {
            string        json  = FF.BytesToString(data);
            HoxisProtocol proto = FF.JsonToObject <HoxisProtocol>(json);

            _protoQueue.Enqueue(proto);
        }
Exemplo n.º 3
0
        public void SendProtocol()
        {
            Dictionary <string, string> args = new Dictionary <string, string>();

            foreach (KVString kv in arguments)
            {
                args.Add(kv.key, kv.val);
            }
            HoxisProtocol proto = new HoxisProtocol
            {
                type   = protocolType,
                handle = FF.ObjectToJson(new ReqHandle {
                    req = method, ts = SF.GetTimeStamp(TimeUnit.Millisecond)
                }),
                err      = error,
                receiver = new HoxisProtocolReceiver {
                    type = receiverType, uid = receiverUID
                },
                sender = new HoxisProtocolSender {
                    uid = senderUID, aid = senderAgentID, loopback = loopback
                },
                action = new HoxisProtocolAction(method, new HoxisProtocolArgs(args)),
                desc   = description
            };

            HoxisDirector.Ins.ProtocolPost(proto);
        }
 public void ProtocolBroadcast(HoxisProtocol proto)
 {
     foreach (HoxisUser u in _users)
     {
         u.ProtocolPost(proto);
     }
 }
        /// <summary>
        /// Post a protocol to client
        /// </summary>
        /// <param name="proto"></param>
        public void ProtocolPost(HoxisProtocol proto)
        {
            string json = FF.ObjectToJson(proto);

            byte[] data = FF.StringToBytes(json);
            OnPost(data);
        }
Exemplo n.º 6
0
 private void OnProtocolEntry(HoxisProtocol proto)
 {
     if (onProtocolEntry == null)
     {
         return;
     }
     onProtocolEntry(proto);
 }
Exemplo n.º 7
0
 private void OnProtocolPost(HoxisProtocol proto)
 {
     if (onProtocolPost == null)
     {
         return;
     }
     onProtocolPost(proto);
 }
        /// <summary>
        /// Response an error with only description
        /// </summary>
        /// <param name="handleArg"></param>
        /// <param name="descArg"></param>
        /// <returns></returns>
        public bool ResponseError(string handleArg, string errArg, string descArg)
        {
            HoxisProtocol proto = new HoxisProtocol
            {
                type     = ProtocolType.Response,
                handle   = handleArg,
                err      = errArg,
                receiver = HoxisProtocolReceiver.undef,
                sender   = HoxisProtocolSender.undef,
                action   = HoxisProtocolAction.undef,
                desc     = descArg
            };

            ProtocolPost(proto);
            return(true);
        }
        /// <summary>
        /// Response a custom result, hardly called immediately
        /// </summary>
        /// <param name="handleArg"></param>
        /// <param name="actionArg"></param>
        /// <returns></returns>
        public bool Response(string handleArg, HoxisProtocolAction actionArg)
        {
            HoxisProtocol proto = new HoxisProtocol
            {
                type     = ProtocolType.Response,
                handle   = handleArg,
                err      = C.RESP_SUCCESS,
                receiver = HoxisProtocolReceiver.undef,
                sender   = HoxisProtocolSender.undef,
                action   = actionArg,
                desc     = ""
            };

            ProtocolPost(proto);
            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Rapidly send a request protocol
        /// </summary>
        /// <param name="method"></param>
        /// <param name="kvs"></param>
        public void Request(string method, params KVString[] kvs)
        {
            HoxisProtocol proto = new HoxisProtocol
            {
                type   = ProtocolType.Request,
                handle = FF.ObjectToJson(new ReqHandle {
                    req = method, ts = SF.GetTimeStamp(TimeUnit.Millisecond)
                }),
                err      = "",
                receiver = HoxisProtocolReceiver.undef,
                sender   = HoxisProtocolSender.undef,
                action   = new HoxisProtocolAction(method, kvs),
                desc     = ""
            };

            ProtocolPost(proto);
            // todo wait for response
        }
Exemplo n.º 11
0
        /// <summary>
        /// Report an action of this gameObject, generally syn
        /// </summary>
        /// <param name="action"></param>
        public void Report(HoxisProtocolAction actionArg)
        {
            HoxisProtocol proto = new HoxisProtocol
            {
                type     = ProtocolType.Synchronization,
                handle   = "",
                receiver = HoxisProtocolReceiver.cluster,
                sender   = new HoxisProtocolSender
                {
                    aid      = id,
                    loopback = true,
                },
                action = actionArg,
                desc   = "",
            };

            Report(proto);
        }
Exemplo n.º 12
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            //线程调用测试
            HoxisProtocol proto = new HoxisProtocol
            {
                type     = ProtocolType.Synchronization,
                receiver = new HoxisProtocolReceiver
                {
                    type = ReceiverType.Cluster,
                    uid  = 0,
                },
                sender = new HoxisProtocolSender
                {
                    aid      = new HoxisAgentID("soldier", 10),
                    loopback = true,
                },
                action = new HoxisProtocolAction
                {
                    method = "shoot",
                    args   = new HoxisProtocolArgs
                    {
                        values = new Dictionary <string, string> {
                            { "val", "15" },
                            { "src", "weapon" },
                        }
                    },
                },
                desc = "thread test",
            };
            string json = FormatFunc.ObjectToJson(proto);
            byte[] data = FormatFunc.StringToBytes(json);

            Thread t = new Thread(() =>
            {
                //Thread.Sleep(1000);
                HoxisDirector.Ins.ProtocolEntry(data);
            });
            t.Start();
        }
    }
Exemplo n.º 13
0
        /// <summary>
        /// Check the request name and time stamp
        /// </summary>
        /// <param name="proto"></param>
        /// <param name="ret"></param>
        public void CheckRequest(HoxisProtocol proto, out Ret ret)
        {
            ReqHandle handle = FF.JsonToObject <ReqHandle>(proto.handle, out ret);

            if (ret.code != 0)
            {
                return;
            }
            // Check if request name matches method name
            if (handle.req != proto.action.method)
            {
                ret = new Ret(LogLevel.Info, 1, "request name doesn't match method name"); return;
            }
            // Check if expired
            long ts   = handle.ts;
            long intv = Math.Abs(SF.GetTimeStamp(TimeUnit.Millisecond) - ts);

            if (intv > requestTTL)
            {
                ret = new Ret(LogLevel.Info, 1, "request is expired"); return;
            }
            ret = Ret.ok;
        }
Exemplo n.º 14
0
 public void CallBehaviour(HoxisProtocol proto)
 {
     _behav.Act(proto);
 }
 public void Act(HoxisProtocol proto)
 {
     Act(proto.action.method, proto.action.args);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Build custom protocol to report
 /// </summary>
 /// <param name="proto"></param>
 public void Report(HoxisProtocol proto)
 {
     HoxisDirector.Ins.ProtocolPost(proto);
 }
Exemplo n.º 17
0
        /// <summary>
        /// **WITHIN THREAD**
        /// The entrance of protocol bytes
        /// </summary>
        /// <param name="data"></param>
        public void ProtocolEntry(byte[] data)
        {
            string        json  = FF.BytesToString(data);
            HoxisProtocol proto = FF.JsonToObject <HoxisProtocol>(json);

            switch (proto.type)
            {
            case ProtocolType.Synchronization:
                switch (proto.receiver.type)
                {
                case ReceiverType.Cluster:
                    if (realtimeData.parentCluster == null)
                    {
                        return;
                    }
                    realtimeData.parentCluster.ProtocolBroadcast(proto);
                    break;

                case ReceiverType.Team:
                    if (realtimeData.parentTeam == null)
                    {
                        return;
                    }
                    realtimeData.parentTeam.ProtocolBroadcast(proto);
                    break;

                case ReceiverType.User:
                    HoxisUser user = HoxisServer.Ins.GetUser(proto.receiver.uid);
                    // todo send
                    break;
                }
                break;

            case ProtocolType.Request:
                // Request check
                Ret ret;
                CheckRequest(proto, out ret);
                if (ret.code != 0)
                {
                    ResponseError(proto.handle, C.RESP_CHECK_FAILED, ret.desc);
                    return;
                }
                // Check ok
                if (!respTable.ContainsKey(proto.action.method))
                {
                    if (DebugRecorder.LogEnable(_logger))
                    {
                        _logger.LogError(FF.StringFormat("invalid request: {0}", proto.action.method), "", true);
                    }
                    ResponseError(proto.handle, C.RESP_CHECK_FAILED, FF.StringFormat("invalid request: {0}", proto.action.method));
                    return;
                }
                respTable[proto.action.method](proto.handle, proto.action.args);
                break;

            default:
                if (DebugRecorder.LogEnable(_logger))
                {
                    _logger.LogError(FF.StringFormat("invalid protocol type: {0}", proto.type), "");
                }
                break;
            }
        }