/// <summary> /// Called when a message has been received from the server /// </summary> /// <param name="bytes">The message as a byte array</param> protected override void OnMessageReceived(byte[] bytes) { //base.OnMessageReceived(bytes); if (_remotePublicKey != null) //then we're secure { base.OnMessageReceived(bytes); //var obj = FormatReceived(bytes); //MessageReceived?.Invoke(this, obj); } else { try { var json = bytes.AsString();//.FromBase64(); if (IsBase64Encoded(json)) { System.Diagnostics.Debug.WriteLine("Remote public key is curiously Base64 encoded"); json = json.FromBase64(); //still not sure how this gets base64 encoded in the first place } _remotePublicKey = Newtonsoft.Json.JsonConvert.DeserializeObject <RSAParameters>(json); System.Diagnostics.Debug.WriteLine("{0} received {1}'s public Key", IsServerSide ? "Server" : "Client", IsServerSide ? "client" : "server"); //bytes = null; //not a real message, so set it to null IsConnectedResetEvent.Set(); Connected?.Invoke(this, Socket.RemoteEndPoint); } catch (Exception e) { //Log.e(e); System.Diagnostics.Debug.WriteLine(e.ToString()); System.Diagnostics.Debugger.Break(); } } }
/// <summary> /// Constructs a new SecureStringClient /// </summary> /// <param name="client">An existing connected TcpClient</param> public Client(TcpClient client) { IsServerSide = true; _client = new Sockets.Socket(client); IsConnectedResetEvent.Set(); InitializeConnection(); }
/// <summary> /// Called when a connection has been established with the server /// </summary> /// <param name="remoteEndPoint">The IPEndPoint of the server</param> protected override void OnConnected(IPEndPoint remoteEndPoint) { //don't call the base because we don't want to raise the Connected event yet //base.OnConnected(remoteEndPoint); IsConnectedResetEvent.Reset(); SendPublicKey(); //immediately send the public key //can't log messages yet - Log.v("Client Sent Public Key"); }
/// <summary> /// Blocks the current thread until either a connection is established or the specified time elapses /// </summary> /// <param name="msTimeout"></param> /// <returns></returns> public bool WaitForSecured(int msTimeout = -1) { return(IsConnectedResetEvent.Wait(msTimeout)); }
/// <summary> /// Called when a connection has been terminated /// </summary> /// <param name="remoteEndPoint">The IPEndPoint of the server</param> /// <param name="e">An exception if one ocurred</param> protected virtual void OnDisconnected(IPEndPoint remoteEndPoint, Exception e) { IsConnectedResetEvent.Reset(); Disconnected?.Invoke(this, remoteEndPoint, e); }
/// <summary> /// Called when a connection has been established with the server /// </summary> /// <param name="remoteEndPoint">The IPEndPoint of the server</param> protected virtual void OnConnected(IPEndPoint remoteEndPoint) { IsConnectedResetEvent.Set(); Connected?.Invoke(this, remoteEndPoint); }