public void Init(string address, int port) { Log.WriteInfo(Log.Verbosity.Normal, "Initializing BOSClient.."); if (address != null) { Log.WriteInfo(Log.Verbosity.Important, "Connecting to " + address + ":" + port.ToString()); while (!communicator.Connect(port, System.Net.IPAddress.Parse(address))) ; CurrentWaitHandler = WaitHandshakeResponseState; Log.WriteInfo(Log.Verbosity.Normal, "Sending greeting.."); communicator.SendMessage(new Communication.JSON.HandshakeRequestMessage("Fabs Superior C# Client", "If u can read this", Algorithm.GetAlgorithmName(), "0.1")); } else { Log.WriteInfo(Log.Verbosity.Important, "Waiting for connection.."); communicator.Listen(port); Log.WriteInfo(Log.Verbosity.Important, "->Connected to " + communicator.Partner); CurrentWaitHandler = WaitHandshakeRequestState; } Log.WriteInfo(Log.Verbosity.Normal, "BOSClient successfully initialized."); }
void OnMessageReceive(Communication.JSON.Message msg) { //Messages which can be received at any time: if(msg is Communication.JSON.MiscellaneousMessage) { Log.WriteGame(Log.Verbosity.Important, "Received miscellaneousMessage: " + (msg as Communication.JSON.MiscellaneousMessage).Text); return; } lock ("UnsafeWaitHandlerState") { var oldhandler = CurrentWaitHandler; try { var res = CurrentWaitHandler.Invoke(msg); if (res == null) throw new BOSClientException("Received an message invalid in the current state of the protocol" + "HandlerType: " + oldhandler.Method.Name + "MessageType:" + msg.GetType().ToString() ); CurrentWaitHandler = res.Item1; if(res.Item2 != null) communicator.SendMessage(res.Item2); return; } catch (Exception ex) { CurrentWaitHandler = oldhandler; Log.WriteError(Log.Verbosity.Normal, "There was an error processing an received message: " + ex.ToString()); } } }