/// <summary> /// Sends the sppecified message through the socket /// Attaches the msgEndDelim to the end of the message to indicate the end of the string /// </summary> /// <param name="message">The message to be sent</param> private void send(ServiceBusRequest message) { MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); binForm.Serialize(memStream, message); byte[] msg = memStream.ToArray(); int msgSize = msg.Length; while (!connection.Connected) { connect(); } //First write the total size of the message byte[] msgSizeBytes = BitConverter.GetBytes(msgSize); connectionStream.Write(msgSizeBytes); connectionStream.Flush(); connectionStream.Write(msg); connectionStream.Flush(); }
/// <summary> /// Executes the request received from the server /// </summary> /// <param name="requestParameters">Information about the request</param> /// <returns>A string representing the result of the request</returns> private ServiceBusResponse executeRequest(ServiceBusRequest request) { System.Diagnostics.Debug.WriteLine("--------------------------------executeRequest---------------------"); switch (request.serviceRequested) { case (Service.Authentication): return(authenticationRequest((AuthenticationServiceRequest)request)); case (Service.Echo): return(echoRequest((EchoServiceRequest)request)); case (Service.CompanyDirectory): return(companyRequest((CompanyDirectoryServiceRequest)request)); case (Service.CompanyReview): return(ReviewRequest((CompanyReviewServiceRequest)request)); case (Service.Chat): return(ChatService((ChatServiceRequest)request)); case (Service.Weather): return(WeatherService((WeatherServiceRequest)request)); default: return(new ServiceBusResponse(false, "Error: Invalid request. Did not specify a valid service type. Specified type was: " + request.serviceRequested.ToString())); } }
/// <summary> /// Sends the sppecified message through the socket /// Attaches the msgEndDelim to the end of the message to indicate the end of the string /// </summary> /// <param name="message">The message to be sent</param> private void send(ServiceBusRequest message) { MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); binForm.Serialize(memStream, message); //memStream.Write(SharedData.msgEndDelim.) byte[] msg = memStream.ToArray(); //byte[] msg = Encoding.UTF8.GetBytes(message + SharedData.msgEndDelim); int msgSize = msg.Length; while (!connection.Connected) { //TODO: Low Importance - Add a timeout to this connect(); } //First write the total size of the message byte[] msgSizeBytes = BitConverter.GetBytes(msgSize); connectionStream.Write(msgSizeBytes); connectionStream.Flush(); connectionStream.Write(msg); connectionStream.Flush(); }
/// <summary> /// Executes the request received from the server /// </summary> /// <param name="requestParameters">Information about the request</param> /// <returns>A string representing the result of the request</returns> private ServiceBusResponse executeRequest(ServiceBusRequest request) { switch (request.serviceRequested) { case (Service.Authentication): return(authenticationRequest((AuthenticationServiceRequest)request)); case (Service.Echo): return(echoRequest((EchoServiceRequest)request)); case (Service.CompanyDirectory): return(directoryRequest((CompanyDirectoryServiceRequest)request)); case (Service.CompanyReviews): return(reviewRequest((CompanyReviewServiceRequest)request)); case (Service.Chat): return(chatRequest((ChatServiceRequest)request)); case (Service.Weather): return(weatherRequest((WeatherNeededRequest)request)); default: return(new ServiceBusResponse(false, "Error: Invalid request. Did not specify a valid service type. Specified type was: " + request.serviceRequested.ToString())); } }
/// <summary> /// Continuously reads data from the client until the end of file string of characters is found /// The end of file string is defined in the Messages library shared by the web server and the bus. /// </summary> /// <returns>The string representation of bytes read from the client socket</returns> private ServiceBusRequest readUntilEOF() { int sizeOfMsg = 0; int bytesRead = 0; byte[] msgSize = new byte[4]; while (bytesRead < msgSize.Length) { try { bytesRead += connectionStream.Read(msgSize, bytesRead, msgSize.Length - bytesRead); } catch (SocketException) // This is thrown when the timeout occurs. The timeout is set in the constructor { Thread.Yield(); // Yield this threads remaining timeslice to another process, this process does not appear to need it currently because the read timed out } catch (IOException) { terminateConnection(); return(null); } } //First we will receive the size of the message sizeOfMsg = BitConverter.ToInt32(msgSize, 0); byte[] requestBytes = new byte[sizeOfMsg]; ServiceBusRequest request = null; bytesRead = 0; while (bytesRead < requestBytes.Length) { try { bytesRead += connectionStream.Read(requestBytes, bytesRead, requestBytes.Length - bytesRead); } catch (SocketException) // This is thrown when the timeout occurs. The timeout is set in the constructor { Thread.Yield(); // Yield this threads remaining timeslice to another process, this process does not appear to need it currently because the read timed out } catch (IOException) { terminateConnection(); return(null); } } MemoryStream memStream = new MemoryStream(requestBytes); BinaryFormatter binForm = new BinaryFormatter(); request = (ServiceBusRequest)binForm.Deserialize(memStream); return(request); }
private ServiceBusResponse weatherRequest(ServiceBusRequest request) { SendOptions sendOptions = new SendOptions(); sendOptions.SetDestination("Weather"); return(requestingEndpoint.Request <ServiceBusResponse>(request, sendOptions). ConfigureAwait(false).GetAwaiter().GetResult()); }
/// <summary> /// Executes the request received from the server /// </summary> /// <param name="requestParameters">Information about the request</param> /// <returns>A string representing the result of the request</returns> private ServiceBusResponse executeRequest(ServiceBusRequest request) { switch (request.serviceRequested) { case (Service.Authentication): return(authenticationRequest((AuthenticationServiceRequest)request)); case (Service.Echo): return(echoRequest((EchoServiceRequest)request)); default: return(new ServiceBusResponse(false, "Error: Invalid request. Did not specify a valid service type. Specified type was: " + request.serviceRequested.ToString())); } }
/// <summary> /// listens for message requests/commands for as long as the connection remains open. /// </summary> public void listenToClient() { while (connection.Connected == true) { //Read the call to the API ServiceBusRequest request = readUntilEOF(); ServiceBusResponse responseMessage = executeRequest(request); sendToClient(responseMessage); if (authenticated == false) { terminateConnection(); } } Debug.consoleMsg("Client connection closing..."); return; }