Пример #1
0
        /// <summary>
        /// 开启网络连接
        /// </summary>
        public void Connect(string serverIP, ushort serverPort, Action <bool> connectCallBack)
        {
            if (IsConnectedServer || IsConnecting) // 已经连接上了服务器
            {
                Debug.LogErrorFormat("已经连接上了服务器,请勿重复链接 0:{0} 1:{1} IP:{2} Port:{3}", IsConnectedServer, IsConnecting, serverIP, serverPort);
                connectCallBack(false);
                return;
            }

            if (!IsStartUp)
            {
                Debug.LogError("连接时,没有初始化raknet");
                connectCallBack(false);
                return;
            }

            this.ServerIP   = serverIP;
            this.ServerPort = serverPort;

            if (!IsValidity())
            {
                Debug.LogErrorFormat("Cann't connect client[{0}], it have some error of server ip[{1}] or server port[{2}], please check it!", this.ClientName, this.ServerIP, this.ServerPort);
                connectCallBack(false);
                return;
            }
            this.connectCallBack = connectCallBack;
            //Debug.Log(string.Format("调用raknet连接接口 serverip{0}, port{1}", this.ServerIP, this.ServerPort));
            ConnectionAttemptResult connectResult = mClient.Connect(this.ServerIP, this.ServerPort, null, 0);

            //Debug.Log("调用连接接口的返回值 = " + connectResult);
            if (connectResult == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
            {
                IsConnecting = true;
                //IsConnectSuccess = false;
                //// 已经向服务器发送了连接请求,等待服务器消息反馈
                //// 真正连接成功需要等待服务器返回消息才知道连接成功与否
            }
            else
            {
                IsConnecting = false;
                connectCallBack(false);
            }
        }
Пример #2
0
        public void Start()
        {
            InitPeer();

            if (_State == RakNetClientState.PeerInitOK)
            {
                StartupResult result = _peer.Startup(1, _socketDesc, 1);
                Log.Debug("Start Result: " + result);
                if (result == StartupResult.RAKNET_STARTED)
                {
                    _State = RakNetClientState.StartUpOK;

                    RakNet.FT_ConnectProcess process = new RakNet.FT_ConnectProcess();
                    process.SetResultHandler(new RakNetClientResultHandler(this));
                    AttachInterface2(process);

                    ConnectionAttemptResult connectResult = _peer.Connect(_sServerIP, _nServerPort, "", 0);
                    Log.Debug("Connect Result: " + connectResult);
                    if (ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED == connectResult)
                    {
                        _State = RakNetClientState.ConnectOK;

                        _systemAddress = new SystemAddress(_sServerIP, _nServerPort);

                        _ReadThread.Start();
                    }
                    else
                    {
                        _State = RakNetClientState.ConnectError;
                    }
                }
                else
                {
                    Log.Debug("RakNet.RakPeerInterface.GetInstance() Error : " + result);
                    _State = RakNetClientState.StartUpError;
                }
            }
        }