Esempio 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;
            }
        }
Esempio 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);
        }
        /// <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;
        }
        /// <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;
            }
        }