private void ConnectButton_Clicked(object sender, EventArgs e) //próba połączenia z serwerem { ConnectionClass.afterAutoreconnect = false; if (!ConnectionClass.connected) { try { if (short.Parse(Port_entry.Text) < 1) { DisplayAlert(AppResources.Error, AppResources.WrongPortNumberError, AppResources.OK); return; } } catch (Exception ex) { DisplayAlert(AppResources.WrongPortNumberError, ex.ToString(), AppResources.OK); return; } if (ConnectionClass.Connect(IP_address_entry.Text, Port_entry.Text, Password_entry.Text) == ConnectionState.CONNECTION_NOT_ESTABLISHED) { DisplayAlert(AppResources.Error, AppResources.NoConnectionError + "\n" + ConnectionClass.exceptionText, AppResources.OK); } else { ConnectButton.Text = AppResources.Disconnect; } } else { if (ConnectionClass.Disconnect() == ConnectionState.DISCONECT_NOT_SUCCESS) { DisplayAlert(AppResources.Error, AppResources.NoConnectionError + "\n" + ConnectionClass.exceptionText, AppResources.OK); } else { ConnectButton.Text = AppResources.Connect; } } }
public static ConnectionState Send(CommandsFromClient commands, Byte[] data = null) //wysyłanie polecenia { if (!startApplicationConnectAttempt && (!afterAutoreconnect && !tcpClient.Connected || !ConnectionClass.connected)) { afterAutoreconnect = true; Disconnect(); for (int i = 0; i < 5; i++) //próba ponownego połączenia w przypadku utraty łączności { Connect(ipAddress, port.ToString(), password); if (tcpClient.Connected && ConnectionClass.connected) { afterAutoreconnect = false; break; } } } if (ConnectionClass.connected) { ConnectionState sendConnectionState = ConnectionState.SEND_SUCCESS; for (int i = 0; i < 5; ++i) //próba ponownego połączenia w przypadku utraty łączności { if (!ConnectionClass.connected) { ConnectionClass.Connect(ipAddress, port.ToString(), password); } try { Byte[] command; Byte[] dataToSend; Byte[] dataToSendEncoded; command = BitConverter.GetBytes((int)commands); if (data == null) { dataToSend = new Byte[command.Length]; Buffer.BlockCopy(command, 0, dataToSend, 0, command.Length); try { using (var stream = new MemoryStream()) { var proc = _aes.CreateEncryptor(); using (var crypto = new CryptoStream(stream, proc, CryptoStreamMode.Write)) { crypto.Write(dataToSend, 0, dataToSend.Length); crypto.Clear(); crypto.Close(); } stream.Close(); dataToSendEncoded = stream.ToArray(); } } catch (Exception error) { exceptionText = error.ToString(); return(ConnectionState.SEND_NOT_SUCCESS); } } else { dataToSend = new Byte[command.Length + data.Length]; int messageSize = (int)(command.Length + data.Length); Buffer.BlockCopy(command, 0, dataToSend, 0, command.Length); Buffer.BlockCopy(data, 0, dataToSend, command.Length, data.Length); try { using (var stream = new MemoryStream()) { var proc = _aes.CreateEncryptor(); using (var crypto = new CryptoStream(stream, proc, CryptoStreamMode.Write)) { crypto.Write(dataToSend, 0, dataToSend.Length); crypto.Clear(); crypto.Close(); } stream.Close(); dataToSendEncoded = stream.ToArray(); } } catch (Exception error) { exceptionText = error.ToString(); return(ConnectionState.SEND_NOT_SUCCESS); } } byte[] dataToSendEncodedWithLength = new byte[sizeof(int) + dataToSendEncoded.Length]; Buffer.BlockCopy(BitConverter.GetBytes(dataToSendEncoded.Length), 0, dataToSendEncodedWithLength, 0, sizeof(int)); Buffer.BlockCopy(dataToSendEncoded, 0, dataToSendEncodedWithLength, sizeof(int), dataToSendEncoded.Length); ConnectionClass.stream.Write(dataToSendEncodedWithLength, 0, dataToSendEncodedWithLength.Length); sendConnectionState = ConnectionState.SEND_SUCCESS; break; } catch (Exception error) { exceptionText = error.ToString(); Disconnect(); sendConnectionState = ConnectionState.SEND_NOT_SUCCESS; } } return(sendConnectionState); } else { return(ConnectionState.CONNECTION_NOT_ESTABLISHED); } }