private void ReceiveCallBack(IAsyncResult ar) { if (_socket == null || !_socket.Connected) { return; } try { byte[] data = (byte[])ar.AsyncState; int length = _socket.EndReceive(ar); if (length > 0) { string str = Encoding.UTF8.GetString(data, 4, data.Length - 4); SubRecvData(ref str, length); MFNetManager.GetInstance().PushRecvData(str); // go on Receive(); } else { //todo 没有接收到数据 } } catch (Exception e) { //todo 加上异常处理 MFLog.LogError(e.ToString()); //uninit(); //SFUtils.logWarning("网络连接中断:" + e.Message); //dispatcher.dispatchEvent(SFEvent.EVENT_NETWORK_INTERRUPTED); } }
public void OnStartGameRespond(MFRespondHeader header, MFStartGameRespond data) { if (header.result == 0) { MFLog.LogInfo("房主开始游戏"); } }
private void Awake() { MFUIMgr.Init(); // 服务器做成一个可以选择的列表 #if UNITY_EDITOR if (!debug) { _netMgr.Init(new MFSocketClient("116.196.109.146", 8090)); } #else if (debug) { _netMgr.Init(new MFSocketClient("10.0.2.2", 8090)); } else { _netMgr.Init(new MFSocketClient("116.196.109.146", 8090)); } MFAgoraMgr.Init(); ssdk = GetComponent <ShareSDK>(); #endif MFApplicationUtil.SetDebugMode(debug); MFServerAgentBase.Init(); DontDestroyOnLoad(gameObject); if (debug) { MFLog.LogInfo("Debug Mode Start"); } }
public static void RegisterRequest(MFProtocolId id, MFProtocolAction actionClass) { if (requestDic.ContainsKey(id)) { MFLog.LogError("请求协议重复注册:", id); return; } requestDic.Add(id, actionClass); }
public static void RegisterRespond(MFProtocolId id, MFProtocolAction p) { if (respondDic.ContainsKey(id)) { MFLog.LogError("响应协议重复注册:", id); return; } respondDic.Add(id, p); }
public static void Send(MFProtocolId id, params object[] args) { MFProtocolAction actionClass; if (!requestDic.TryGetValue(id, out actionClass)) { MFLog.LogError("请求协议未注册:", id); return; } actionClass.Request(id, args); }
public static void Receive(MFProtocolId id, string data) { MFProtocolAction actionClass; if (!respondDic.TryGetValue(id, out actionClass)) { MFLog.LogError("响应协议未注册:", id); return; } actionClass.Respond(data); }
public void OnReadyToStartRespond(MFRespondHeader header, MFReadyToStartRespond data) { if (header.result == 0) { MFLog.LogInfo("玩家准备完成"); RefreshPlayerList(data.userList); if (isRoomMaster() && AllPlayerReady()) { uiBind.startBtn.enabled = true; } } }
private void ConnectCallBack(IAsyncResult ar) { try { _socket.EndConnect(ar); MFLog.LogInfo("Connect Success"); Receive(); } catch (Exception e) { MFLog.LogError("Connect Faild"); MFLog.LogError(e.ToString()); //todo 加上异常处理 //m_stateCallback((int)ESocketState.eST_Error); } }
/// <summary> /// 打开UI UI还存活的直接显示,否则创建新的UI。 /// action会在OnShow之前调用 /// </summary> public static void Open<T>(Action<T> action = null) where T : MFUIBase { Type uiScript = typeof(T); if (!IsBind<T>()) { MFLog.LogError("UI脚本没有绑定Prefab"); return; } if (IsAlive<T>()) { Show<T>(action); return; } CreateNewUI<T>(action); }
public void Send(string msg) { if (_socket == null || !_socket.Connected) { return; } try { byte[] data = Encoding.UTF8.GetBytes(msg); _socket.BeginSend(data, 0, data.Length, SocketFlags.None, result => { _socket.EndSend(result); }, null); } catch (Exception e) { MFLog.LogError(e.ToString()); } }
/// <summary> /// 将UI脚本组件与Prefab绑定 /// </summary> public static void BindPrefab<T>(string prefabPath, UILayer layer, UIInstanceType instType) where T : MFUIBase { Type uiScript = typeof(T); GameObject uiPrefab = MFResoureUtil.LoadPrefabFromPath(prefabPath); Assert.IsNotNull(uiPrefab); UIBindInfo info = new UIBindInfo { prefab = uiPrefab, layer = layer, instType = instType, }; if (_uiInfobDic.ContainsKey(uiScript)) { MFLog.LogError("UI脚本重复绑定!!!"); } _uiInfobDic.Add(uiScript, info); }
public void Connect() { try { IPAddress ipAddr; if (!IPAddress.TryParse(_ip, out ipAddr)) { MFLog.LogError("Parse IPAdderss Error"); return; } _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.BeginConnect(ipAddr, _port, ConnectCallBack, null); }catch (Exception e) { MFLog.LogError(e.ToString()); MFLog.LogError("Socket Connect Failed"); } }
private void DispatchRespond(string data) { // 随便找个int当做具体类型 反正只需要header部分 try { MFLog.LogInfo(data); MFRespondHeader rh = MFJsonSerialzator.DeSerialize <MFRespondProtocol <int> >(data).header; if (rh.result != 0) { MFLog.LogError(rh.result, rh.errMsg); } MFServerAgentBase.Receive(rh.protocolId, data); } catch (Exception e) { MFLog.LogError(e.ToString()); MFLog.LogError(data); } }
/// <summary> /// 通过路径查找GameObject或Component中子节点(GameObject) /// </summary> public static GameObject Find(Object target, string path) { GameObject targetGo = null; Transform transform = GetTransform(target); if (transform) { if (transform = transform.Find(path)) { targetGo = transform.gameObject; } } if (!target) { MFLog.LogError(string.Format("GameObject[path={0}] is not found!", path)); } return(targetGo); }
private const string PRE_PATH = "Assets/Config/"; // tab文件前置路径 public static List <T> LoadTabFile <T>(string path, string primaryKey = null) { path = string.Format("{0}/{1}", PRE_PATH, path); if (!File.Exists(path)) { MFLog.LogError("File {0} Not Found" + path); return(null); } List <string> fileDataList = LoadFileData(path); int index = 0; string[] fields = ReadFileField(ref index, fileDataList); if (!CheckFileField <T>(fields)) { MFLog.LogError("File {0} Header Struct Error" + path); return(null); } return(ReadFileContent <T>(index, fields, fileDataList)); }
public void OnSelectCharacterRespond(MFRespondHeader header, MFSelectCharacterRespond data) { if (header.result == 0) { if (header.broadcast) { // 更新角色列表状态 RefreshCharacterInfoList(data.roleList); } else { if (data.result) { RefreshCharacterInfoList(data.roleList); } else { MFLog.LogError("选择失败"); } } } }
public void OnSendCharacterScriptRespond(MFRespondHeader header, MFSendCharacterScriptRespond data) { if (header.result == 0) { MFLog.LogInfo("发放成功"); MFLog.LogInfo(data.script); if (isRoomMaster()) { uiBind.sendScriptBtn.gameObject.SetActive(false); uiBind.sendScriptBtn.enabled = false; uiBind.startBtn.gameObject.SetActive(true); uiBind.startBtn.enabled = false; } else { uiBind.readyBtn.gameObject.SetActive(true); uiBind.readyBtn.enabled = true; } } }
/// <summary> /// 通过路径查找GameObject或Component中子节点的Component /// </summary> public static T Find <T>(Object target, string path) where T : Component { T targetT = null; Transform transform = GetTransform(target); if (transform) { if (transform = transform.Find(path)) { targetT = transform.GetComponent <T>(); if (targetT == null) { targetT = transform.gameObject.AddComponent <T>(); } } } if (!targetT) { MFLog.LogError(string.Format("{0}[path={1}] is not found!", typeof(T), path)); } return(targetT); }
// 预约房间 private void OnReserverRoomBtnClick() { MFLog.LogInfo("OnReserverRoomBtnClick"); }
public MFSocketClient(string ip, int port) { MFLog.LogInfo("ip", ip); _ip = ip; _port = port; }
private static void OnHeartBeatRespond(string data) { MFLog.LogInfo("heartbeat"); }