예제 #1
0
        private unsafe void LoadCell_impl(SynReqRspArgs args)
        {
            /******************************
            * Protocol: LoadCell
            * Request: |8B CellId|
            * Response: [ 4B TrinityErrorCode header ] -- if success --> | 4B Size|2B CellType| Payload |
            ******************************/
            var id  = *(long *)(args.Buffer + args.Offset);
            var err = m_memorycloud.LoadCell(id, out var cellBuff, out var cellType);

            if (err == TrinityErrorCode.E_SUCCESS)
            {
                var len  = TrinityProtocol.MsgHeader + sizeof(int) + cellBuff.Length + sizeof(ushort);
                var buf  = (byte *)Memory.malloc((ulong)len);
                var sp   = PointerHelper.New(buf);
                *sp.ip++ = len - TrinityProtocol.SocketMsgHeader;
                *sp.ip++ = (int)err;
                *sp.ip++ = cellBuff.Length;
                *sp.sp++ = (short)cellType;
                fixed(byte *p = cellBuff)
                {
                    Memory.memcpy(sp.bp, p, (ulong)cellBuff.Length);
                }

                args.Response = new TrinityMessage(buf, len);
            }
            else
            {
                var buf  = (byte *)Memory.malloc(TrinityProtocol.MsgHeader);
                var sp   = PointerHelper.New(buf);
                *sp.ip++ = TrinityProtocol.MsgHeader - TrinityProtocol.SocketMsgHeader;
                *sp.ip   = (int)err;
                args.Response = new TrinityMessage(buf, TrinityProtocol.MsgHeader);
            }
        }
예제 #2
0
        private unsafe void RedirectMessageWithResponse_impl(SynReqRspArgs args)
        {
            /******************************
            * Protocol: RedirectMessage
            * Request: |4B PartitionId| TrinityMessage |
            * Response: | TrinityResponse |
            *
            * Redirects the message to another instance.
            ******************************/

            PointerHelper sp          = PointerHelper.New(args.Buffer + args.Offset);
            int           partitionId = *sp.ip++;

            TrinityMessage tm = new TrinityMessage(sp.bp, (*sp.ip) + TrinityProtocol.SocketMsgHeader);

            m_memorycloud[partitionId].SendMessage(tm, out var rsp);

            int   rsp_size = TrinityProtocol.MsgHeader + rsp.Size;
            byte *rsp_buf  = (byte *)Memory.malloc((ulong)rsp_size);

            *(int *)rsp_buf = rsp_size - TrinityProtocol.SocketMsgHeader;
            Memory.Copy(rsp.Buffer, rsp.Offset, rsp_buf, TrinityProtocol.MsgHeader, rsp.Size);
            rsp.Dispose();

            args.Response = new TrinityMessage(rsp_buf, rsp_size);
        }
예제 #3
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.");
            }
        }
예제 #4
0
        internal unsafe static void FailureNotificationMessageHandler(ref SynReqRspArgs args)
        {
            int leaderCandidate = -1;
            int failureServerId = -1;

            {
                int *p = (int *)(args.Buffer + args.Offset);
                leaderCandidate = *p++;
                failureServerId = *p;
            }

            #region Debug
#if DEBUG
            Log.WriteLine(LogLevel.Info, "Received failure notification message from {0} who claim {1} failed.", leaderCandidate, failureServerId);
#endif
            #endregion

            TrinityMessage response = new TrinityMessage(34);

            {
                *(int *)response.Buffer = 30;
                long *p        = (long *)(response.Buffer + TrinityMessage.Offset);
                var   workload = SystemStatus.Workloads;
                *     p++      = workload.AvailableMemoryBytes; // 8
                *     p++      = workload.CellCount;            // 8
                *     p++      = workload.SliceSize;            //8
                *(int *)p = workload.SliceCount;                //4
            }

            args.Response = response;
        }
예제 #5
0
        private unsafe void PollEvents_impl(SynReqRspArgs args)
        {
            /******************************
            * Protocol: PollEvents
            * Request: |4B InstanceId|4B Cookie|
            * Response:[4B E_RESULT Header] - |8B p| TrinityMessage |
            * Response.p != 0 if the response is a "request with response"
            * Response.TrinityMessage length header < 0 if there are no events
            * E_RESULT values:
            *
            *      0 = E_SUCCESS
            *      1 = E_NO_EVENTS
            *      2 = E_INVALID_CLIENT
            *
            * !NOTE Here, response itself is a TrinityMessage and Response.TrinityMessage
            * is the inner payload.
            ******************************/
            PointerHelper  sp         = PointerHelper.New(args.Buffer + args.Offset);
            int            instanceId = *sp.ip++;
            int            cookie     = *sp.ip++;
            byte *         outer_buf;
            int            outer_len;
            long           p  = 0;
            TrinityMessage tm = null;
            TaskCompletionSource <bool> tsrc = null;
            bool invalid_client = false;

            try
            {
                var stg = CheckInstanceCookie(cookie, instanceId);
                stg.Pulse     = DateTime.Now;
                (p, tm, tsrc) = stg.PollEvents_impl();
            }
            catch (ClientInstanceNotFoundException)
            {
                invalid_client = true;
            }

            if (tm == null)
            {
                outer_len         = TrinityProtocol.MsgHeader + sizeof(long) + sizeof(int);
                outer_buf         = (byte *)Memory.malloc((ulong)outer_len);
                *(int *)outer_buf = outer_len - TrinityProtocol.SocketMsgHeader;
                *(long *)(outer_buf + TrinityProtocol.MsgHeader) = 0;
                *(int *)(outer_buf + TrinityProtocol.MsgHeader + sizeof(long)) = -1;
                *(int *)(outer_buf + TrinityProtocol.SocketMsgHeader)          = invalid_client ? 2 : 1;
            }
            else
            {
                outer_len         = TrinityProtocol.MsgHeader + sizeof(long) + tm.Size;
                outer_buf         = (byte *)Memory.malloc((ulong)outer_len);
                *(int *)outer_buf = outer_len - TrinityProtocol.SocketMsgHeader;
                *(long *)(outer_buf + TrinityProtocol.MsgHeader) = p;
                Memory.memcpy(outer_buf + TrinityProtocol.MsgHeader + sizeof(long), tm.Buffer, (ulong)tm.Size);
                tsrc.SetResult(true);
                *(int *)(outer_buf + TrinityProtocol.SocketMsgHeader) = 0;
            }
            args.Response = new TrinityMessage(outer_buf, outer_len);
        }
        private unsafe void _HelloMessageHandler(SynReqRspArgs args)
        {
            var rsp = new HelloMessageReponseWriter();

            HelloMessageHandler(new HelloNessageRequestReader(args.Buffer, args.Offset), rsp);
            *(int *)(rsp.m_ptr - TrinityProtocol.MsgHeader) = rsp.Length + TrinityProtocol.TrinityMsgHeader;
            args.Response = new TrinityMessage(rsp.buffer, rsp.Length + TrinityProtocol.MsgHeader);
        }
예제 #7
0
        private unsafe void _QueryHandler(SynReqRspArgs args)
        {
            fixed(byte *p = &args.Buffer[args.Offset])
            {
                var responseBuff = new ResultWriter();

                QueryHandler(new NameRequestReader(p), responseBuff);
                *(int *)(responseBuff.CellPtr - TrinityProtocol.MsgHeader) = responseBuff.Length + TrinityProtocol.TrinityMsgHeader;
                args.Response = new TrinityMessage(responseBuff.buffer, responseBuff.Length + TrinityProtocol.MsgHeader);
            }
        }
예제 #8
0
        private unsafe void UpdateCell_impl(SynReqRspArgs args)
        {
            /******************************
            * Protocol: UpdateCell
            * Request: |8B CellId|4B Size| Payload |
            * Response: [ 4B TrinityErrorCode header ]
            ******************************/
            var sp   = PointerHelper.New(args.Buffer + args.Offset);
            var id   = *sp.lp++;
            var size = *sp.ip++;

            var err = (int)m_memorycloud.UpdateCell(id, sp.bp, size);
            var buf = (byte *)Memory.malloc(TrinityProtocol.MsgHeader);

            sp = PointerHelper.New(buf);
            *sp.ip++ = TrinityProtocol.MsgHeader - TrinityProtocol.SocketMsgHeader;
            *sp.ip   = err;
            args.Response = new TrinityMessage(buf, TrinityProtocol.MsgHeader);
        }
예제 #9
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);
            }
        }