Example #1
0
    // connect to server
    public bool Connect(string strHost, int Port)
    {
        //m_strServer = strHost;
        //m_iPort = Port;

        bool br = false;

        try
        {
            if (Application.webSecurityEnabled)
            {
                int start_index = UnityEngine.Random.Range(0, CROSSDOMAIN_PORTLIST.Length);
                int i           = 0;
                for (; i < CROSSDOMAIN_PORTLIST.Length; i++)
                {
                    if (Security.PrefetchSocketPolicy(strHost, CROSSDOMAIN_PORTLIST[(start_index + i) % CROSSDOMAIN_PORTLIST.Length]))
                    {
                        break;
                    }
                }
                if (i == CROSSDOMAIN_PORTLIST.Length)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "cross_domain failed, ip=" + strHost, LogType.Error);
                    return(br);
                }
            }
            // Debug.LogWarning("connect000============" + strHost);
            br = m_client.connect(strHost, Port);
        }
        catch
        {
        }

        return(br);
    }
Example #2
0
    public int SendCliMsg(object obj, ushort type, bool bEncrypt)
    {
        int iRet = -2;

        if ((null != m_client) && (null != obj))
        {
            try
            {
                iRet = m_client.send((byte[])obj, bEncrypt);
                //byte[] item = NetClient.ConvertToByte(obj);
                //int offset = 0;
                //NetComm.MsgHeadDef header = XConvert.ToObject<NetComm.MsgHeadDef>(item, ref offset);
                //header.cVersion = 3;
                //header.cMsgType = type;
                //byte[] headItem = NetClient.ConvertToByte(header);
                //Array.Copy(headItem, item, headItem.Length);
                ////DEBUG.NetworkClient(type, item);
                //iRet = m_client.send(item, bEncrypt);
            }
            catch (Exception e)
            {
                DEBUG.Networking(DEBUG.TRACER_LOG + "GameClient.SendCliMsg, exp: " + e.Message, LogType.Exception);
            }
        }

        return(iRet);
    }
Example #3
0
        // <summary>
        /// 连接报名服务器
        /// </summary>
        public void Connect()
        {
            tryCount++;
            //设置回调函数
            NetClient.SetCallBack(OnNetMessage, OnDisConnect, OnConnectResult);
            if (NetworkMgr.Instance.ProxyEnable == 1)
            {
                if (proxyIndex == 0)
                {
                    proxyIndex = 1 + Random.Range(0, NetworkMgr.Instance.ProxyInfoList.Count);
                }
                Debug.LogWarning("开始连接服务器" + "开始通过代理[" + NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].ip + ":"
                                 + NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].port + "]连接游戏报名服务器["
                                 + ServerIP + ":" + ServerPort + "]");

                DEBUG.Networking(DEBUG.TRACER_LOG + "开始通过代理[" + NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].ip + ":"
                                 + NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].port + "]连接游戏报名服务器["
                                 + ServerIP + ":" + ServerPort + "]");

                //把代理网址转为IP地址
                IPHostEntry ipHost = Dns.GetHostEntry(NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].ip);
                IPAddress[] ipAddr = ipHost.AddressList;
                NetClient.ConnectServer(NetComm.NetProxy.ProxyType.PROXY_HTTP,
                                        ipAddr[0].ToString(), NetworkMgr.Instance.ProxyInfoList[proxyIndex - 1].port,
                                        ServerIP, ServerPort, "", "");
            }
            else
            {
                proxyIndex = 0;

                Debug.LogError("[LobbyNetMgr] " + "开始连接游戏报名服务器[" + ServerIP + ":" + ServerPort + "]");

                NetClient.ConnectServer(ServerIP, ServerPort);
            }
        }
Example #4
0
        /// <summary>
        /// 当收到网络消息
        /// </summary>
        /// <param name='cMsgType'>消息类型</param>
        /// <param name='pMsg'>消息数据</param>
        /// <param name='len'>数据长度</param>
        void OnNetMessage(ushort cMsgType, byte[] pMsg, int len)
        {
            switch (cMsgType)
            {
            case NetMsg.CLIENT_SERVER_RES:
                DEBUG.Networking(DEBUG.RECV_MSG_LOG + MsgID.ToString(cMsgType));
                HanldeGateSeverRes(pMsg, len);
                break;

            default:
                DEBUG.Networking(DEBUG.TRACER_LOG + "未处理的接收消息类型:" + MsgID.ToString(cMsgType), LogType.Warning);
                break;
            }
            NetworkMgr.Instance.OnNetMessage(cMsgType);
        }
Example #5
0
        public int send(byte[] data, bool bEncrypt)
        {
            int ir = -3;

            if (null != data)
            {
                try
                {
                    string s = MsgID.ToString(data[1]);
                    s += ("  长度: " + data.Length);
                    s += "  数据:";

                    for (int i = 0; i < data.Length; ++i)
                    {
                        s += (" " + data[i].ToString("X2"));
                    }

                    ////// TODO 查看发送十六进制数据(底层发送十六进制消息)
                    if (MahjongLobby_SX_Changzhi.LobbyContants.isOpenDebugMessage_Send)
                    {
                        Debug.Log("+++++++++++++++++++++" + s);
                    }

                    //DEBUG.NetworkClient(data[1], data);
                    byte[] pMsg = (bEncrypt) ? AESCrypt.Encrypt(data, AESCrypt.KEY) : data;
                    if (null != pMsg)
                    {
                        short  iMsgLen = (short)(pMsg.Length + 4);
                        byte[] msg     = new byte[iMsgLen];
                        msg[0]  = PACKAGE_IDENTIFY;
                        msg[1]  = (byte)((bEncrypt) ? 0 : 1);
                        iMsgLen = IPAddress.HostToNetworkOrder(iMsgLen);
                        byte[] len = BitConverter.GetBytes(iMsgLen);
                        Array.Copy(len, 0, msg, 2, len.Length);
                        Array.Copy(pMsg, 0, msg, 4, pMsg.Length);

                        ir = send(msg);
                    }
                }
                catch (Exception e)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetConnection.send, exp: " + e.Message, LogType.Exception);
                }
            }

            return(ir);
        }
Example #6
0
 override protected void OnClose(bool bClosed) //true: server closed
 {
     try
     {
         length = 0;
         ev.WaitOne();
         Packet msgPk = new Packet();
         msgPk.data    = null;
         msgPk.bClosed = bClosed;
         list.Add(msgPk);
         ev.Set();
     }
     catch (Exception e)
     {
         DEBUG.Networking(DEBUG.TRACER_LOG + "NetConnection.OnClose, exp: " + e.Message, LogType.Exception);
     }
 }
Example #7
0
        public new int send(byte[] data, ref EndPoint addr)
        {
            int ir = -1;

            if (null != data)
            {
                try
                {
                    ir = base.send(data, ref addr);
                }
                catch (Exception e)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetUdp.send, exp: " + e.Message, LogType.Exception);
                }
            }

            return(ir);
        }
Example #8
0
        public int send(byte[] data, ref EndPoint addr)
        {
            int ir = -1;

            if ((null != s) && (null != data))
            {
                try
                {
                    ir = s.SendTo(data, addr);
                }
                catch (Exception e)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetSocket.send, exp: " + e.Message, LogType.Exception);
                }
            }

            return(ir);
        }
Example #9
0
        /// <summary>
        /// 发送消息,把消息加入发送队列
        /// </summary>
        /// <param name='obj'>消息对象</param>
        /// <param name='type'>消息类型</param>
        /// <param name='encrypt'>是否加密</param>
        public void SendClientMsg(object obj, ushort type, bool encrypt)
        {
            MsgData msgData;

            try
            {
                msgData          = new MsgData();
                msgData.cType    = type;
                msgData.bEncrypt = encrypt;
                //msgData.obj = obj;
                byte[] item = ToByteArray(obj, type);
                msgData.obj = (object)item;
                sendMsgQueue.Add(msgData);
            }
            catch
            {
                DEBUG.Networking(DEBUG.TRACER_LOG + "Send message except.", LogType.Exception);
            }
        }
Example #10
0
        public int send(byte[] data)
        {
            int ir = -1;

            if ((null != s) && (null != data))
            {
                try
                {
                    //Debug.Log("发送消息:" + data[5].ToString("X2"));
                    //Debug.LogError("发送消息");
                    //FishLobby.GameData.Instance.Printbuff(data);
                    ir = s.Send(data);
                }
                catch (Exception e)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetSocket.send, exp: " + e.Message, LogType.Exception);
                }
            }

            return(ir);
        }
Example #11
0
        public void close()
        {
            if (ev != null)
            {
                ev.WaitOne();
            }
            if (null != s)
            {
                try
                {
                    s.Close();
                }
                catch (Exception e)
                {
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetSocket.close, exp: " + e.Message, LogType.Exception);
                }
                s = null;
            }

            if (ev != null)
            {
                ev.Set();
            }
        }
Example #12
0
        override protected void OnRead(byte[] data, int len, ref EndPoint addr)
        {
            byte[] msg = null;
            Packet msgPk = null;
            int    pos = 0, size = 0, msglen;

            try
            {
                if (0 < length)
                {
                    size = length + len;

                    if (PACKAGE_HEADER_SIZE > size)
                    {
                        if (BUFFER_SIZE >= size)
                        {
                            Array.Copy(data, 0, buffer, length, len);
                            length = size;
                        }
                        else
                        {
                            Debug.LogWarning("Close===========2");
                            close();
                        }
                        return;
                    }

                    if (PACKAGE_HEADER_SIZE > length)
                    {
                        pos = PACKAGE_HEADER_SIZE - length;
                        Array.Copy(data, 0, buffer, length, pos);
                        length += pos;
                        len    -= pos;
                    }

                    //message version
                    if (PACKAGE_IDENTIFY == buffer[0])
                    {
                        //msg length
                        msglen = BitConverter.ToInt16(buffer, 2);
                        msglen = IPAddress.NetworkToHostOrder((short)msglen);

                        if (length + len >= msglen)
                        {
                            size = msglen - length;
                            msg  = new byte[msglen - PACKAGE_HEADER_SIZE];
                            Array.Copy(buffer, PACKAGE_HEADER_SIZE, msg, 0, length - PACKAGE_HEADER_SIZE);
                            Array.Copy(data, pos, msg, length - PACKAGE_HEADER_SIZE, size);
                            pos       += size;
                            len       -= size;
                            msgPk      = new Packet();
                            msgPk.data = (0 == buffer[1]) ? AESCrypt.Decrypt(msg, AESCrypt.KEY) : msg;
                            DEBUG.NetworkServer(data[1], data);
                            if (null != msgPk.data)
                            {
                                ev.WaitOne();
                                list.Add(msgPk);
                                ev.Set();
                            }
                            else
                            {
                                Debug.LogWarning("Close========3");
                                close();
                                return;
                            }
                        }
                        else
                        {
                            size = length + len;
                            if (BUFFER_SIZE >= size)
                            {
                                Array.Copy(data, pos, buffer, length, len);
                                length = size;
                            }
                            else
                            {
                                Debug.LogWarning("Close========4");
                                close();
                            }
                            return;
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Close=========5");
                        close();
                        return;
                    }
                }

                while (PACKAGE_HEADER_SIZE <= len)
                {
                    //message version
                    if (PACKAGE_IDENTIFY == data[pos])
                    {
                        //msg length
                        msglen = BitConverter.ToInt16(data, pos + 2);
                        msglen = IPAddress.NetworkToHostOrder((short)msglen);

                        if (BUFFER_SIZE < msglen)
                        {
                            Debug.LogWarning("Close=========6");
                            close();
                            return;
                        }
                        else if (len >= msglen)
                        {
                            msg = new byte[msglen - PACKAGE_HEADER_SIZE];
                            Array.Copy(data, pos + PACKAGE_HEADER_SIZE, msg, 0, msg.Length);
                            msgPk      = new Packet();
                            msgPk.data = (0 == data[pos + 1]) ? AESCrypt.Decrypt(msg, AESCrypt.KEY) : msg;
                            pos       += msglen;
                            len       -= msglen;

                            if (null != msgPk.data)
                            {
                                ev.WaitOne();
                                list.Add(msgPk);
                                ev.Set();
                            }
                            else
                            {
                                Debug.LogWarning("Close=========7");
                                close();
                                return;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Close=========8");
                        close();
                        return;
                    }
                }

                Array.Copy(data, pos, buffer, 0, len);
                length = len;
            }
            catch (Exception e)
            {
                DEBUG.Networking(DEBUG.TRACER_LOG + "NetConnection.OnRead, exp: " + e.Message, LogType.Exception);
            }
        }
Example #13
0
        public bool create(string strHost, int Port, NetType type)
        {
            //Debug.LogError("create1");
            bProxy = false;
            bool br = false;

            if (null == s)
            {
                //Debug.LogError("create2");
                try
                {
                    IPAddress ip = (null == strHost) ? new IPAddress(0) : IPAddress.Parse(Hostname2ip(strHost));
                    if (MahjongLobby_SX_Changzhi.SDKManager.Instance.IOSCheckStaus == 1)
                    {
                        //Debug.LogError("create3   " + Hostname2ip(strHost));
                        // IPAddress ip = (null == strHost) ? new IPAddress(0) : IPAddress.Parse(Hostname2ip(strHost));
                        //Debug.LogError("create4    " + ip.ToString());
                        addr = new IPEndPoint(ip, Port);

                        Socket ipv6 = new Socket(AddressFamily.InterNetworkV6, (NetType.TCP == type) ? SocketType.Stream : SocketType.Dgram, (ProtocolType)type);
                        Socket ipv4 = new Socket(AddressFamily.InterNetwork, (NetType.TCP == type) ? SocketType.Stream : SocketType.Dgram, (ProtocolType)type);

                        if (s == null)
                        {
                            try
                            {
                                string        serverIp;
                                AddressFamily ipType;
                                CompatibilityIP.GetIpType(((null == strHost) ? new IPAddress(0) : IPAddress.Parse(Hostname2ip(strHost))).ToString(), Port.ToString(), out serverIp, out ipType);
                                //  Debug.LogError("ipType   " + ipType.ToString());
                                ipv6 = new Socket(ipType, SocketType.Stream, ProtocolType.Tcp);

                                // Debug.LogError("create5   " + serverIp);
                                IPAddress address = IPAddress.Parse(serverIp);
                                //  Debug.LogError("create6   " + address.ToString());
                                //EndPoint port = new IPEndPoint(address, Port);
                                s = ipv6;// new Socket(ipType, SocketType.Stream, ProtocolType.Tcp) ;
                                s.Connect(address, Port);

                                Debug.Log("ipv6 ");
                            }
                            catch
                            {
                                s = ipv4; //new Socket(AddressFamily.InterNetwork, (NetType.TCP == type) ? SocketType.Stream : SocketType.Dgram, (ProtocolType)type); ;
                                s.Connect(Hostname2ip(strHost), Port);
                                Debug.Log("ipv4 ");
                            }
                        }
                        else if (NetType.TCP == type)
                        {
                            s.Connect(Hostname2ip(strHost), Port);
                            Debug.Log("正常111 ");
                        }
                        else
                        {
                            s.Bind(addr);
                            Debug.Log("正常2222 ");
                        }
                    }
                    else
                    {
                        //#else
                        //Debug.LogError("create4");
                        s = new Socket(AddressFamily.InterNetwork, (NetType.TCP == type) ? SocketType.Stream : SocketType.Dgram, (ProtocolType)type);
                        // IPAddress ip = (null == strHost) ? new IPAddress(0) : IPAddress.Parse(Hostname2ip(strHost));
                        addr = new IPEndPoint(ip, Port);
                        //Debug.Log("ipv4 ");
                        //Debug.LogError("类型============" + type);
                        if (NetType.TCP == type)
                        {
                            //Debug.LogError("域名解析ip" + Hostname2ip(strHost));
                            s.Connect(Hostname2ip(strHost), Port);
                        }
                        else
                        {
                            s.Bind(addr);
                        }
                        //s.BeginReceiveFrom(buf, 0, buf.Length, 0, ref addr, RecvCallback, null);
                        //br = true;

                        //先开始ipv6的支持,ipv6连不上就使用ipv4
                        //Debug.Log("正常 ");

                        //#endif
                    }
                    s.BeginReceiveFrom(buf, 0, buf.Length, 0, ref addr, RecvCallback, null);
                    br = true;
                }
                catch
                {
                    if (null != s)
                    {
                        s.Close();
                        s = null;
                    }
                    DEBUG.Networking(DEBUG.TRACER_LOG + "NetSocket.create, exp: ", LogType.Exception);
                }
            }
            return(br);
        }