//all bots are going to want to do the init handshake with the server public virtual void Initialize() { _client = new EOClient(); if (!_client.ConnectToServer(_host, _port)) throw new ArgumentException(string.Format("Bot {0}: Unable to connect to server! Host={1} Port={2}", _index, _host, _port)); _api = new PacketAPI(_client); InitData data; if (!_api.Initialize(0, 0, 28, EOLib.HDDSerial.GetHDDSerial(), out data)) throw new TimeoutException(string.Format("Bot {0}: Failed initialization handshake with server!", _index)); _client.SetInitData(data); if (!_api.ConfirmInit(data.emulti_e, data.emulti_d, data.clientID)) throw new TimeoutException(string.Format("Bot {0}: Failed initialization handshake with server!", _index)); if (!_api.Initialized || !_client.ConnectedAndInitialized || data.ServerResponse != InitReply.INIT_OK) throw new InvalidOperationException(string.Format("Bot {0}: Invalid response from server or connection failed! Must receive an OK reply.", _index)); _initialized = true; }
public EOBot(int index, string host, int port) { m_index = index + 1; m_client = new EOClient(); if (!m_client.ConnectToServer(host, port)) throw new ArgumentException(); m_api = new PacketAPI(m_client); InitData data; if (!m_api.Initialize(0, 0, 28, EOLib.Win32.GetHDDSerial(), out data)) throw new TimeoutException(); m_client.SetInitData(data); if (!m_api.ConfirmInit(data.emulti_e, data.emulti_d, data.clientID)) throw new TimeoutException(); if (!m_api.Initialized || !m_client.ConnectedAndInitialized || data.ServerResponse != InitReply.INIT_OK) throw new InvalidOperationException(); m_terminationEvent = new AutoResetEvent(false); }
//-------------------------- //***** 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(); }); }