Esempio n. 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);
        }
    }
Esempio n. 2
0
 public static void RegisterRespond(MFProtocolId id, MFProtocolAction p)
 {
     if (respondDic.ContainsKey(id))
     {
         MFLog.LogError("响应协议重复注册:", id);
         return;
     }
     respondDic.Add(id, p);
 }
Esempio n. 3
0
 public static void RegisterRequest(MFProtocolId id, MFProtocolAction actionClass)
 {
     if (requestDic.ContainsKey(id))
     {
         MFLog.LogError("请求协议重复注册:", id);
         return;
     }
     requestDic.Add(id, actionClass);
 }
Esempio n. 4
0
    public static void Receive(MFProtocolId id, string data)
    {
        MFProtocolAction actionClass;

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

        actionClass.Respond(data);
    }
Esempio n. 5
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);
    }
Esempio n. 6
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);
    }
Esempio n. 7
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);
     }
 }
Esempio n. 8
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);
    }
Esempio n. 9
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());
     }
 }
Esempio n. 10
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");
        }
    }
Esempio n. 11
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);
        }
    }
Esempio n. 12
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);
    }
Esempio n. 13
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));
    }
Esempio n. 14
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("选择失败");
             }
         }
     }
 }
Esempio n. 15
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);
    }