/************************************************* NetClient lua 中转 author:Daixiwei **************************************************/ using LuaInterface; using taurus.client; namespace taurus.unity { public class LuaNetClient { const string PARAM_MSG = "msg"; const string PARAM_CODE = "code"; LuaFunction _eventFunc; LuaFunction _connectFunc; LuaFunction _callbackFunc; LuaTable _obj; TaurusClient _netClient; public LuaNetClient(string host, string game, LuaTable obj, int protocol = (int)ConnectionProtocol.Tcp) { UnityEngine.Debug.Log("神奇的服务器地址:"+host+"=="+game+"=="+(ConnectionProtocol)protocol); if(VerCheck.Instance.isGFF==false&&VerCheck.Instance.isDXYGFF==false) { //int portT = LoadNetP(host); //if (isHttp) //{ // host = "http://" + "8.134.59.224" + ":" + portT + "/"; //} //else //{ // host = "8.134.59.224" + ":" + portT; //} _netClient = new TaurusClient(host, game, (ConnectionProtocol)protocol); _obj = obj; _netClient.addEventListener(NetClientEvent.OnEvent, __OnEvent); _netClient.addEventListener(NetClientEvent.Connect, __OnConnect); } else { int portT=LoadNetP(host); // UnityEngine.Debug.LogError("LuaNetClient port==>>>" + portT); if(portT!=0) { securityConnection connTem = LoadNet(portT); if (connTem.ip!=null) { if (isHttp) { host = "http://" + connTem.ip + ":" + connTem.port + "/"; } else { host = connTem.ip + ":" + connTem.port; } //UnityEngine.Debug.LogError("LuaNetClient host==>>>" + host); _netClient = new TaurusClient(host, game, (ConnectionProtocol)protocol); _obj = obj; _netClient.addEventListener(NetClientEvent.OnEvent, __OnEvent); _netClient.addEventListener(NetClientEvent.Connect, __OnConnect); } } } } /// /// 连接服务器 /// public void Connect() { _netClient.connect(); } public void Send(string cmd, string data, LuaFunction callback) { if (callback == null) { _netClient.sendText(cmd, data, null); return; } // UnityEngine.Debug.LogError(data); var msg = _netClient.sendText(cmd, data, __OnNetData); msg.data = callback; } /// /// 设置回调LUA函数 /// /// public void SetCallBackListener(LuaFunction func) { _callbackFunc = func; } /// /// 设置网络事件LUA函数 /// /// public void SetNetEventListener(LuaFunction func) { _eventFunc = func; } /// /// 设置网络连接状态LUA函数 /// /// public void SetNetConnectListener(LuaFunction func) { _connectFunc = func; } void __OnNetData(MessageResponse msg) { if (_callbackFunc == null) return; if (msg.messageData.data == null) return; LuaFunction p = msg.messageData.data as LuaFunction; _callbackFunc.BeginPCall(); _callbackFunc.Push(_obj); _callbackFunc.Push(msg.messageData.command); _callbackFunc.Push(msg.returnCode); _callbackFunc.Push(msg.messageData.textParam); _callbackFunc.Push(p); _callbackFunc.PCall(); if (_callbackFunc == null) return; _callbackFunc.EndPCall(); } void __OnEvent(Event evt) { if (_eventFunc == null) return; var msg = (Message)evt.getParameter(PARAM_MSG); _eventFunc.BeginPCall(); _eventFunc.Push(_obj); _eventFunc.Push(msg.command); _eventFunc.Push(msg.textParam); _eventFunc.PCall(); if (_eventFunc == null) return; _eventFunc.EndPCall(); } void __OnConnect(Event evt) { if (_connectFunc == null) return; SocketCode code = (SocketCode)evt.getParameter(PARAM_CODE); _connectFunc.BeginPCall(); _connectFunc.Push(_obj); _connectFunc.Push((int)code); _connectFunc.PCall(); if (_connectFunc == null) return; _connectFunc.EndPCall(); } public void ClearResponse() { if (_netClient == null) return; _netClient.clearResponse(); } public void Destroy() { NetManager.killConnection(_netClient.getId()); if (_callbackFunc != null) _callbackFunc.Dispose(); _callbackFunc = null; if (_connectFunc != null) _connectFunc.Dispose(); _connectFunc = null; if (_eventFunc != null) _eventFunc.Dispose(); _eventFunc = null; if (_obj != null) _obj.Dispose(); _obj = null; } /// /// 服务器Session字符 /// public string Session { get { return _netClient.getSession(); } set { _netClient.setSession(value); } } /// /// 延时 /// public int AveragePingTime { get { return _netClient.getAveragePingTime(); } } /// /// 网络是否已连接 /// public bool IsConnected { get { return _netClient.isConnected(); } } bool isHttp = false; private int LoadNetP(string host) { isHttp = false; if (!string.IsNullOrEmpty(host)) { string[] str = host.Split(':'); int len = str[str.Length-1].IndexOf("/"); string port = str[str.Length - 1]; if(len>0) { port = str[str.Length-1].Substring(0, len); } int curInt = System.Convert.ToInt32(port); if (str.Length > 2) isHttp = true; return curInt; } return 0; } securityConnection conn1; public securityConnection LoadNet(int port) { if (VerCheck.Instance.isGFF) { int rc1 = AppVest.getServerIPAndPort(ref conn1, "", port); if (rc1 != 0) { UnityEngine.Debug.LogError("getServerIPAndPort failed"); } } else { conn1.ip = "127.0.0.1"; conn1.port = port; } return conn1; } } }