コード例 #1
0
        /// <inheritdoc/>
        public void SendMessage(byte *message, int size, out TrinityResponse response)
        {
            TrinityMessageType msgType = *(TrinityMessageType *)(message + TrinityProtocol.MsgTypeOffset);
            ushort             msgId   = *(ushort *)(message + TrinityProtocol.MsgIdOffset);
            SynReqRspArgs      sync_rsp_args;
            TrinityErrorCode   msgProcessResult;

            if (msgType == TrinityMessageType.PRESERVED_SYNC_WITH_RSP)
            {
                sync_rsp_args = new SynReqRspArgs(message,
                                                  TrinityProtocol.MsgHeader,
                                                  size - TrinityProtocol.MsgHeader,
                                                  MessageHandlers.DefaultParser.preserved_sync_rsp_handlers[msgId]);
            }
            else// msgType == TrinityMessageType.SYNC_WITH_RSP
            {
                sync_rsp_args = new SynReqRspArgs(message,
                                                  TrinityProtocol.MsgHeader,
                                                  size - TrinityProtocol.MsgHeader,
                                                  MessageHandlers.DefaultParser.sync_rsp_handlers[msgId]);
            }
            msgProcessResult = sync_rsp_args.MessageProcess();
            if (msgProcessResult == TrinityErrorCode.E_SUCCESS)
            {
                response = new TrinityResponse(sync_rsp_args.Response);
            }
            else//  msgProcessResult == TrinityErrorCode.E_RPC_EXCEPTION
            {
                throw new IOException("Local message handler throws an exception.");
            }
        }
コード例 #2
0
ファイル: TrinityMessage.cs プロジェクト: qx-will/GraphEngine
 internal TrinityMessage(TrinityMessageType msgType, ushort msgId, int size)
 {
     Size           = TrinityProtocol.MsgHeader + size;
     Buffer         = (byte *)CMemory.C_malloc((ulong)Size);
     *(int *)Buffer = TrinityProtocol.TrinityMsgHeader + size;
     *(Buffer + TrinityProtocol.MsgTypeOffset)         = (byte)msgType;
     *(ushort *)(Buffer + TrinityProtocol.MsgIdOffset) = msgId;
 }
コード例 #3
0
        internal override void SendMessage(byte *message, int size)
        {
            TrinityMessageType msgType = (TrinityMessageType)message[TrinityProtocol.MsgTypeOffset];
            int msgId = message[TrinityProtocol.MsgIdOffset];
            TrinityErrorCode msgProcessResult;

            switch (msgType)
            {
            case TrinityMessageType.PRESERVED_SYNC:
                SynReqArgs preserved_sync_args = new SynReqArgs(message,
                                                                TrinityProtocol.MsgHeader, size - TrinityProtocol.MsgHeader,
                                                                MessageHandlers.DefaultParser.preserved_sync_handlers[msgId]);
                msgProcessResult = preserved_sync_args.MessageProcess();
                break;

            case TrinityMessageType.SYNC:
                SynReqArgs sync_args = new SynReqArgs(message,
                                                      TrinityProtocol.MsgHeader,
                                                      size - TrinityProtocol.MsgHeader,
                                                      MessageHandlers.DefaultParser.sync_handlers[msgId]);
                msgProcessResult = sync_args.MessageProcess();
                break;

            case TrinityMessageType.PRESERVED_ASYNC:
            {
                AsynReqArgs aut_request = new AsynReqArgs(message,
                                                          TrinityProtocol.MsgHeader,
                                                          size - TrinityProtocol.MsgHeader,
                                                          MessageHandlers.DefaultParser.preserved_async_handlers[msgId]);
                msgProcessResult = aut_request.AsyncProcessMessage();
            }
            break;

            case TrinityMessageType.ASYNC:
            {
                AsynReqArgs aut_request = new AsynReqArgs(message,
                                                          TrinityProtocol.MsgHeader,
                                                          size - TrinityProtocol.MsgHeader,
                                                          MessageHandlers.DefaultParser.async_handlers[msgId]);
                msgProcessResult = aut_request.AsyncProcessMessage();
            }
            break;

            default:
                throw new IOException("Wrong message type.");
            }

            if (msgProcessResult == TrinityErrorCode.E_RPC_EXCEPTION)
            {
                throw new IOException("Local message handler throws an exception.");
            }
        }
コード例 #4
0
        /// <inheritdoc/>
        public void SendMessage(byte **message, int *sizes, int count)
        {
            byte *buf;
            int   len;

            _serialize(message, sizes, count, out buf, out len);

            TrinityMessageType msgType = *(TrinityMessageType *)(buf + TrinityProtocol.MsgTypeOffset);
            ushort             msgId   = *(ushort *)(buf + TrinityProtocol.MsgIdOffset);

            // For async messages, we omit the buffer copy, use the serialized buffer directly.
            switch (msgType)
            {
            case TrinityMessageType.ASYNC:
            {
                AsynReqArgs aut_request = new AsynReqArgs(
                    MessageHandlers.DefaultParser.async_handlers[msgId],
                    buf,
                    TrinityProtocol.MsgHeader,
                    len - TrinityProtocol.MsgHeader);
                if (aut_request.AsyncProcessMessage() == TrinityErrorCode.E_RPC_EXCEPTION)
                {
                    throw new IOException("Local message handler throws an exception.");
                }
            }
            break;

            case TrinityMessageType.ASYNC_WITH_RSP:
            {
                AsynReqRspArgs async_rsp_args = new AsynReqRspArgs(
                    MessageHandlers.DefaultParser.async_rsp_handlers[msgId],
                    buf,
                    TrinityProtocol.MsgHeader,
                    len - TrinityProtocol.MsgHeader);
                if (async_rsp_args.AsyncProcessMessage() == TrinityErrorCode.E_RPC_EXCEPTION)
                {
                    throw new IOException("Local message handler throws an exception.");
                }
            }
            break;

            default:
            {
                SendMessage(buf, len);
                CMemory.C_free(buf);
            }
            break;
            }
        }
コード例 #5
0
        public static void RegisterProtocolSemantic(TrinityMessageType messageType, ushort messageId, ProtocolSemantic semantic)
        {
            int mt = (int)messageType;
            int ms = (int)semantic;

            if (mt < 0 || mt >= 6 || ms < 0 || ms >= (int)ProtocolSemantic.ProtocolSemanticEND)
            {
                throw new ArgumentOutOfRangeException(nameof(messageType));
            }
            if (semantic == ProtocolSemantic.Broadcast &&
                (messageType == TrinityMessageType.ASYNC_WITH_RSP ||
                 messageType == TrinityMessageType.SYNC_WITH_RSP ||
                 messageType == TrinityMessageType.PRESERVED_SYNC_WITH_RSP))
            {
                throw new ArgumentException($"Cannot register {ProtocolSemantic.Broadcast} semantic for a protocol with response. Use {nameof(Partition.Broadcast)} method instead.");
            }
            s_protocolSemantics[mt, messageId] = ms;
        }
コード例 #6
0
        internal void *DispatchMessage(MessageBuff *sendRecvBuff)
        {
            byte *ByteArray = sendRecvBuff->Buffer;
            int   Length    = (int)sendRecvBuff->Length;

            TrinityMessageType msgType = *(TrinityMessageType *)(sendRecvBuff->Buffer + TrinityProtocol.TrinityMsgTypeOffset);
            ushort             msgId   = *(ushort *)(ByteArray + TrinityProtocol.TrinityMsgIdOffset);
            TrinityErrorCode   msgProcessResult;

            try
            {
                switch (msgType)
                {
                case TrinityMessageType.SYNC_WITH_RSP:
                    SynReqRspArgs sync_rsp_args = new SynReqRspArgs(ByteArray,
                                                                    TrinityProtocol.TrinityMsgHeader,
                                                                    Length - TrinityProtocol.TrinityMsgHeader,
                                                                    sync_rsp_handlers[msgId]);
                    msgProcessResult = sync_rsp_args.MessageProcess();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff, sync_rsp_args.Response);
                    return(null);

                case TrinityMessageType.PRESERVED_SYNC_WITH_RSP:
                    SynReqRspArgs preserved_sync_rsp_args = new SynReqRspArgs(ByteArray,
                                                                              TrinityProtocol.TrinityMsgHeader,
                                                                              Length - TrinityProtocol.TrinityMsgHeader,
                                                                              preserved_sync_rsp_handlers[msgId]);
                    msgProcessResult = preserved_sync_rsp_args.MessageProcess();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff, preserved_sync_rsp_args.Response);
                    return(null);

                case TrinityMessageType.SYNC:
                    SynReqArgs sync_args = new SynReqArgs(ByteArray,
                                                          TrinityProtocol.TrinityMsgHeader,
                                                          Length - TrinityProtocol.TrinityMsgHeader,
                                                          sync_handlers[msgId]);
                    msgProcessResult = sync_args.MessageProcess();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff);
                    return(null);

                case TrinityMessageType.PRESERVED_SYNC:
                    SynReqArgs preserved_sync_args = new SynReqArgs(ByteArray,
                                                                    TrinityProtocol.TrinityMsgHeader,
                                                                    Length - TrinityProtocol.TrinityMsgHeader,
                                                                    preserved_sync_handlers[msgId]);
                    msgProcessResult = preserved_sync_args.MessageProcess();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff);
                    return(null);

                case TrinityMessageType.ASYNC:
                    AsynReqArgs async_args = new AsynReqArgs(ByteArray,
                                                             TrinityProtocol.TrinityMsgHeader,
                                                             Length - TrinityProtocol.TrinityMsgHeader,
                                                             async_handlers[msgId]);
                    msgProcessResult = async_args.AsyncProcessMessage();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff);
                    return(null);

                case TrinityMessageType.PRESERVED_ASYNC:
                    AsynReqArgs preserved_async_args = new AsynReqArgs(ByteArray,
                                                                       TrinityProtocol.TrinityMsgHeader,
                                                                       Length - TrinityProtocol.TrinityMsgHeader,
                                                                       preserved_async_handlers[msgId]);
                    msgProcessResult = preserved_async_args.AsyncProcessMessage();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff);
                    return(null);

                case TrinityMessageType.ASYNC_WITH_RSP:
                    AsynReqRspArgs async_rsp_args = new AsynReqRspArgs(ByteArray,
                                                                       TrinityProtocol.TrinityMsgHeader,
                                                                       Length - TrinityProtocol.TrinityMsgHeader,
                                                                       async_rsp_handlers[msgId]);
                    msgProcessResult = async_rsp_args.AsyncProcessMessage();
                    _SetSendRecvBuff(msgProcessResult, sendRecvBuff);
                    return(null);

                default:
                    throw new Exception("Not recognized message type.");
                }
            }
            catch (MessageTooLongException ex)
            {
                Log.WriteLine("Message Type: " + msgType);
                Log.WriteLine("Message SN: " + msgId);

                Log.WriteLine(ex.Message);
                Log.WriteLine(ex.StackTrace);
                _SetSendRecvBuff(TrinityErrorCode.E_MSG_OVERFLOW, sendRecvBuff);
                return(null);
            }
            catch (Exception ex)
            {
                Log.WriteLine("Message Type: " + msgType);
                Log.WriteLine("Message SN: " + msgId);

                Log.WriteLine(ex.Message);
                Log.WriteLine(ex.StackTrace);
                _SetSendRecvBuff(TrinityErrorCode.E_RPC_EXCEPTION, sendRecvBuff);
                return(null);
            }
        }