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(); } }
public override void OnReceived(ITcpSession session, IDynamicBuffer dataBuffer) { // var result = new byte[dataBuffer.DataSize]; //Buffer.BlockCopy(dataBuffer.Buffer, 0, result, 0, dataBuffer.DataSize); //session.SessionData.Set("islogin", true);//设置登录状态 //var txt= Encoding.UTF8.GetString(result); session.SendAsync(data); }
public override void OnReceived(ITcpSession session, IDynamicBuffer dataBuffer) { var result = new byte[dataBuffer.DataSize]; Buffer.BlockCopy(dataBuffer.Buffer, 0, result, 0, dataBuffer.DataSize); //var txt= Encoding.UTF8.GetString(result); session.SendAsync(data); }
public override void OnReceived(ITcpSession session, IDynamicBuffer dataBuffer) { //Stream str = new MemoryStream(); // var result = new byte[dataBuffer.DataSize]; //Buffer.BlockCopy(dataBuffer.Buffer, 0, result, 0, dataBuffer.DataSize); //session.SessionData.Set("islogin", true);//设置登录状态 //var txt= Encoding.UTF8.GetString(result); session.SendAsync(data); }
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); }
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(); } }