Пример #1
0
    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);
        }
    }
Пример #2
0
 public void OnStartGameRespond(MFRespondHeader header, MFStartGameRespond data)
 {
     if (header.result == 0)
     {
         MFLog.LogInfo("房主开始游戏");
     }
 }
Пример #3
0
    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");
        }
    }
Пример #4
0
 public static void RegisterRequest(MFProtocolId id, MFProtocolAction actionClass)
 {
     if (requestDic.ContainsKey(id))
     {
         MFLog.LogError("请求协议重复注册:", id);
         return;
     }
     requestDic.Add(id, actionClass);
 }
Пример #5
0
 public static void RegisterRespond(MFProtocolId id, MFProtocolAction p)
 {
     if (respondDic.ContainsKey(id))
     {
         MFLog.LogError("响应协议重复注册:", id);
         return;
     }
     respondDic.Add(id, p);
 }
Пример #6
0
    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);
    }
Пример #7
0
    public static void Receive(MFProtocolId id, string data)
    {
        MFProtocolAction actionClass;

        if (!respondDic.TryGetValue(id, out actionClass))
        {
            MFLog.LogError("响应协议未注册:", id);
            return;
        }

        actionClass.Respond(data);
    }
Пример #8
0
    public void OnReadyToStartRespond(MFRespondHeader header, MFReadyToStartRespond data)
    {
        if (header.result == 0)
        {
            MFLog.LogInfo("玩家准备完成");
            RefreshPlayerList(data.userList);

            if (isRoomMaster() && AllPlayerReady())
            {
                uiBind.startBtn.enabled = true;
            }
        }
    }
Пример #9
0
 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);
     }
 }
Пример #10
0
    /// <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);
    }
Пример #11
0
 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());
     }
 }
Пример #12
0
    /// <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);
    }
Пример #13
0
    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");
        }
    }
Пример #14
0
    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);
        }
    }
Пример #15
0
    /// <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);
    }
Пример #16
0
    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));
    }
Пример #17
0
 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("选择失败");
             }
         }
     }
 }
Пример #18
0
    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;
            }
        }
    }
Пример #19
0
    /// <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);
    }
Пример #20
0
 // 预约房间
 private void OnReserverRoomBtnClick()
 {
     MFLog.LogInfo("OnReserverRoomBtnClick");
 }
Пример #21
0
 public MFSocketClient(string ip, int port)
 {
     MFLog.LogInfo("ip", ip);
     _ip   = ip;
     _port = port;
 }
Пример #22
0
 private static void OnHeartBeatRespond(string data)
 {
     MFLog.LogInfo("heartbeat");
 }