Example #1
0
        public override void OnReceived(ITcpSession session, IDynamicBuffer dataBuffer)
        {
            int          cmd = dataBuffer.Buffer[0];
            MemoryStream ms  = new MemoryStream(dataBuffer.Buffer, 1, dataBuffer.DataSize - 1);

            switch (cmd)
            {
            case 1:
            {
                RpcCallData data = Serializer.Deserialize <RpcCallData>(ms);
                ThreadPool.QueueUserWorkItem(CallFunc, new CallStatus()
                    {
                        Session = session, Data = data
                    });
            }
            break;

            case 2:
            {
                var data = Serializer.Deserialize <RpcReturnData>(ms);
                RpcFactory.GetInvoke(session.SessionId).ReturnData(data);
            }
            break;

            default:
            {
                var data = Serializer.Deserialize <RpcErrorInfo>(ms);
                RpcFactory.GetInvoke(session.SessionId).ReturnError(data);
            }
            break;
            }
            ms.Dispose();
        }
Example #2
0
        protected void CallProcess(ITcpSession session, RpcCallData data)
        {
            IServerController controller = rpcContainer.GetController(session.SessionId, data.Controller.ToLower());

            if (controller.Session == null)
            {
                controller.Session = session;
            }
            try
            {
                string   key    = (data.Controller + ":" + data.Action).ToLower();
                var      method = rpcContainer.GetMethod(key);
                object[] args   = null;
                if (data.Arguments != null && data.Arguments.Count > 0)
                {
                    args = new object[data.Arguments.Count];
                    var types = GetParaTypeList(key);
                    for (int i = 0; i < data.Arguments.Count; i++)
                    {
                        var          arg    = data.Arguments[i];
                        MemoryStream stream = new MemoryStream(arg, 0, arg.Length);
                        var          obj    = Serializer.NonGeneric.Deserialize(types[i], stream);
                        args[i] = obj;
                        stream.Dispose();
                    }
                }
                object        value  = method.Invoke(controller, args);
                RpcReturnData result = new RpcReturnData()
                {
                    Id = data.Id
                };
                var ms = new MemoryStream();
                Serializer.Serialize(ms, value);
                byte[] bytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, bytes, 0, bytes.Length);
                result.Value = bytes;
                ms.Position  = 0;
                ms.WriteByte(2);
                Serializer.Serialize(ms, result);
                byte[] rBytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
                session.SendAsync(rBytes);
                ms.Dispose();
            }
            catch (Exception e)
            {
                RpcErrorInfo error = new RpcErrorInfo()
                {
                    Id = data.Id, Message = e.Message
                };
                var ms = new MemoryStream();
                ms.WriteByte(0);
                Serializer.Serialize(ms, error);
                byte[] rBytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
                session.SendAsync(rBytes);
                ms.Dispose();
            }
        }
Example #3
0
    public RpcCallReceiverProcessor New(string name, string data, NetworkPlayer player, NetworkMessageInfo info)
    {
        string[]            dataParts    = RpcCallData.GetDataParts(data);
        RpcCallReceivedData receivedData = new RpcCallReceivedData(name, dataParts, player, info);

        logRpcReceived(receivedData);

        return(buildProcessor(name, receivedData));
    }
Example #4
0
        private async Task <byte[]> Call(string controller, string action, params object[] arguments)
        {
            int id      = idGenerator.NewId();
            var tSource = new TaskCompletionSource <byte[]>();

            if (!taskDict.TryAdd(id, tSource))
            {
                id = idGenerator.NewId();
                if (!taskDict.TryAdd(id, tSource))
                {
                    while (true)
                    {
                        id = idGenerator.NewId();
                        if (taskDict.TryAdd(id, tSource))
                        {
                            break;
                        }
                        await Task.Delay(100);
                    }
                }
            }
            RpcCallData transData = new RpcCallData()
            {
                Id = id, Controller = controller, Action = action, Arguments = new List <byte[]>()
            };
            MemoryStream ams = new MemoryStream();

            foreach (var arg in arguments)
            {
                Serializer.Serialize(ams, arg);
                byte[] argBytes = new byte[ams.Position];
                Buffer.BlockCopy(ams.GetBuffer(), 0, argBytes, 0, argBytes.Length);
                transData.Arguments.Add(argBytes);
                ams.Position = 0;
            }
            ams.Position = 0;
            ams.WriteByte(1);
            Serializer.Serialize(ams, transData);
            byte[] bytes = new byte[ams.Position];
            Buffer.BlockCopy(ams.GetBuffer(), 0, bytes, 0, bytes.Length);
            Session.SendAsync(bytes);
            ams.Dispose();
            var cancelsource = new CancellationTokenSource(invokeTimeOut);

            tSource.Task.Wait(cancelsource.Token);
            return(await tSource.Task);
        }
Example #5
0
        public override void OnReceived(ITcpClientSession session, IDynamicBuffer dataBuffer)
        {
            int          cmd = dataBuffer.Buffer[0];
            MemoryStream ms  = new MemoryStream(dataBuffer.Buffer, 1, dataBuffer.DataSize - 1);

            switch (cmd)
            {
            case 1:
            {
                RpcCallData data = Serializer.Deserialize <RpcCallData>(ms);
                ThreadPool.QueueUserWorkItem(CallFunc, data);
            }
            break;

            case 2:
            {
                var data = Serializer.Deserialize <RpcReturnData>(ms);
                TaskCompletionSource <byte[]> tSource;
                if (taskDict.TryRemove(data.Id, out tSource))
                {
                    tSource.SetResult(data.Value);
                }
            }
            break;

            default:
            {
                var info = Serializer.Deserialize <RpcErrorInfo>(ms);
                TaskCompletionSource <byte[]> tSource;
                if (taskDict.TryRemove(info.Id, out tSource))
                {
                    tSource.SetException(new Exception(info.Message));
                }
            }
            break;
            }
            ms.Dispose();
        }
Example #6
0
 public static string getDebugName(RpcCallData rpc)
 {
     return rpc.Name + "@" + rpc.Sender + " (" + string.Join(DATA_SEPARATOR, rpc.Data) + ")";
 }
Example #7
0
 protected void CallProcess(ITcpSession session, RpcCallData data)
 {
     IServerController controller = rpcContainer.GetController(session.SessionId,data.Controller.ToLower());
     if (controller.Session==null)
     {
         controller.Session =session;
     }
     try
     {
         string key = (data.Controller + ":" + data.Action).ToLower();
         var method = rpcContainer.GetMethod(key);
         object[] args = null;
         if (data.Arguments != null && data.Arguments.Count > 0)
         {
             args = new object[data.Arguments.Count];
             var types = GetParaTypeList(key);
             for (int i = 0; i < data.Arguments.Count; i++)
             {
                 var arg = data.Arguments[i];
                 MemoryStream stream = new MemoryStream(arg, 0, arg.Length);
                 var obj = Serializer.NonGeneric.Deserialize(types[i], stream);
                 args[i] = obj;
                 stream.Dispose();
             }
         }
         object value = method.Invoke(controller, args);
         RpcReturnData result = new RpcReturnData() { Id = data.Id };
         var ms = new MemoryStream();
         Serializer.Serialize(ms, value);
         byte[] bytes = new byte[ms.Position];
         Buffer.BlockCopy(ms.GetBuffer(), 0, bytes, 0, bytes.Length);
         result.Value = bytes;
         ms.Position = 0;
         ms.WriteByte(2);
         Serializer.Serialize(ms, result);
         byte[] rBytes = new byte[ms.Position];
         Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
         session.SendAsync(rBytes);
         ms.Dispose();
     }
     catch (Exception e)
     {
         RpcErrorInfo error = new RpcErrorInfo() { Id = data.Id, Message = e.Message };
         var ms = new MemoryStream();
         ms.WriteByte(0);
         Serializer.Serialize(ms, error);
         byte[] rBytes = new byte[ms.Position];
         Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
         session.SendAsync(rBytes);
         ms.Dispose();
     }
 }
Example #8
0
 public static string getDebugName(RpcCallData rpc)
 {
     return(rpc.Name + "@" + rpc.Sender + " (" + string.Join(DATA_SEPARATOR, rpc.Data) + ")");
 }