public async static Task <string> SendandRecieveTCPData(HTTPData data, int Timeout = 5000) { try { string json = JsonConvert.SerializeObject(data); var req = new HttpRequestMessage() { Content = new ByteArrayContent(Encoding.ASCII.GetBytes(json)), Method = new HttpMethod("POST"), RequestUri = URI, }; //msClient.Timeout = TimeSpan.FromMilliseconds(Timeout); var msg = msClient.SendAsync(req).Result; Stream rstream = await msg.Content.ReadAsStreamAsync(); byte[] buff = new byte[1024]; int rec = rstream.Read(buff, 0, Convert.ToInt32(rstream.Length)); byte[] databuff = new byte[rec]; Array.Copy(buff, databuff, rec); string text = Encoding.ASCII.GetString(databuff); return(text); } catch (SocketException ex) { throw new Exception("Could Not Send Data to Master Server!", ex); } }
private void SendData(string fpm, string airspd, string plnPitch, string pnlBank, string windSpeed, string windHeading, string aircrftType, string aircraftID) { try { if (GlobalData.LoggedIn && GlobalData.CurrentConnectedLRMServer.Type == "LRMComp") { HTTPData data = new HTTPData() { Auth = GlobalData.Auth, Header = "Landing_Data", Body = new Dictionary <string, string>() { { "Fpm", fpm }, { "Air_Speed", airspd }, { "Plane_Pitch", plnPitch }, { "Plane_Bank", pnlBank }, { "Wind_Speed", windSpeed }, { "Wind_Heading", windHeading }, { "Aircraft_Type", aircrftType }, { "Aircraft_ID", aircraftID } } }; //Send to master server } } catch (Exception ex) { GlobalData.ErrorLogInput(ex, "WARNING"); } }
public static async Task <bool> Connect(string rURI) { try { LRMServerClient = new HttpClient(); URI = new Uri(rURI); HTTPData data = new HTTPData() { Header = "New_LRMClient", Auth = GlobalData.Auth, Body = new Dictionary <string, string>() { { "Simulator", GlobalData.CurrentFlightSim.Simulator.ToString() } } }; string responce = await SendandRecieveHTTPData(data); HTTPData reData = JsonConvert.DeserializeObject <HTTPData>(responce); if (reData.Header == "Connected") { GlobalData.CurrentConnectedLRMServer = new LRMServerData() { Name = reData.Body.FirstOrDefault(x => x.Key == "Name").Value, Type = reData.Body.FirstOrDefault(x => x.Key == "Type").Value, PlayerCount = reData.Body.FirstOrDefault(x => x.Key == "Player_Count").Value, serverURI = URI }; Connected = true; return(true); } if (reData.Header == "Server_Error") { string reason = reData.Body.FirstOrDefault(x => x.Key == "Reason").Value; MessageBox.Show($"Error connecting to server: {reason}", "Uh Oh!", MessageBoxButtons.OK); Connected = false; return(false); } if (reData.Header == "Invalad") { MessageBox.Show(reData.Body.FirstOrDefault(x => x.Key == "Reason").Value, "Uh Oh!", MessageBoxButtons.OK); Connected = false; return(false); } else { MessageBox.Show("Error Connecting to server, Unknown Responce", "Uh Oh!", MessageBoxButtons.OK); Connected = false; return(false); } } catch { MessageBox.Show("Error talking to LRM Server!", "Uh Oh!", MessageBoxButtons.OK); Connected = false; return(false); } }
private void button2_Click(object sender, EventArgs e) { try { if (_clientSocket.Connected) { HTTPData jdata = new HTTPData() { Body = new Dictionary <string, string>(), Header = "Disconnect", Auth = GlobalData.Auth }; GlobalData.sendJSONdata(jdata); _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); if (!_clientSocket.Connected) { label1.Text = "Connected to: None"; label1.ForeColor = Color.Red; //GlobalData.socket = null; //MessageBox.Show("Disconnected!", "Disconnect message"); _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { MessageBox.Show("Failed to Disconnect.. :(", "Uh Oh!"); } } else { _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); } } catch (Exception ex) { //_clientSocket.Shutdown(SocketShutdown.Both); //_clientSocket.Close(); //if (!_clientSocket.Connected) //{ // label1.Text = "Connected to: None"; // label1.ForeColor = Color.Red; // GlobalData.socket = null; // //MessageBox.Show("Disconnected!", "Disconnect message"); // _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //} //else //{ // MessageBox.Show("Failed to Disconnect.. :(", "Uh Oh!"); //} } }
internal static Uri URI = new Uri("http://localhost:8081/"); //master server ip public async static Task <bool> Connect() { HTTPData data = new HTTPData(); data.Header = "New_Client"; data.Body = new Dictionary <string, string>(); data.Body.Add("Version", GlobalData.Version); string rcved = ""; msClient.Timeout = new TimeSpan(50000000); try { rcved = await SendandRecieveTCPData(data); } catch (Exception) { var msgb = MessageBox.Show("Error Connecting to Master server, Continue in offline mode?", "Uh Oh!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (msgb == DialogResult.Yes) { GlobalData.Offlinemode = true; return(true); } } HTTPData recievedData = JsonConvert.DeserializeObject <HTTPData>(rcved); if (recievedData.Header == "Good_Version") { //good return(true); } if (recievedData.Header == "Bad_Version") { //bad MessageBox.Show("Client out of date. Please install the new version at https://www.5fsx.com/lrm/ to access Online mode!, you are now in Offline Mode!", "Uh Oh!", MessageBoxButtons.OK); GlobalData.Offlinemode = true; return(true); } else { var m = MessageBox.Show("Unknown Responce From Master Server, Would you like to play in offline mode?", "Uh Oh!", MessageBoxButtons.YesNo); if (m == DialogResult.Yes) { GlobalData.Offlinemode = true; return(true); } else { return(false); } //unknown responce } }
private void button3_Click(object sender, EventArgs e) { try { HTTPData data = new HTTPData() { Auth = GlobalData.Auth, Header = "Get_Servers", Body = new Dictionary <string, string>() }; string text = ""; try { text = MasterServer.SendandRecieveTCPData(data).Result; } catch (Exception ex) { GlobalData.ErrorLogInput(ex, "ERROR"); MessageBox.Show("Error Recieving The servers list!", "Uh Oh!", MessageBoxButtons.OK); } //var header = JsonConvert.DeserializeObject<Dictionary<string, string>>(text).FirstOrDefault(x => x.Key == "Header").Value; if (text.Contains("Header\":\"Server_Error")) { if (GlobalData.Auth == "") { label2.Show(); } else { HTTPData recdata = JsonConvert.DeserializeObject <HTTPData>(text); MessageBox.Show($"Invalad Request! Server Responce:\n{recdata.Body.FirstOrDefault(x => x.Key == "Reason").Value}", "Uh Oh!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); GlobalData.ErrorLogInput(new Exception("Invalad Authentication Token!"), "ERROR"); } } if (text.Contains("Header\":\"LRMServers_List")) { label2.Hide(); MasterServer.LRMServerClientListHTTP serverListData = JsonConvert.DeserializeObject <MasterServer.LRMServerClientListHTTP>(text); if (serverListData.Auth == GlobalData.Auth) { serverList = serverListData.Body; updataFormItems(); } } else { MessageBox.Show("Unknown Server Responce!", "Uh Oh!"); } } catch (Exception ex) { GlobalData.ErrorLogInput(ex, "WARNING"); } }
internal static void sendJSONdata(HTTPData data) { //if(socket != null) //{ // if (LoggedIn) // { // if (!data.Equals(null)) // { // string jsonString = JsonConvert.SerializeObject(data); // byte[] dat = Encoding.ASCII.GetBytes(jsonString); // socket.Send(dat); // } // else // throw new Exception("Data was null!"); // } // else // throw new Exception("User not logged in!"); //} //else { throw new Exception("The current server socket was null!", new SocketException()); } }
private async void GetandSendPosData(object sender, EventArgs e) { try { if (!GlobalData.Offlinemode) { HTTPData data = new HTTPData(); data.Header = "Position_Data"; data.Auth = GlobalData.Auth; data.Body = new Dictionary <string, string>() { { "Latitude", GlobalData.Lat.ToString() }, { "Longitude", GlobalData.Long.ToString() }, { "Speed", GlobalData.speed.ToString() }, { "Heading", GlobalData.Heading.ToString() } }; await MasterServer.SendandRecieveTCPData(data); } } catch { } }
private void button1_Click(object sender, EventArgs e) { if (!GlobalData.LoggedIn) { if (!memloging) { try { HttpListener l = new HttpListener(); l.Prefixes.Add("http://*****:*****@"\5Daddy Landing Monitor.exe.config"); if (Regex.IsMatch(filecont, "<!--" + memloginName + "|" + memloginToken + "-->")) { string newrplc = Regex.Replace(filecont, "<!--" + memloginName + "|" + memloginToken + "-->", string.Empty); File.WriteAllText(Environment.CurrentDirectory + @"\5Daddy Landing Monitor.exe.config", newrplc); button1.Text = "Login with Discord"; } } } GlobalData.Username = ""; GlobalData.LoggedIn = false; memloginToken = ""; memloginName = ""; memloging = false; timer1.Enabled = false; label4.Text = "Not logged in.."; label4.ForeColor = Color.Red; } }
private async void GetServers(object sender, EventArgs e) { if (GlobalData.Offlinemode) { label1.Hide(); label5.Show(); dataGridView1.Hide(); button1.Hide(); button2.Hide(); button3.Hide(); comboBox1.Hide(); label2.Hide(); } else { if (GlobalData.LoggedIn) { if (Visible) { label2.Hide(); HTTPData data = new HTTPData(); data.Header = "Get_Servers"; data.Auth = GlobalData.Auth; //byte[] sendBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(data)); MasterServer.ReceiveTimeout = 5000; string retString = ""; try { retString = await MasterServer.SendandRecieveTCPData(data); } catch (Exception ex) { MessageBox.Show("Could not Refresh sevrers. The Master Didnt Respond!", "Uh Oh!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string text = retString; if (text.Contains("Header\":\"Server_Error")) { if (GlobalData.Auth == "") { label2.Show(); } else { HTTPData recdata = JsonConvert.DeserializeObject <HTTPData>(text); MessageBox.Show($"Invalad Request! Server Responce:\n{recdata.Body.FirstOrDefault(x => x.Key == "Reason").Value}", "Uh Oh!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); GlobalData.ErrorLogInput(new Exception("Invalad Authentication Token!"), "ERROR"); } } if (text.Contains("Header\":\"LRMServers_List")) { label2.Hide(); MasterServer.LRMServerClientListHTTP serverListData = JsonConvert.DeserializeObject <MasterServer.LRMServerClientListHTTP>(text); if (serverListData.Auth == GlobalData.Auth) { serverList = serverListData.Body; updataFormItems(); } } else { MessageBox.Show("Unknown Server Responce!", "Uh Oh!"); } } } else { label1.Hide(); label5.Hide(); dataGridView1.Hide(); button1.Hide(); button2.Hide(); button3.Hide(); comboBox1.Hide(); label2.Show(); } } }
private void button1_Click(object sender, EventArgs e) { //if(GlobalData.socket is null) { try { if (comboBox1.SelectedItem != null) { KeyValuePair <string, string> KVP = (KeyValuePair <string, string>)comboBox1.SelectedItem; if (KVP.Value != null) { _clientSocket.Connect(KVP.Value, 7878); HTTPData jdata = new HTTPData() { Auth = GlobalData.Auth, Header = "Connect", Body = new Dictionary <string, string>() }; jdata.Body.Add("Discord_Username", GlobalData.Username); GlobalData.sendJSONdata(jdata); label1.Text = "Connected to: " + KVP.Key; label1.ForeColor = Color.Green; _clientSocket.ReceiveTimeout = 5000; byte[] ResponceBuff = new byte[1024]; int lng = 0; try { lng = _clientSocket.Receive(ResponceBuff); } catch (Exception ex) { MessageBox.Show("Server Timed out!, Disconnecting!", "Uh Oh!", MessageBoxButtons.OK); GlobalData.ErrorLogInput(ex, "Sever Time Out!"); _clientSocket.Disconnect(true); return; } byte[] recBytes = new byte[lng]; Array.Copy(ResponceBuff, recBytes, lng); string responce = Encoding.ASCII.GetString(recBytes); HTTPData responceData = JsonConvert.DeserializeObject <HTTPData>(responce); if (responceData.Auth == GlobalData.Auth) { if (responceData.Header == "Success_Connect") { LRMServerData serv = new LRMServerData() { Name = responceData.Body.FirstOrDefault(x => x.Key == "Name").Value, Type = responceData.Body.FirstOrDefault(x => x.Key == "Type").Value, PlayerCount = responceData.Body.FirstOrDefault(x => x.Key == "Players").Value, //serverSocket = _clientSocket, }; } else { MessageBox.Show("Server Responded with an Invalad Responce!", "Uh Oh!", MessageBoxButtons.OK); } } else { MessageBox.Show("Server Responded with an Invalad Responce!", "Uh Oh!", MessageBoxButtons.OK); } } } } catch (Exception ex) { GlobalData.ErrorLogInput(ex, "ERROR"); MessageBox.Show("Error: " + ex.Message, "Uh Oh!"); } } }