예제 #1
0
        //--------------------------
        //***** HELPER METHODS *****
        //--------------------------
        private async Task TryConnectToServer(Action successAction)
        {
            //the mutex here should simulate the action of spamming the button.
            //no matter what, it will only do it one at a time: the mutex is only released when the bg thread ends
            if (connectMutex == null)
            {
                connectMutex = new AutoResetEvent(true);
                if (!connectMutex.WaitOne(0))
                {
                    return;
                }
            }
            else if (!connectMutex.WaitOne(0))
            {
                return;
            }

            if (World.Instance.Client.ConnectedAndInitialized && World.Instance.Client.Connected)
            {
                successAction();
                connectMutex.Set();
                return;
            }

            //execute this logic on a separate thread so the game doesn't lock up while it's trying to connect to the server
            await TaskFramework.Run(() =>
            {
                try
                {
                    if (World.Instance.Client.ConnectToServer(host, port))
                    {
                        m_packetAPI = new PacketAPI((EOClient)World.Instance.Client);

                        //set up event packet handling event bindings:
                        //	some events are triggered by the server regardless of action by the client
                        m_callbackManager = new PacketAPICallbackManager(m_packetAPI, this);
                        m_callbackManager.AssignCallbacks();

                        ((EOClient)World.Instance.Client).EventDisconnect += () => m_packetAPI.Disconnect();

                        InitData data;
                        if (m_packetAPI.Initialize(World.Instance.VersionMajor,
                                                   World.Instance.VersionMinor,
                                                   World.Instance.VersionClient,
                                                   Win32.GetHDDSerial(),
                                                   out data))
                        {
                            switch (data.ServerResponse)
                            {
                            case InitReply.INIT_OK:
                                ((EOClient)World.Instance.Client).SetInitData(data);

                                if (!m_packetAPI.ConfirmInit(data.emulti_e, data.emulti_d, data.clientID))
                                {
                                    throw new Exception();                                             //connection failed!
                                }

                                World.Instance.MainPlayer.SetPlayerID(data.clientID);
                                World.Instance.SetAPIHandle(m_packetAPI);
                                successAction();
                                break;

                            default:
                                string extra;
                                DATCONST1 msg = m_packetAPI.GetInitResponseMessage(out extra);
                                EODialog.Show(msg, extra);
                                break;
                            }
                        }
                        else
                        {
                            throw new Exception();                             //connection failed!
                        }
                    }
                    else
                    {
                        //show connection not found
                        throw new Exception();
                    }
                }
                catch
                {
                    if (!exiting)
                    {
                        EODialog.Show(DATCONST1.CONNECTION_SERVER_NOT_FOUND);
                    }
                }

                connectMutex.Set();
            });
        }
예제 #2
0
		//--------------------------
		//***** HELPER METHODS *****
		//--------------------------
		private async Task TryConnectToServer(Action successAction)
		{
			//the mutex here should simulate the action of spamming the button.
			//no matter what, it will only do it one at a time: the mutex is only released when the bg thread ends
			if (_connectMutex == null)
			{
				_connectMutex = new AutoResetEvent(true);
				if (!_connectMutex.WaitOne(0))
					return;
			}
			else if (!_connectMutex.WaitOne(0))
			{
				return;
			}

			if (World.Instance.Client.ConnectedAndInitialized && World.Instance.Client.Connected)
			{
				successAction();
				_connectMutex.Set();
				return;
			}
			
			//execute this logic on a separate thread so the game doesn't lock up while it's trying to connect to the server
			await TaskFramework.Run(() =>
			{
				try
				{
					if (World.Instance.Client.ConnectToServer(host, port))
					{
						_packetAPI = new PacketAPI((EOClient) World.Instance.Client);

						//set up event packet handling event bindings: 
						//	some events are triggered by the server regardless of action by the client
						_callbackManager = new PacketAPICallbackManager(_packetAPI, this);
						_callbackManager.AssignCallbacks();

						((EOClient) World.Instance.Client).EventDisconnect += () => _packetAPI.Disconnect();

						InitData data;
						if (_packetAPI.Initialize(World.Instance.VersionMajor,
							World.Instance.VersionMinor,
							World.Instance.VersionClient,
							HDDSerial.GetHDDSerial(),
							out data))
						{
							switch (data.ServerResponse)
							{
								case InitReply.INIT_OK:
									((EOClient) World.Instance.Client).SetInitData(data);

									if (!_packetAPI.ConfirmInit(data.emulti_e, data.emulti_d, data.clientID))
									{
										throw new Exception(); //connection failed!
									}

									World.Instance.MainPlayer.SetPlayerID(data.clientID);
									World.Instance.SetAPIHandle(_packetAPI);
									successAction();
									break;
								default:
									string extra;
									DATCONST1 msg = _packetAPI.GetInitResponseMessage(out extra);
									EOMessageBox.Show(msg, extra);
									break;
							}
						}
						else
						{
							throw new Exception(); //connection failed!
						}
					}
					else
					{
						//show connection not found
						throw new Exception();
					}
				}
				catch
				{
					if (!_exiting)
					{
						EOMessageBox.Show(DATCONST1.CONNECTION_SERVER_NOT_FOUND);
					}
				}

				_connectMutex.Set();
			});
		}