| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Model
- {
- public sealed class Session : Entity
- {
- private static uint RpcId { get; set; }
- private readonly NetworkComponent network;
- private readonly Dictionary<uint, Action<object>> requestCallback = new Dictionary<uint, Action<object>>();
- private readonly AChannel channel;
- public Session(NetworkComponent network, AChannel channel)
- {
- this.network = network;
- this.channel = channel;
- this.StartRecv();
- }
- public string RemoteAddress
- {
- get
- {
- return this.channel.RemoteAddress;
- }
- }
- public ChannelType ChannelType
- {
- get
- {
- return this.channel.ChannelType;
- }
- }
- private async void StartRecv()
- {
- while (true)
- {
- if (this.Id == 0)
- {
- return;
- }
- byte[] messageBytes;
- try
- {
- messageBytes = await channel.Recv();
- if (this.Id == 0)
- {
- return;
- }
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- continue;
- }
- if (messageBytes.Length < 3)
- {
- continue;
- }
- ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
- opcode = NetworkHelper.NetworkToHostOrder(opcode);
- try
- {
- this.Run(opcode, messageBytes);
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- }
- private void Run(ushort opcode, byte[] messageBytes)
- {
- int offset = 0;
- byte flag = messageBytes[2];
- bool isCompressed = (flag & 0x80) > 0;
- const int opcodeAndFlagLength = 3;
- if (isCompressed) // 最高位为1,表示有压缩,需要解压缩
- {
- messageBytes = ZipHelper.Decompress(messageBytes, opcodeAndFlagLength, messageBytes.Length - opcodeAndFlagLength);
- offset = 0;
- }
- else
- {
- offset = opcodeAndFlagLength;
- }
- this.RunDecompressedBytes(opcode, messageBytes, offset);
- }
- private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
- {
- Type messageType = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetType(opcode);
- object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
- //Log.Debug($"recv: {JsonHelper.ToJson(message)}");
- AResponse response = message as AResponse;
- if (response != null)
- {
- // rpcFlag>0 表示这是一个rpc响应消息
- // Rpc回调有找不着的可能,因为client可能取消Rpc调用
- Action<object> action;
- if (!this.requestCallback.TryGetValue(response.RpcId, out action))
- {
- return;
- }
- this.requestCallback.Remove(response.RpcId);
- action(message);
- return;
- }
- this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
- }
- /// <summary>
- /// Rpc调用
- /// </summary>
- public Task<Response> Call<Response>(ARequest request, CancellationToken cancellationToken)
- where Response : AResponse
- {
- request.RpcId = ++RpcId;
- this.SendMessage(request);
- var tcs = new TaskCompletionSource<Response>();
- this.requestCallback[RpcId] = (message) =>
- {
- try
- {
- Response response = (Response)message;
- if (response.Error > 100)
- {
- tcs.SetException(new RpcException(response.Error, response.Message));
- return;
- }
- Log.Debug($"recv: {MongoHelper.ToJson(response)}");
- tcs.SetResult(response);
- }
- catch (Exception e)
- {
- tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
- }
- };
- cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
- return tcs.Task;
- }
- /// <summary>
- /// Rpc调用,发送一个消息,等待返回一个消息
- /// </summary>
- public Task<Response> Call<Response>(ARequest request) where Response : AResponse
- {
- request.RpcId = ++RpcId;
- this.SendMessage(request);
- var tcs = new TaskCompletionSource<Response>();
- this.requestCallback[RpcId] = (message) =>
- {
- try
- {
- Response response = (Response)message;
- if (response.Error > 100)
- {
- tcs.SetException(new RpcException(response.Error, response.Message));
- return;
- }
- Log.Debug($"recv: {MongoHelper.ToJson(response)}");
- tcs.SetResult(response);
- }
- catch (Exception e)
- {
- tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
- }
- };
- return tcs.Task;
- }
- public void Send(AMessage message)
- {
- if (this.Id == 0)
- {
- throw new Exception("session已经被Dispose了");
- }
- this.SendMessage(message);
- }
- public void Reply<Response>(Response message) where Response : AResponse
- {
- if (this.Id == 0)
- {
- throw new Exception("session已经被Dispose了");
- }
- this.SendMessage(message);
- }
- private void SendMessage(object message)
- {
- Log.Debug($"send: {MongoHelper.ToJson(message)}");
- ushort opcode = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
- opcode = NetworkHelper.HostToNetworkOrder(opcode);
- byte[] opcodeBytes = BitConverter.GetBytes(opcode);
- byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
- byte flag = 0;
- if (messageBytes.Length > 100)
- {
- byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
- if (newMessageBytes.Length < messageBytes.Length)
- {
- messageBytes = newMessageBytes;
- flag |= 0x80;
- }
- }
- byte[] flagBytes = { flag };
- channel.Send(new List<byte[]> { opcodeBytes, flagBytes, messageBytes });
- }
- public override void Dispose()
- {
- if (this.Id == 0)
- {
- return;
- }
- long id = this.Id;
- base.Dispose();
- this.channel.Dispose();
- this.network.Remove(id);
- }
- }
- }
|