/// <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> /// (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); }
/// <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)); }
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(); } }
public static int SendCall(Socket in_socket, GbxCall in_call) { if (in_socket.Connected) { lock (in_socket) { try { // create request body ... byte[] body = Encoding.UTF8.GetBytes(in_call.Xml); // create response header ... byte[] bSize = BitConverter.GetBytes(body.Length); byte[] bHandle = BitConverter.GetBytes(in_call.Handle); // create call data ... byte[] call = new byte[bSize.Length + bHandle.Length + body.Length]; Array.Copy(bSize, 0, call, 0, bSize.Length); Array.Copy(bHandle, 0, call, 4, bHandle.Length); Array.Copy(body, 0, call, 8, body.Length); // send call ... in_socket.Send(call); return in_call.Handle; } catch { return 0; } } } else { throw new NotConnectedException(); } }
public static int SendCall(Socket in_socket, GbxCall in_call) { if (in_socket.Connected) { lock (in_socket) { try { // create request body ... byte[] body = Encoding.UTF8.GetBytes(in_call.Xml); // create response header ... byte[] bSize = BitConverter.GetBytes(body.Length); byte[] bHandle = BitConverter.GetBytes(in_call.Handle); // create call data ... byte[] call = new byte[bSize.Length + bHandle.Length + body.Length]; Array.Copy(bSize, 0, call, 0, bSize.Length); Array.Copy(bHandle, 0, call, 4, bHandle.Length); Array.Copy(body, 0, call, 8, body.Length); // send call ... in_socket.Send(call); return(in_call.Handle); } catch { return(0); } } } else { throw new NotConnectedException(); } }
public static GbxCall ReceiveCall(Socket in_socket, byte[] inHeader) { if (in_socket.Connected) { lock (in_socket) { // read response size and handle ... byte[] bSize = new byte[4]; byte[] bHandle = new byte[4]; if (inHeader == null) { in_socket.Receive(bSize); in_socket.Receive(bHandle); } else { Array.Copy(inHeader, 0, bSize, 0, 4); Array.Copy(inHeader, 4, bHandle, 0, 4); } int size = BitConverter.ToInt32(bSize, 0); int handle = BitConverter.ToInt32(bHandle, 0); // receive response body ... byte[] data = ReceiveRpc(in_socket, size); // parse the response ... GbxCall call = new GbxCall(handle, data); return(call); } } else { throw new NotConnectedException(); } }
public static GbxCall ReceiveCall(Socket in_socket, byte[] inHeader) { if (in_socket.Connected) { lock (in_socket) { // read response size and handle ... byte[] bSize = new byte[4]; byte[] bHandle = new byte[4]; if (inHeader == null) { in_socket.Receive(bSize); in_socket.Receive(bHandle); } else { Array.Copy(inHeader, 0, bSize, 0, 4); Array.Copy(inHeader, 4, bHandle, 0, 4); } int size = BitConverter.ToInt32(bSize, 0); int handle = BitConverter.ToInt32(bHandle, 0); // receive response body ... byte[] data = ReceiveRpc(in_socket, size); // parse the response ... GbxCall call = new GbxCall(handle, data); return call; } } else { throw new NotConnectedException(); } }
public GbxCallbackEventArgs(GbxCall response) { Response = response; }
/// <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); }
/// <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; } } }