public override void async(IoMessage ioMessage) { byte[] buf = ProtostuffUtils.ProtobufSerialize(ioMessage); ByteArray arr = new ByteArray(); arr.WriteInt(-777888);//包头 arr.WriteShort(dataSrc); arr.WriteShort((short)buf.Length); arr.WriteBytes(buf); try { socket.Send(arr.Buffer); } catch (Exception) { this.close(); Debug.Log("网络错误"); } }
/**都是基础类型的参数*/ private void baseIoMessageHandler(object obj, Type t, IoMessage ioMessage) { try { MethodInfo method = t.GetMethod(ioMessage.getMethodName()); ParameterInfo[] parameterInfo = method.GetParameters(); object[] args = null; if (parameterInfo.Length > 0) { string[] paramxx = ((string)ioMessage.getData()).Split('$');//先分割$字符串 for (int i = 0; i < parameterInfo.Length; i++) { args[i] = TypeExchange.exchange(parameterInfo[i].GetType(), paramxx[i]); } } method.Invoke(obj, args); } catch (Exception e) { Debug.LogError("反射异常"); } }
private object request(IoMessage ioMessage) { if (typeof(IHttpAsyncService).IsAssignableFrom(type)) { SocketFactory.getInstance().httpAsyncRequest(ioMessage); return(null); } else if (typeof(IHttpSyncService).IsAssignableFrom(type)) { return(SocketFactory.getInstance().httpSyncRequest(ioMessage)); } else if (typeof(ITcpAsyncService).IsAssignableFrom(type)) { SocketFactory.getInstance().tcpAsyncRequest(ioMessage); return(null); } else if (typeof(ITcpSyncService).IsAssignableFrom(type)) { return(SocketFactory.getInstance().tcpSyncRequest(ioMessage)); } return(null); }
public override object sync(IoMessage ioMessage) { throw new NotImplementedException(); }
public override void async(IoMessage ioMessage) { throw new System.NotImplementedException(); }
public override object sync(IoMessage ioMessage) { var request = new UnityWebRequest("http://127.0.0.1:8888/game/handle", "POST"); byte[] buf = ProtostuffUtils.ProtobufSerialize(ioMessage); ByteArray arr = new ByteArray(); arr.WriteInt(-777888);//包头 arr.WriteShort(1); arr.WriteShort((short)buf.Length); arr.WriteBytes(buf); request.uploadHandler = new UploadHandlerRaw(arr.Buffer); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/octet-stream"); request.SendWebRequest(); Debug.Log("发送完成"); if (request.responseCode == 200) { ByteArray byteArray = new ByteArray(); byteArray.WriteBytes(request.downloadHandler.data); int head = byteArray.ReadInt(); if (head == -777888) { short dataSrcRespone = byteArray.ReadShort(); //数据类型 1,全是基础数据类型 2,PB类型 short resultCode = byteArray.ReadShort(); //成功失败码 short length = byteArray.ReadShort(); byte[] result = byteArray.ReadBytes(); if (resultCode == 404) { IoMessageBaseTypeImpl errorMessage = ProtostuffUtils.ProtobufDeserialize <IoMessageBaseTypeImpl>(result); string errMsg = (string)errorMessage.getData(); IError error = new ErrorImpl(); string[] str = errMsg.Split('$'); string[] msg = new string[] { "" }; if (str.Length <= 0) { error.err(202, msg); throw new NotImplementedException("远程调用异常"); } else if (str.Length > 1) { msg = new string[str.Length - 1]; for (int i = 0; i < str.Length; i++) { msg[i] = str[i + 1]; } } error.err(short.Parse(str[0]), msg); throw new NotImplementedException("远程调用异常"); } switch (dataSrcRespone) { case (short)DataSrcEnum.BaseType: Debug.Log("马上返回"); return(ProtostuffUtils.ProtobufDeserialize <IoMessageBaseTypeImpl>(result)); case (short)DataSrcEnum.PBType: return(ProtostuffUtils.ProtobufDeserialize <IoMessagePBTypeImpl>(result)); } } } return(null); }
//消息入队 public void push(IoMessage ioMessage) { messageQueue.Enqueue(ioMessage); }