Пример #1
0
 private IExtensible BytesToMessage(ArraySegmentInput input, Type messageType, int messageSize)
 {
     if (messageSize > 0)
     {
         var messageBytes = new byte[messageSize];
         input.ReadBytes(messageBytes, 0, messageBytes.Length);
         object messageObject;
         using (var stream = new MemoryStream(messageBytes))
         {
             messageObject = Serializer.NonGeneric.Deserialize(messageType, stream);
         }
         return((IExtensible)messageObject);
     }
     else
     {
         object messageObject;
         using (var stream = new MemoryStream())
         {
             messageObject = Serializer.NonGeneric.Deserialize(messageType, stream);
         }
         return((IExtensible)messageObject);
     }
 }
Пример #2
0
        private void OnReceived(object sender, Bcp.BcpSession.ReceivedEventArgs e)
        {
            var input            = new ArraySegmentInput(e.Buffers);
            var messageId        = input.ReadInt();
            var messageType      = input.ReadByte();
            var messageNameSize  = input.ReadByte();
            var messageSize      = input.ReadInt();
            var messageNameBytes = new byte[messageNameSize];

            input.ReadBytes(messageNameBytes, 0, messageNameBytes.Length);
            var        messageName = Encoding.UTF8.GetString(messageNameBytes);
            var        packageName = messageName.Substring(0, messageName.LastIndexOf('.'));
            RpcService service;

            RpcService.IncomingEntry messageEntry;
            switch (messageType)
            {
            case BcpRpc.REQUEST:
            {
                if (IncomingServices.IncomingProxyMap.TryGetValue(packageName, out service))
                {
                    if (service.IncomingMessages.IncomingMessageMap.TryGetValue(messageName, out messageEntry))
                    {
                        var message = BytesToMessage(input, messageEntry.MessageType, messageSize);
                        try
                        {
                            var responseMessage = messageEntry.ExecuteRequest(message, this);
                            SendMessage(BcpRpc.SUCCESS, messageId, responseMessage);
                        }
                        catch (ErrorCode exception)
                        {
                            var errorMessage = exception.ProtobufMessage;
                            SendMessage(BcpRpc.FAIL, messageId, errorMessage);
                        }
                        catch (Exception exception)
                        {
                            LogExecuteFail(exception);
                        }
                    }
                    else
                    {
                        LogCantHandleMessage(messageName);
                    }
                }
                else
                {
                    LogNotService(packageName);
                }
                break;
            }

            case BcpRpc.PUSHMESSAGE:
            {
                if (IncomingServices.IncomingProxyMap.TryGetValue(packageName, out service))
                {
                    if (service.IncomingMessages.IncomingMessageMap.TryGetValue(messageName, out messageEntry))
                    {
                        try
                        {
                            var message = BytesToMessage(input, messageEntry.MessageType, messageSize);
                            messageEntry.ExecuteMessage(message, this);
                        }
                        catch (Exception exception)
                        {
                            LogExecuteFail(exception);
                        }
                    }
                    else
                    {
                        LogCantHandleMessage(messageName);
                    }
                }
                else
                {
                    LogNotService(packageName);
                }
                break;
            }

            case BcpRpc.SUCCESS:
            {
                IResponseHandler handler;
                if (outgoingProxy.outgoingRpcResponseHandlers.TryGetValue(messageId, out handler))
                {
                    try
                    {
                        outgoingProxy.outgoingRpcResponseHandlers.Remove(messageId);
                        var message = BytesToMessage(input, handler.ResponseType, messageSize);
                        handler.OnSuccess(message);
                    }
                    catch (Exception exception)
                    {
                        LogExecuteFail(exception);
                    }
                }
                else
                {
                    LogCantHandleMessage(messageName);
                }
                break;
            }

            case BcpRpc.FAIL:
            {
                IResponseHandler handler;
                if (outgoingProxy.outgoingRpcResponseHandlers.TryGetValue(messageId, out handler))
                {
                    outgoingProxy.outgoingRpcResponseHandlers.Remove(messageId);
                    Type errorType;
                    if (ErrorCodes.ErrorCodeMap.TryGetValue(messageName, out errorType))
                    {
                        try
                        {
                            var message = BytesToMessage(input, errorType, messageSize);
                            handler.OnFailure(message);
                        }
                        catch (Exception exception)
                        {
                            LogExecuteFail(exception);
                        }
                    }
                    else
                    {
                        LogCantHandleMessage(messageName);
                    }
                }
                else
                {
                    LogCantHandleMessage(messageName);
                }
                break;
            }

            default:
            {
                LogCantHandleMessage(messageName);
                break;
            }
            }
        }