protected sealed override IObservable <char> DataReceived() { return(Observable.Defer(async() => { var stream = _client.GetStream(); var buffer = new byte[1024]; var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length); var chars = Encoding.ASCII.GetChars(buffer); return chars.Take(readBytes).ToObservable(); })); }
private void SendMessage(int NumRetries, string str) { IrDAClient client = null; int CurrentTries = 0; do { try { client = new IrDAClient(ServiceName); } catch (Exception se) { if ((CurrentTries >= NumRetries)) { throw se; } } CurrentTries = CurrentTries + 1; } while (client == null & CurrentTries < NumRetries); if ((client == null)) { StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error establishing contact" }); return; } System.IO.Stream stream = null; try { stream = client.GetStream(); stream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length); StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Message sent!" }); txtMessagesArchive.Text = str + "\r\n" + txtMessagesArchive.Text; } catch (Exception e) { StatusBar1.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error sending message." }); } finally { if ((!(stream == null))) { stream.Close(); } if ((!(client == null))) { client.Close(); } } }
private string ReceiveMessage() { int bytesRead = 0; IrDAListener listener = new IrDAListener(ServiceName); IrDAClient client = null; System.IO.Stream stream = null; byte[] Buffer = new byte[MAX_MESSAGE_SIZE - 1]; string str = string.Empty; try { listener.Start(); //---blocking call--- client = listener.AcceptIrDAClient(); stream = client.GetStream(); bytesRead = stream.Read(Buffer, 0, Buffer.Length); //---format the received message--- str = ">" + System.Text.ASCIIEncoding.ASCII.GetString(Buffer, 0, bytesRead); } catch (SocketException ex) { //---ignore error--- } catch (Exception e) { txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus), new object[] { e.ToString() }); } finally { if ((!(stream == null))) { stream.Close(); } if ((!(client == null))) { client.Close(); } listener.Stop(); } return(str); }
private void SendMessage(int NumRetries, string str) { IrDAClient client = null; int CurrentTries = 0; //---try to establish a connection--- do { try { client = new IrDAClient(ServiceName); } catch (Exception se) { if ((CurrentTries >= NumRetries)) { throw se; } } CurrentTries = CurrentTries + 1; } while (client == null & CurrentTries < NumRetries); //---timeout occurred--- if ((client == null)) { txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error establishing contact" }); return; } //---send the message over a stream object--- System.IO.Stream stream = null; try { stream = client.GetStream(); stream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length); //---update the status bar--- txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Message sent!" }); //---display the message that was sent--- txtMessagesArchive.Text = str + Environment.NewLine + txtMessagesArchive.Text; } catch (Exception e) { txtMessagesArchive.BeginInvoke(new myDelegate(UpdateStatus), new object[] { "Error sending message." }); } finally { if ((!(stream == null))) { stream.Close(); } if ((!(client == null))) { client.Close(); } } }
private ObexStatusCode Connect() { if (!connected) { if (ns == null) { try { if (uri.Host.Length == 0) { System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream != null); System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream.CanRead && m_alreadyConnectedObexStream.CanWrite); ns = m_alreadyConnectedObexStream; } else { if (IrDAAddress.TryParse(uri.Host, out var address)) { IrDAClient client = new IrDAClient(); string serviceName; switch (uri.Scheme) { //potential for other obex based profiles to be added default: serviceName = IrDAService.ObjectExchange; break; } client.Connect(new IrDAEndPoint(address, serviceName)); if (!client.Connected) { return(ObexStatusCode.NotFound); } ns = client.GetStream(); } else if (BluetoothAddress.TryParse(uri.Host, out BluetoothAddress ba)) { BluetoothClient cli = new BluetoothClient(); Guid serviceGuid; switch (uri.Scheme) { case SchemeNames.Ftp: serviceGuid = BluetoothService.ObexFileTransfer; break; //potential for other obex based profiles to be added case SchemeNames.Sync: serviceGuid = BluetoothService.IrMCSyncCommand; break; case SchemeNames.Pbap: serviceGuid = BluetoothService.PhonebookAccessPse; break; case SchemeNames.Map: serviceGuid = BluetoothService.MessageAccessProfile; break; default: serviceGuid = BluetoothService.ObexObjectPush; break; } cli.Connect(ba, serviceGuid); if (!cli.Connected) { return(ObexStatusCode.NotFound); } ns = cli.GetStream(); } else { //assume a tcp host s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipa = null; var addresses = System.Net.Dns.GetHostAddresses(uri.Host); if (addresses.Length > 0) { ipa = addresses[0]; } else { throw new WebException("Host not recognised", WebExceptionStatus.NameResolutionFailure); } IPEndPoint ipep = new IPEndPoint(ipa, 650); s.Connect(ipep); } if (ns == null) // BluetoothClient used above { ns = new System.Net.Sockets.NetworkStream(s, true); } // Timeout //ns.ReadTimeout = timeout; //ns.WriteTimeout = timeout; } return(Connect_Obex()); } finally { if (s != null && !s.Connected) { s = null; } } } } Debug.Fail("Don't know that we every get here (Connect when connected)."); return((ObexStatusCode)0); }
/// <summary> /// Devuelve el Stream con el que nos comunicamos al dispositivo remoto /// </summary> /// <returns>El Stream</returns> public override Stream GetStream() { return(clienteIR.GetStream()); }