/// <summary> /// The callback of Connect_to_Server which ends the connect /// </summary> /// <param name="state_in_an_ar_object"> The state passed from the caller</param> public static void Connected_to_Server(IAsyncResult state_in_an_ar_object) { PreservedState currentState = (PreservedState)state_in_an_ar_object.AsyncState; try { // End the connect currentState.stateSocket.EndConnect(state_in_an_ar_object); // Call the callback currentState.Callback(currentState); } catch (Exception e) { // Tell the GUI there's an error currentState.error = true; currentState.Callback(currentState); Console.WriteLine(e.Message); } }
/// <summary> /// The callback of i_want_more_data that ends the receive /// </summary> /// <param name="state_in_an_ar_object"> The state sent from the caller</param> public static void ReceiveCallback(IAsyncResult state_in_an_ar_object) { PreservedState state = (PreservedState)state_in_an_ar_object.AsyncState; try { Socket sock = state.stateSocket; // End the receive int bytesRead = sock.EndReceive(state_in_an_ar_object); // Put the server's bytes into the stringbuilder if (bytesRead > 0) { lock (state.sb) { state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); // Call the callback state.Callback(state); } } else { if (state.sb.Length > 1) { response = state.sb.ToString(); } } } catch (Exception e) { // Tell the GUI there's an error state.error = true; state.Callback(state); Console.WriteLine(e.Message); } }
/// <summary> /// Begins receive for more data from the server /// </summary> /// <param name="state_in_an_ar_object"> The state sent from the caller</param> public static void i_want_more_data(PreservedState state_in_an_ar_object) { PreservedState state = (PreservedState)state_in_an_ar_object; try { Socket client = state.stateSocket; // Begin the receive client.BeginReceive(state.buffer, 0, PreservedState.BufferSize, 0, ReceiveCallback, state); } catch (Exception e) { // Tell the GUI there's an error state.error = true; state.Callback(state); Console.WriteLine(e.Message); } }