public static bool CheckParams(string hostname, string port, byte[] buffer, string timeout, byte operation) { //check other parameters string information; bool valid = false; if (!ushort.TryParse(port, out ushort portResult)) { information = "Port must be a combination of four numbers."; } //give the user a max timeout instead of saying it should be less than the allowed size of an int (4,000,000,000) else if (!int.TryParse(timeout, out int timeoutResult) || timeoutResult < 1000 || timeoutResult > 60000) { information = "Timeout must be greater than 1000 and less than 60000 and numbers only."; } else if (buffer.Length >= GlobalConstants.MAX_REQUEST_SIZE) { information = "Maximum request length has been exceeded"; } //using ports 4544 and 4545 else if ((hostname == "codingchallenge.identityone.net") && ((portResult == 4544) || (portResult == 4545))) { //update status box information = (operation == GlobalConstants.ENCODE_OP)? "Requesting to Encode " + buffer.Length + " byte(s) of data." : "Requesting to Decode " + buffer.Length + " byte(s) of data."; valid = true; } else { information = "Check the server name and port."; } //if input contains error, update status and return; otherwise continue to check other parameters if (!valid) { thisCode = new OutputCode(DateTime.UtcNow.ToString("HH:mm:ss.ffff"), "Error", information); return(false); } else { thisCode = new OutputCode(DateTime.UtcNow.ToString("HH:mm:ss.ffff"), "Information", information); } return(true); }
private async void SendDataToServer(string ipOrHostname, ushort port, byte[] payload, int timeout, byte operation) { //set up client to connect to server; pass in timeout value in milliseconds sr.SetProperties(ipOrHostname, port, payload, timeout); //create the request structure before connecting to server so that data is ready //myConnect() should only worry about connecting to the server and reading/writing data sr.CreateRequest(operation); //use task to connect to server so that program doesn't halt await Task.Run(() => sr.MyConnect()).ContinueWith(task => { //if data was read (whether there was error or not) pass this data on to be parsed //if data was read with no error, new data will be returned as newBuffer //if there was an error, the original buffer will be retained resp = new Response(sr.totalDataReceived); //update status box depending on status code received from response lstStatus.Items.Insert(0, OutputCode.StatusOutput((Codes)Enum.Parse(typeof(Codes), resp.GetStatus().ToString()))); //if there was no error increment cycle and update the buffer if ((Codes)Enum.Parse(typeof(Codes), resp.GetStatus().ToString()) == Codes.Zero) { //update cycle count if (operation == GlobalConstants.ENCODE_OP) { sr.IncrementEncodes(); lblEncodeCycleCount.Content = sr.numOfEncodes; } else { sr.DecrementEncodes(); lblEncodeCycleCount.Content = sr.numOfEncodes; } newBuffer = resp.GetData(); buffer = newBuffer; newBuffer = null; } //reenable the UI after process finishes this.IsEnabled = true; }, TaskScheduler.FromCurrentSynchronizationContext()); }