/// <summary> /// Sends a Request and does not wait for a response of the server. /// The response will be written into a buffer or you can set a callback method /// that will be executed. /// </summary> /// <param name="inMethodName">The method to call.</param> /// <param name="inParams">Parameters describing your request.</param> /// <param name="callbackHandler">An optional delegate which is callen when the response is available otherwise set it to null.</param> /// <returns>Returns a handle to your request.</returns> public int AsyncRequest(string inMethodName, object[] inParams, GbxCallCallbackHandler callbackHandler) { // send the call and remember the handle ... GbxCall Request = new GbxCall(inMethodName, inParams); Request.Handle = --this.requests; int handle = XmlRpc.SendCall(this.tcpSocket, Request); lock (this) { if (handle != 0) { // register a callback on this request ... if (callbackHandler != null) { callbackList.Add(handle, callbackHandler); } // return handle id ... return(handle); } else { return(0); } } }
/// <summary> /// Sends a request to the server and blocks until a response has been received. /// </summary> /// <param name="inMethodName">The method to call.</param> /// <param name="inParams">Parameters describing your request.</param> /// <returns>Returns a response object from the server.</returns> public GbxCall Request(string inMethodName, object[] inParams) { // reset event ... callRead.Reset(); // send the call and remember the handle we are waiting on ... GbxCall Request = new GbxCall(inMethodName, inParams); Request.Handle = --this.requests; int handle = XmlRpc.SendCall(this.tcpSocket, Request); // wait until we received the call ... do { callRead.WaitOne(); } while (responses[handle] == null && tcpSocket.Connected); // did we get disconnected ? if (!tcpSocket.Connected) { throw new NotConnectedException(); } // get the call and return it ... return(GetResponse(handle)); }
/// <summary> /// (Dis)activates callbacks from the server. /// </summary> /// <param name="inState">Whether to receive callbacks from the server.</param> /// <returns>Returns new callback state</returns> public bool EnableCallbacks(bool inState) { GbxCall EnableCall = new GbxCall("EnableCallbacks", new object[] { inState }); EnableCall.Handle = --this.requests; return(XmlRpc.SendCall(this.tcpSocket, EnableCall) != 0); }
private void OnDataArrive(IAsyncResult iar) { // end receiving and check if connection's still alive ... try { tcpSocket.EndReceive(iar); // receive the message from the server ... GbxCall call = XmlRpc.ReceiveCall(this.tcpSocket, m_buffer); // watch out for the next calls ... m_buffer = new byte[8]; asyncResult = tcpSocket.BeginReceive(m_buffer, 0, m_buffer.Length, SocketFlags.None, new AsyncCallback(OnDataArrive), null); if (call.Type == MessageTypes.Callback) { // throw new event ... GbxCallbackEventArgs eArgs = new GbxCallbackEventArgs(call); OnGbxCallback(eArgs); } else { // add the response to the queue ... lock (this) { responses.Add(call.Handle, call); } // callback if any method was set ... if (callbackList[call.Handle] != null) { ((GbxCallCallbackHandler)callbackList[call.Handle]).BeginInvoke(call, null, null); callbackList.Remove(call.Handle); } } } catch { this.m_connected = false; // something went wrong :S tcpSocket.Close(); // release a disconnect event ... OnDisconnectCallback(); } finally { // we received something :) callRead.Set(); } }
private void ParseEventGbx(XmlRpc.GbxCallbackEventArgs e) { if (e.Response.MethodName == "ManiaPlanet.PlayerChat" && e.Response.Params.Count == 4) { try { Structs.PlayerChat PC = new Structs.PlayerChat(); PC.PlayerUid = (int)e.Response.Params[0]; PC.Login = (string)e.Response.Params[1]; PC.Text = (string)e.Response.Params[2]; PC.IsRegisteredCmd = (bool)e.Response.Params[3]; if (OnPlayerChat != null) OnPlayerChat(PC); } catch { } } else if (e.Response.MethodName == "ManiaPlanet.PlayerConnect" && e.Response.Params.Count == 2) { try { Structs.PlayerConnect PC = new Structs.PlayerConnect(); PC.Login = (string)e.Response.Params[0]; PC.IsSpectator = (bool)e.Response.Params[1]; if (OnPlayerConnect != null) OnPlayerConnect(PC); } catch { } } else if (e.Response.MethodName == "ManiaPlanet.PlayerDisconnect" && e.Response.Params.Count == 1) { try { Structs.PlayerDisconnect PD = new Structs.PlayerDisconnect(); PD.Login = (string)e.Response.Params[0]; if (OnPlayerDisconnect != null) OnPlayerDisconnect(PD); } catch { } } else if (e.Response.MethodName == "ManiaPlanet.VoteUpdated" && e.Response.Params.Count == 4) { try { Structs.VoteUpdated VU = new Structs.VoteUpdated(); VU.StateName = (string)e.Response.Params[0]; VU.Login = (string)e.Response.Params[1]; VU.CmdName = (string)e.Response.Params[2]; VU.CmdParam = (string)e.Response.Params[3]; if (OnVoteUpdated != null) OnVoteUpdated(VU); } catch { } } else if (e.Response.MethodName == "ManiaPlanet.ModeScriptCallback" && e.Response.Params.Count == 2) { try { Structs.ModeScriptCallback MSC = new Structs.ModeScriptCallback(); MSC.Param1 = (string)e.Response.Params[0]; MSC.Param2 = (string)e.Response.Params[1]; if (OnModeScriptCallback != null) OnModeScriptCallback(MSC); } catch { } } else { // Unhandled callback } }
void Client_EventGbxCallback(object o, XmlRpc.GbxCallbackEventArgs e) { lock (m_lockCallback) { ParseEventGbx(e); } return; SimpleDelegate d = delegate() { ParseEventGbx(e); }; ThreadStart threadDelegate = new ThreadStart(d); new System.Threading.Thread(threadDelegate).Start(); }