SessionWrap.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Threading.Tasks;
  3. using Model;
  4. namespace Hotfix
  5. {
  6. public class SessionWrap
  7. {
  8. public Session session;
  9. public void Send(IMessage message)
  10. {
  11. #if !ILRuntime
  12. ushort opcode = Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
  13. byte[] bytes = ProtobufHelper.ToBytes(message);
  14. this.session.Send(opcode, bytes);
  15. #else
  16. this.session.Send(message);
  17. #endif
  18. }
  19. public async Task<IResponse> Call(IRequest request)
  20. {
  21. #if !ILRuntime
  22. byte[] bytes = ProtobufHelper.ToBytes(request);
  23. ushort opcode = Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(request.GetType());
  24. PacketInfo packetInfo = await this.session.Call(opcode, bytes);
  25. ushort responseOpcode = BitConverter.ToUInt16(packetInfo.Bytes, 0);
  26. Type t = GetType(responseOpcode);
  27. IResponse response = (IResponse)ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
  28. return response;
  29. #else
  30. AResponse response = await this.session.Call(request);
  31. return response;
  32. #endif
  33. }
  34. public static Type GetType(ushort opcode)
  35. {
  36. return null;
  37. }
  38. }
  39. }