Beispiel #1
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;
            }
        }
Beispiel #2
0
        /// <inheritdoc/>
        public void SendMessage(byte *message, int size)
        {
            TrinityMessageType msgType = *(TrinityMessageType *)(message + TrinityProtocol.MsgTypeOffset);
            ushort             msgId   = *(ushort *)(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;

            case TrinityMessageType.ASYNC_WITH_RSP:
            {
                AsynReqRspArgs async_rsp_args = new AsynReqRspArgs(message,
                                                                   TrinityProtocol.MsgHeader,
                                                                   size - TrinityProtocol.MsgHeader,
                                                                   MessageHandlers.DefaultParser.async_rsp_handlers[msgId]);
                msgProcessResult = async_rsp_args.AsyncProcessMessage();
            }
            break;

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

            if (msgProcessResult == TrinityErrorCode.E_RPC_EXCEPTION)
            {
                throw new IOException("Local message handler throws an exception.");
            }
        }
Beispiel #3
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);
            }
        }