private void ReceiveDataCallback(SocketErrorData errData, SocketReceiveData recvData)
        {
            try
            {
                // error
                if (errData != null)
                {
                    if (errData.ErrorCode != ClientErrorCode.None)
                    {
                        this.textBoxTCPTips.AppendText($"{errData.ErrorStr}\r\n");
                    }
                    if (errData.ErrorCode == ClientErrorCode.ConnectErrorType)
                    {
                        // remote host was closed
                        CloseTcpClient(ref this.clientTcp);
                    }
                    return;
                }
                ConnectStatus(true);
                // 解析接收数据
                if (recvData.Length > 0)
                {
                    string data = Encoding.UTF8.GetString(recvData.RecvBuffer, 0, recvData.Length);
                    this.textBoxTCPRecv.AppendText($"RecvData: {data}\r\n");

                    Assembly m_ass = Assembly.GetExecutingAssembly();

                    Object o = m_ass.CreateInstance("SoftLiu_VSMainMenuTools.SocketClient.SocketData.ProtocolData.ActionData",
                                                    false, System.Reflection.BindingFlags.Default,
                                                    null, new object[] { recvData }, null, null);
                    if (o != null && o is ActionData)
                    {
                        Console.WriteLine("Activator Create Instance success.");
                        ActionData actionData = o as ActionData;
                        actionData.Init();
                    }
                    else
                    {
                        Console.WriteLine("Activator Create Instance failed.");
                    }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine($"ReceiveDataCallback Error::{error.Message}");
            }
        }
        private void RecvData(byte[] receiveData)
        {
            disconnected = false;

            string recvStr = Encoding.UTF8.GetString(receiveData);

            recvStr = recvStr.TrimEnd('\0');
            if (string.IsNullOrEmpty(recvStr))
            {
                return;
            }
            textBoxReceive.AppendText($"{recvStr}\r\n");

            Dictionary <string, object> dataRecvDic = JsonUtils.Instance.JsonToDictionary(recvStr) as Dictionary <string, object>;

            if (dataRecvDic != null && dataRecvDic.ContainsKey("action"))
            {
                if (dataRecvDic != null && dataRecvDic.ContainsKey("action"))
                {
                    string action = dataRecvDic["action"].ToString();

                    IEnumerable <WebSocketProtocolData> protocols = WebSocketManager.Instance.ProtocolDatas.Where(data => { return(data.Protocol == action); });
                    WebSocketProtocolData protocol = protocols.FirstOrDefault();
                    if (protocols != null)
                    {
                        // 获取当前程序集
                        Assembly assembly = Assembly.GetExecutingAssembly();
                        //dynamic obj = assembly.CreateInstance("类的完全限定名(即包括命名空间)");
                        dynamic obj = assembly.CreateInstance($"SoftLiu_VSMainMenuTools.SocketClient.WebSocketData.Data.{protocol.Type}");
                        if (obj is ActionData)
                        {
                            ActionData data = obj as ActionData;
                            data.Init(recvStr);
                        }
                        else
                        {
                            Console.WriteLine($"WebSocketProtocolData CreateInstance is null, action: {action}, type: {protocol.Type}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"WebSocketProtocolData is null, action: {action}");
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void ActionHandOut(Socket client, string recvData)
        {
            Dictionary <string, object> dataRecvDic = JsonUtils.Instance.JsonToObject <Dictionary <string, object> >(recvData);

            if (dataRecvDic != null && dataRecvDic.ContainsKey("action"))
            {
                if (dataRecvDic != null && dataRecvDic.ContainsKey("action"))
                {
                    string action = dataRecvDic["action"].ToString();

                    IEnumerable <SocketProtocolData> protocols = SocketManager.Instance.ProtocolDatas.Where(data => { return(data.Protocol == action); });
                    SocketProtocolData protocol = protocols.FirstOrDefault();
                    if (protocols != null)
                    {
                        //dynamic obj = assembly.CreateInstance("类的完全限定名(即包括命名空间)");

                        // 传参数
                        object[] paramenters = new object[] {
                            client
                        };


                        //Object o = Activator.CreateInstance(typeof(ActionData), BindingFlags.Default, paramenters);

                        dynamic obj = m_assembly.CreateInstance($"SoftLiu_ServerIOCSharp.SocketData.ProtocolData.{protocol.Type}",
                                                                false, BindingFlags.Default,
                                                                null, paramenters, null, null);
                        if (obj is ActionData)
                        {
                            ActionData data = obj as ActionData;
                            data.Init(recvData);
                        }
                        else
                        {
                            Console.WriteLine($"SocketProtocolData CreateInstance is null, action: {action}, type: {protocol.Type}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"SocketProtocolData is null, action: {action}");
                    }
                }
            }
        }