private void BtnWXLoginOnClick() { // AudioManager.Instance.PlaySound("button"); if (Application.platform == RuntimePlatform.WindowsEditor) { string acc = SecurityUtils.AESEncrypt("WX-o2xMF1vxkphDaUP88lkypIYwN14E", kv[0], kv[1]); string pwd = SecurityUtils.AESEncrypt("123458", kv[0], kv[1]); LoginOperation loginOperation = new LoginOperation(); LoginDto loginDto = loginOperation.regist(UnityEngine.Random.Range(1, 10000).ToString(), UnityEngine.Random.Range(1, 100000).ToString(), "123", "Default_" + UnityEngine.Random.Range(0, 7), "男", "123"); if (loginDto != null) { //SaveLoginInfo(); //登录成功 跳转场景 PlayerCache.loginInfo = loginDto; UIManager.Instance.Clear(); TcpSocketImpl.getInstance().init(); ClientTcpIoMessage ioMessage = new ClientTcpIoMessage(); ioMessage.methodName = ""; ioMessage.interfaceName = ""; List <object> list = new List <object>(2); list.Add(loginDto.uid); list.Add(loginDto.secretKey); ioMessage.args = JsonMapper.ToJson(list); TcpSocketImpl.getInstance().async(ioMessage, 1, -100); XUIUtils.LoadSceneX((int)SceneType.Hall); } } else { loginByWX.LoginByWX(); } }
// Update is called once per frame void Update() { ClientTcpIoMessage ioMessage = IoMessageHandler.getInstance().Pop(); if (ioMessage != null) { Handler(ioMessage); } }
//消息入队 public void push(ClientTcpIoMessage ioMessage) { lock (myLock) { messageQueue.Enqueue(ioMessage); if (messageQueue.Count > 20) { messageQueue.Dequeue(); } } }
void Handler(ClientTcpIoMessage ioMessage) { if (ioMessage.methodName.Equals("error")) { //异常处理 ErrorImpl error = new ErrorImpl(); List <string> list = JsonMapper.ToObject <List <string> >(ioMessage.args); int errorCode = int.Parse(list[0]); string[] str = null; if (list.Count > 1) { if (!list[1].Equals("null")) { str = list[1].Split(','); } } error.err(errorCode, str); } else { if (TcpManager.Instance.dict.ContainsKey(ioMessage.interfaceName)) { if (TcpManager.Instance.dict[ioMessage.interfaceName].ParamInfo.ContainsKey(ioMessage.methodName)) { ILRuntimeParameterInfo[] paramInfo = TcpManager.Instance.dict[ioMessage.interfaceName].ParamInfo[ioMessage.methodName]; object[] args = null; if (paramInfo.Length > 0) { args = new object[paramInfo.Length]; //List<string> list = JsonConvert.DeserializeObject<List<string>>(ioMessage.args); List <string> list = JsonMapper.ToObject <List <string> >(ioMessage.args); if (list.Count != paramInfo.Length) { //错误参数 throw new NullReferenceException("错误参数"); } for (int i = 0; i < paramInfo.Length; i++) { // args[i] = JsonConvert.DeserializeObject(list[i], paramInfo[i].ParameterType); args[i] = JsonMapper.DeserializeObject(list[i], paramInfo[i].ParameterType); } } if (TcpManager.Instance.dict[ioMessage.interfaceName].MetnodInfo.ContainsKey(ioMessage.methodName)) { TcpManager.Instance.dict[ioMessage.interfaceName].MetnodInfo[ioMessage.methodName].Invoke(TcpManager.Instance.dict[ioMessage.interfaceName].ObjInstance, args); } // ILMgr.Instance.appDomain.Invoke(IoMessageHandler.getInstance().dict[ioMessage.interfaceName].ObjName, ioMessage.methodName, IoMessageHandler.getInstance().dict[ioMessage.interfaceName].ObjInstance, args); } } } }
private void onData() { //消息长度小于数据基础长度说明包没完整 if (ioBuffer.Length < BASE_LENGTH) { isRead = false; return; } //读取定义的消息长度 while (true) { int datazie = ioBuffer.ReadInt(); if (datazie == -777888) { break; } } short cmd = ioBuffer.ReadShort(); short gameId = ioBuffer.ReadShort(); short srcGameId = ioBuffer.ReadShort(); int length = ioBuffer.ReadInt(); if (ioBuffer.Length < length + BASE_LENGTH) { //还原指针 ioBuffer.Postion = 0; isRead = false; //数据不全 还原指针等待下一数据包(分包) return; } ByteArray ioData = new ByteArray(); ioData.WriteBytes(ioBuffer.Buffer, BASE_LENGTH, length); ioBuffer.Postion += length; byte[] buf = new byte[length]; buf = ioData.ReadBytes(); ClientTcpIoMessage clientTcpIoMessage = ProtostuffUtils.ProtobufDeserialize <ClientTcpIoMessage>(buf); Debug.Log("收到:" + clientTcpIoMessage.interfaceName + "::" + clientTcpIoMessage.methodName + "====" + clientTcpIoMessage.args); IoMessageHandler.getInstance().push(clientTcpIoMessage); ByteArray bytes = new ByteArray(); bytes.WriteBytes(ioBuffer.Buffer, ioBuffer.Postion, ioBuffer.Length - ioBuffer.Postion); ioBuffer = bytes; onData(); }
public static void CreateTcpProxy(Type obj, string methodName, List <string> args, short gameId) { //var clazzType = (Proxy)Attribute.GetCustomAttribute(obj, typeof(Proxy)); //if (clazzType == null) //{ // throw new NullReferenceException(""); //} // short gameId = clazzType.gameId; ClientTcpIoMessage client = new ClientTcpIoMessage(); client.interfaceName = obj.FullName; client.methodName = methodName; // client.args = JsonConvert.SerializeObject(callMessage.Args); if (args != null) { client.args = JsonMapper.ToJson(args); } else { client.args = null; } TcpSocketImpl.getInstance().async(client, gameId, -10); }
public void async(ClientTcpIoMessage ioMessage, short gameId, short cmd) { Debug.Log("发送" + ioMessage.interfaceName + "::" + ioMessage.methodName + "====" + ioMessage.args); byte[] buf = ProtostuffUtils.ProtobufSerialize(ioMessage); ByteArray arr = new ByteArray(); arr.WriteInt(-777888); arr.WriteShort(cmd); arr.WriteShort(gameId); arr.WriteShort(-100); arr.WriteInt(buf.Length); arr.WriteBytes(buf); try { socket.Send(arr.Buffer); } catch (Exception e) { this.close(); Debug.Log("网络错误"); Debug.LogException(e); } }