Example usage of this class - c#

I found this class on the internet but it didn't have any example usage. I tried reading it and the <events> are confusing me. When I try to initialize an event it stays null even after the irc.DataRead+=new EventHandler<DataReadEventArgs>(irc_DataRead);
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace asynchronous
{
/// <summary>
/// Represents an asynchronous Tcp Client.
/// </summary>
public class Client
{
/// <summary>
/// The default length for the read buffer.
/// </summary>
private const int DefaultClientReadBufferLength = 4096;
/// <summary>
/// The tcp client used for the outgoing connection.
/// </summary>
private readonly TcpClient client;
/// <summary>
/// The port to connect to on the remote server.
/// </summary>
private readonly int port;
/// <summary>
/// A reset event for use if a DNS lookup is required.
/// </summary>
private readonly ManualResetEvent dnsGetHostAddressesResetEvent = null;
/// <summary>
/// The length of the read buffer.
/// </summary>
private readonly int clientReadBufferLength;
/// <summary>
/// The addresses to try connection to.
/// </summary>
private IPAddress[] addresses;
/// <summary>
/// How many times to retry connection.
/// </summary>
private int retries;
/// <summary>
/// Occurs when the client connects to the server.
/// </summary>
public event EventHandler Connected;
/// <summary>
/// Occurs when the client disconnects from the server.
/// </summary>
public event EventHandler Disconnected;
/// <summary>
/// Occurs when data is read by the client.
/// </summary>
public event EventHandler<DataReadEventArgs> DataRead;
/// <summary>
/// Occurs when data is written by the client.
/// </summary>
public event EventHandler<DataWrittenEventArgs> DataWritten;
/// <summary>
/// Occurs when an exception is thrown during connection.
/// </summary>
public event EventHandler<ExceptionEventArgs> ClientConnectException;
/// <summary>
/// Occurs when an exception is thrown while reading data.
/// </summary>
public event EventHandler<ExceptionEventArgs> ClientReadException;
/// <summary>
/// Occurs when an exception is thrown while writing data.
/// </summary>
public event EventHandler<ExceptionEventArgs> ClientWriteException;
/// <summary>
/// Occurs when an exception is thrown while performing the DNS lookup.
/// </summary>
public event EventHandler<ExceptionEventArgs> DnsGetHostAddressesException;
/// <summary>
/// Constructor for a new client object based on a host name or server address string and a port.
/// </summary>
/// <param name="hostNameOrAddress">The host name or address of the server as a string.</param>
/// <param name="port">The port on the server to connect to.</param>
/// <param name="clientReadBufferLength">The clients read buffer length.</param>
public Client(string hostNameOrAddress, int port, int clientReadBufferLength = DefaultClientReadBufferLength)
: this(port, clientReadBufferLength)
{
this.dnsGetHostAddressesResetEvent = new ManualResetEvent(false);
Dns.BeginGetHostAddresses(hostNameOrAddress, this.DnsGetHostAddressesCallback, null);
}
/// <summary>
/// Constructor for a new client object based on a number of IP Addresses and a port.
/// </summary>
/// <param name="addresses">The IP Addresses to try connecting to.</param>
/// <param name="port">The port on the server to connect to.</param>
/// <param name="clientReadBufferLength">The clients read buffer length.</param>
public Client(IPAddress[] addresses, int port, int clientReadBufferLength = DefaultClientReadBufferLength)
: this(port, clientReadBufferLength)
{
this.addresses = addresses;
}
/// <summary>
/// Constructor for a new client object based on a single IP Address and a port.
/// </summary>
/// <param name="address">The IP Address to try connecting to.</param>
/// <param name="port">The port on the server to connect to.</param>
/// <param name="clientReadBufferLength">The clients read buffer length.</param>
public Client(IPAddress address, int port, int clientReadBufferLength = DefaultClientReadBufferLength)
: this(new[] { address }, port, clientReadBufferLength)
{
}
/// <summary>
/// Private constructor for a new client object.
/// </summary>
/// <param name="port">The port on the server to connect to.</param>
/// <param name="clientReadBufferLength">The clients read buffer length.</param>
private Client(int port, int clientReadBufferLength)
{
this.client = new TcpClient();
this.port = port;
this.clientReadBufferLength = clientReadBufferLength;
}
/// <summary>
/// Starts an asynchronous connection to the remote server.
/// </summary>
public void Connect()
{
if (this.dnsGetHostAddressesResetEvent != null)
this.dnsGetHostAddressesResetEvent.WaitOne();
this.retries = 0;
this.client.BeginConnect(this.addresses, this.port, this.ClientConnectCallback, null);
}
/// <summary>
/// Writes a string to the server using a given encoding.
/// </summary>
/// <param name="value">The string to write.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>A Guid that can be used to match the data written to the confirmation event.</returns>
public Guid Write(string value, Encoding encoding)
{
byte[] buffer = encoding.GetBytes(value);
return this.Write(buffer);
}
/// <summary>
/// Writes a byte array to the server.
/// </summary>
/// <param name="buffer">The byte array to write.</param>
/// <returns>A Guid that can be used to match the data written to the confirmation event.</returns>
public Guid Write(byte[] buffer)
{
Guid guid = Guid.NewGuid();
NetworkStream networkStream = this.client.GetStream();
networkStream.BeginWrite(buffer, 0, buffer.Length, this.ClientWriteCallback, guid);
return guid;
}
/// <summary>
/// Callback from the asynchronous DNS lookup.
/// </summary>
/// <param name="asyncResult">The result of the async operation.</param>
private void DnsGetHostAddressesCallback(IAsyncResult asyncResult)
{
try
{
this.addresses = Dns.EndGetHostAddresses(asyncResult);
this.dnsGetHostAddressesResetEvent.Set();
}
catch (Exception ex)
{
if (this.DnsGetHostAddressesException != null)
this.DnsGetHostAddressesException(this, new ExceptionEventArgs(ex));
}
}
/// <summary>
/// Callback from the asynchronous Connect method.
/// </summary>
/// <param name="asyncResult">The result of the async operation.</param>
private void ClientConnectCallback(IAsyncResult asyncResult)
{
try
{
this.client.EndConnect(asyncResult);
if (this.Connected != null)
this.Connected(this, new EventArgs());
}
catch (Exception ex)
{
retries++;
if (retries < 3)
{
this.client.BeginConnect(this.addresses, this.port, this.ClientConnectCallback, null);
}
else
{
if (this.ClientConnectException != null)
this.ClientConnectException(this, new ExceptionEventArgs(ex));
}
return;
}
try
{
NetworkStream networkStream = this.client.GetStream();
byte[] buffer = new byte[this.clientReadBufferLength];
networkStream.BeginRead(buffer, 0, buffer.Length, this.ClientReadCallback, buffer);
}
catch (Exception ex)
{
if (this.ClientReadException != null)
this.ClientReadException(this, new ExceptionEventArgs(ex));
}
}
/// <summary>
/// Callback from the asynchronous Read method.
/// </summary>
/// <param name="asyncResult">The result of the async operation.</param>
private void ClientReadCallback(IAsyncResult asyncResult)
{
try
{
NetworkStream networkStream = this.client.GetStream();
int read = networkStream.EndRead(asyncResult);
if (read == 0)
{
if (this.Disconnected != null)
this.Disconnected(this, new EventArgs());
}
byte[] buffer = asyncResult.AsyncState as byte[];
if (buffer != null)
{
byte[] data = new byte[read];
Buffer.BlockCopy(buffer, 0, data, 0, read);
networkStream.BeginRead(buffer, 0, buffer.Length, this.ClientReadCallback, buffer);
if (this.DataRead != null)
this.DataRead(this, new DataReadEventArgs(data));
}
}
catch (Exception ex)
{
if (this.ClientReadException != null)
this.ClientReadException(this, new ExceptionEventArgs(ex));
}
}
/// <summary>
/// Callback from the asynchronous write callback.
/// </summary>
/// <param name="asyncResult">The result of the async operation.</param>
private void ClientWriteCallback(IAsyncResult asyncResult)
{
try
{
NetworkStream networkStream = this.client.GetStream();
networkStream.EndWrite(asyncResult);
Guid guid = (Guid)asyncResult.AsyncState;
if (this.DataWritten != null)
this.DataWritten(this, new DataWrittenEventArgs(guid));
}
catch (Exception ex)
{
if (this.ClientWriteException != null)
this.ClientWriteException(this, new ExceptionEventArgs(ex));
}
}
}
/// <summary>
/// Provides data for an exception occuring event.
/// </summary>
public class ExceptionEventArgs : EventArgs
{
/// <summary>
/// Constructor for a new Exception Event Args object.
/// </summary>
/// <param name="ex">The exception that was thrown.</param>
public ExceptionEventArgs(Exception ex)
{
this.Exception = ex;
}
public Exception Exception { get; private set; }
}
/// <summary>
/// Provides data for a data read event.
/// </summary>
public class DataReadEventArgs : EventArgs
{
/// <summary>
/// Constructor for a new Data Read Event Args object.
/// </summary>
/// <param name="data">The data that was read from the remote host.</param>
public DataReadEventArgs(byte[] data)
{
this.Data = data;
}
/// <summary>
/// Gets the data that has been read.
/// </summary>
public byte[] Data { get; private set; }
}
/// <summary>
/// Provides data for a data write event.
/// </summary>
public class DataWrittenEventArgs : EventArgs
{
/// <summary>
/// Constructor for a Data Written Event Args object.
/// </summary>
/// <param name="guid">The guid of the data written.</param>
public DataWrittenEventArgs(Guid guid)
{
this.Guid = guid;
}
/// <summary>
/// Gets the Guid used to match the data written to the confirmation event.
/// </summary>
public Guid Guid { get; private set; }
}
}
This is what I've tried but I don't get it. I'm new to c#:
using System;
using System.Text;
using System.Threading;
namespace asynchronous
{
class Program
{
private static EventHandler connection;
private static EventHandler<DataReadEventArgs> irc_DataRead;
static void Main(string[] args)
{
var irc = new Client("irc.rizon.net", 6667);
Console.WriteLine("Connecting...");
irc.Connect();
Thread.Sleep(2000);
irc.Write("Test", Encoding.UTF8);
irc.DataRead+=new EventHandler<DataReadEventArgs>(irc_DataRead);
Console.WriteLine(irc_DataRead);
Console.WriteLine("Connected.");
Console.ReadLine();
}
}
}
Could someone please help me setup that class to connect to IRC and read and write text?

Here you are hooking up a null instance of an event handler into an event. You need to hookup an actual method / lambda in order for this to work. Try the following
static void OnDataRead(object sender, DataReadEventArgs e) {
// Data reads call this method
}
static void Main(string[] args) {
...
irc.DataRead += OnDataRead;
...
}

Related

Where is the python in this script being called/pulled from?

I found a project in Unity 5 where python can be run in Unity, and i want to add libraries to it. The trouble is, i can't seem to find where the python libraries from this script. To me, it looks like they're being generated out of thin air. Can i have a pointer as to where this might be coming from? I tried adding it to python path, and its not seeming to work.
using System;
using System.IO;
using System.Linq;
using UnityEngine;
using System.Text;
using System.Collections;
using IronPython.Hosting;
using IronPython.Modules;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;
/// <summary>
/// Interpreter for IronPython.
/// </summary>
public class Interpreter
{
/// <summary>
/// The scope.
/// </summary>
private ScriptScope Scope;
/// <summary>
/// The engine.
/// </summary>
private ScriptEngine Engine;
/// <summary>
/// The source.
/// </summary>
private ScriptSource Source;
/// <summary>
/// The compiled.
/// </summary>
private CompiledCode Compiled;
/// <summary>
/// The operation.
/// </summary>
private ObjectOperations Operation;
/// <summary>
/// The python class.
/// </summary>
private object PythonClass;
/// <summary>
/// Initializes a new instance of the <see cref="Interpreter"/> class.
/// </summary>
public Interpreter()
{
Engine = Python.CreateEngine();
Scope = Engine.CreateScope();
SetLibrary();
}
/// <summary>
/// Initializes a new instance of the <see cref="Interpreter"/> class.
/// </summary>
/// <param name="source">Source.</param>
public Interpreter(string src) : this()
{
Compile(src);
}
/// <summary>
/// Compile the specified src.
/// </summary>
/// <param name="src">Source.</param>
public string Compile(string src, Microsoft.Scripting.SourceCodeKind CodeKind =
Microsoft.Scripting.SourceCodeKind.SingleStatement)
{
if(src == string.Empty)
return string.Empty;
LoadRuntime();
Source = CodeKind == Microsoft.Scripting.SourceCodeKind.SingleStatement ?
Engine.CreateScriptSourceFromString(src, CodeKind) :
Engine.CreateScriptSourceFromFile(src);
ErrorHandle errors = new ErrorHandle();
MemoryStream stream = new MemoryStream();
//Set IO Ouput of execution
Engine.Runtime.IO.SetOutput(stream, new StreamWriter(stream));
Compiled = Source.Compile(errors);
Operation = Engine.CreateOperations();
try {
Compiled.Execute(Scope);
return FormatOutput(ReadFromStream(stream));
} catch(Exception ex) {
return Engine.GetService<ExceptionOperations>().FormatException(ex);
}
}
/// <summary>
/// Formats the output of execution
/// </summary>
/// <returns>The output.</returns>
/// <param name="output">Output.</param>
private string FormatOutput(string output)
{
return string.IsNullOrEmpty(output) ? string.Empty
: string.Join("\n", output.Remove(output.Length-1)
.Split('\n')
.Reverse().ToArray());
}
/// <summary>
/// Reads MemoryStream.
/// </summary>
/// <returns>The from stream.</returns>
/// <param name="ms">Ms.</param>
private string ReadFromStream(MemoryStream ms) {
int length = (int)ms.Length;
Byte[] bytes = new Byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(bytes, 0, length);
return Encoding.GetEncoding("utf-8").GetString(bytes, 0, length);
}
/// <summary>
/// Set sys paths
/// </summary>
private void SetLibrary()
{
if(PythonBase.SysPath.Count > 0) {
ICollection<string> SysPath = Engine.GetSearchPaths();
foreach(string path in PythonBase.SysPath)
SysPath.Add(path);
Engine.SetSearchPaths(SysPath);
}
}
/// <summary>
/// Load runtime Assemblies of Unity3D
/// </summary>
private void LoadRuntime()
{
Engine.Runtime.LoadAssembly(typeof(GameObject).Assembly);
}
public void AddRuntime<T>()
{
Engine.Runtime.LoadAssembly(typeof(T).Assembly);
}
public void AddRuntime(Type type)
{
Engine.Runtime.LoadAssembly(type.Assembly);
}
/// <summary>
/// Gets the variable or class
/// </summary>
/// <returns>The variable.</returns>
/// <param name="name">Name.</param>
public object GetVariable(string name)
{
return Operation.Invoke(Scope.GetVariable(name));
}
/// <summary>
/// Calls the method.
/// </summary>
/// <param name="name">Name.</param>
public void InvokeMethod(object nameClass, string Method, params object[] parameters)
{
object output = new object();
if(Operation.TryGetMember(nameClass, Method, out output)) {
object Func = Operation.GetMember(nameClass, Method);
Operation.Invoke(Func, parameters);
}
}
}

Unable to connect to a TCPClient on another computer

So I have two laptops connected to the same wifi network with one running a very simple server and the other a very simple client connecting to it. When I run both the server and the client on one laptop they connect without a problem, but when running one on each laptop the client cannot connect to the server.
The code for the server is this:
using System;
using System.Net;
using System.Net.Sockets;
namespace Utils
{
/// <summary>
/// A base server which handles listening for client connections and has simple API to communicate back and forth
/// </summary>
public class BaseServer
{
#region Properties and Fields
/// <summary>
/// The listener we can use to detect incoming connections from clients to the server
/// </summary>
private TcpListener Listener { get; set; }
/// <summary>
/// Our interface to the single client we are supporting for now
/// </summary>
public Comms ClientComms { get; private set; }
/// <summary>
/// Determines whether we have clients connected
/// </summary>
public bool Connections { get; private set; }
#endregion
public BaseServer()
{
Listener = new TcpListener(IPAddress.Any, 1490);
Listener.Start();
ListenForNewClient();
}
/// <summary>
/// Starts an asynchronous check for new connections
/// </summary>
private void ListenForNewClient()
{
Listener.BeginAcceptTcpClient(AcceptClient, null);
}
/// <summary>
/// Callback for when a new client connects to the server
/// </summary>
/// <param name="asyncResult"></param>
protected virtual void AcceptClient(IAsyncResult asyncResult)
{
ClientComms = new Comms(Listener.EndAcceptTcpClient(asyncResult));
ClientComms.OnDataReceived += ProcessMessage;
ListenForNewClient();
}
#region Message Callbacks
/// <summary>
/// A function which is called when the Client sends a message to the server.
/// Override to perform custom message handling
/// </summary>
/// <param name="data"></param>
protected virtual void ProcessMessage(byte[] data) { }
#endregion
}
}
And the code for the client is this:
using System;
using System.Net.Sockets;
namespace Utils
{
/// <summary>
/// A base client class which connects and communicates with a remote server
/// </summary>
public class BaseClient
{
#region Properties and Fields
/// <summary>
/// The interface to the server
/// </summary>
public Comms ServerComms { get; private set; }
#endregion
public BaseClient(string ipAddress, int portNumber = 1490)
{
// Attempt to connect
try
{
ServerComms = new Comms(new TcpClient(ipAddress, portNumber));
ServerComms.OnDataReceived += OnMessageReceived;
ServerComms.OnDisconnect += OnServerDisconnect;
}
catch (Exception e)
{
Console.WriteLine("Connection failed");
}
}
#region Callbacks
/// <summary>
/// A function which is called when this client receives a message.
/// Override to perform behaviour when custom messages arrive.
/// </summary>
/// <param name="data"></param>
protected virtual void OnMessageReceived(byte[] data) { }
/// <summary>
/// A function called when this client can no longer communicate to the server it is connected to
/// </summary>
protected virtual void OnServerDisconnect() { }
#endregion
}
}
The server is started from the main loop like this:
using System;
namespace BuildServer
{
class Program
{
static void Main(string[] args)
{
BaseServer server = new BaseServer();
while (true)
{
}
}
}
}
and the Client is started like this:
using System;
using Utils;
namespace BuildServerClient
{
class Program
{
static void Main(string[] args)
{
BaseClient client = new BaseClient();
while (true)
{
Console.WriteLine("Ready");
string message = Console.ReadLine();
client.ServerComms.Send(message);
}
}
}
}
One final class is a Comms class which is really a wrapper around the TCPClient and not really used currently, but I am adding it for the same of completeness.
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using static Utils.Delegates;
namespace Utils
{
/// <summary>
/// An interface to a client.
/// Hides the nuts and bolts and provides a public interface of just data input and output from a data sender/receiver.
/// </summary>
public class Comms
{
#region Properties and Fields
private TcpClient Client { get; set; }
private MemoryStream ReadStream { get; set; }
private MemoryStream WriteStream { get; set; }
private BinaryReader Reader { get; set; }
private BinaryWriter Writer { get; set; }
/// <summary>
/// Useful buffer for reading packeted messages from the server
/// </summary>
private byte[] ReadBuffer { get; set; }
/// <summary>
/// An event that is fired when this Comms receives a message
/// </summary>
public event OnDataReceived OnDataReceived;
/// <summary>
/// An event that is fired when this Comms can no longer communicate with the client sending it messages
/// </summary>
public event OnDisconnect OnDisconnect;
#endregion
public Comms(TcpClient client)
{
Client = client;
ReadStream = new MemoryStream();
WriteStream = new MemoryStream();
Reader = new BinaryReader(ReadStream);
Writer = new BinaryWriter(WriteStream);
ReadBuffer = new byte[2048];
Client.NoDelay = true;
StartListening();
}
#region Data Sending Functions
/// <summary>
/// Convert a string to a byte array and then send to our client
/// </summary>
/// <param name="client"></param>
/// <param name="str"></param>
public void Send(string str)
{
SendByteArray(Encoding.UTF8.GetBytes(str));
}
/// <summary>
/// Send a byte array to our client
/// </summary>
/// <param name="client"></param>
/// <param name="bytes"></param>
protected void SendByteArray(byte[] bytes)
{
Writer.Write(bytes);
int bytesWritten = (int)WriteStream.Position;
byte[] result = new byte[bytesWritten];
WriteStream.Position = 0;
WriteStream.Read(result, 0, bytesWritten);
WriteStream.Position = 0;
Client.GetStream().BeginWrite(result, 0, result.Length, null, null);
Writer.Flush();
}
#endregion
#region Data Receiving Functions
/// <summary>
/// Start listening for messages from the server
/// </summary>
private void StartListening()
{
try
{
Client.GetStream().BeginRead(ReadBuffer, 0, 2048, StreamReceived, null);
}
catch
{
OnDisconnect?.Invoke();
}
}
/// <summary>
/// Callback which processes a message sent from the server
/// </summary>
/// <param name="ar"></param>
private void StreamReceived(IAsyncResult ar)
{
int bytesRead = 0;
try
{
lock (Client.GetStream())
{
bytesRead = Client.GetStream().EndRead(ar);
}
}
catch { }
//Create the byte array with the number of bytes read
byte[] data = new byte[bytesRead];
//Populate the array
for (int i = 0; i < bytesRead; i++)
{
data[i] = ReadBuffer[i];
}
OnDataReceived?.Invoke(data);
//Listen for new data
StartListening();
}
#endregion
}
}
The IP Addresses and Port numbers are correct and since it works when running on the same machine, I was thinking it might be a firewall issue or something? Does anyone have any ideas as to what might be causing this problem?
Make sure the firewall (Windows Firewall, or whatever's there) is turned off on the server machine, or there's a firewall exception for your port number.
David's answer was correct. I previously tried it with just the firewall disabled for private networks. However, I disabled the firewall for guest and public networks and it worked a treat.
The method of testing proposed by codenoir was also very effective at ruling out my client. I suspected it was something to do with the firewall, but once you rule out the impossible...
Thanks to both

About Softphone C#

I am a electronic student, I am developing a GUI with Visual Studio; so I found examples and information in the next page.
http://www.voip-sip-sdk.com/
my principal class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Ozeki.Media;
using Ozeki.VoIP;
using Ozeki.Common;
namespace Consola2
{
/// <summary>
/// Basic softphone logic.
/// </summary>
/// <remarks>
/// This class is used to introduce how to declare, define and initialize a softphone,
/// how to handle some of it's events, and use some of it's functions.
/// The Program.cs uses this class to create a softphone,
/// uses the functions and events declared here as public.
/// </remarks>
class Softphone
{
ISoftPhone _softphone; // softphone object
IPhoneLine _phoneLine; // phone line object
IPhoneCall _call; // the call object
Microphone _microphone;
Speaker _speaker;
MediaConnector _connector; // connects the devices to each other (eg. microphone, speaker, mediaSender, mediaReceiver)
PhoneCallAudioSender _mediaSender; // after connected with the microphone, this will be attached to the call
PhoneCallAudioReceiver _mediaReceiver; // after connected with the speaker, this will be attached to the call
bool _incomingCall; // indicates wheter we have an incoming call (so, the phone is ringing)
#region Events
/// <summary>
/// Occurs when an incoming call received.
/// </summary>
public event EventHandler IncomingCall;
/// <summary>
/// Occurs when the registration state of the phone line has changed.
/// </summary>
public event EventHandler<RegistrationStateChangedArgs> PhoneLineStateChanged;
/// <summary>
/// Occurs when the state of the call has changed.
/// </summary>
public event EventHandler<CallStateChangedArgs> CallStateChanged;
#endregion
/// <summary>
/// Default constructor, initalizes the softphone with deafult parameters.
/// </summary>
public Softphone()
{
_softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
_microphone = Microphone.GetDefaultDevice();
_speaker = Speaker.GetDefaultDevice();
_connector = new MediaConnector();
_mediaSender = new PhoneCallAudioSender();
_mediaReceiver = new PhoneCallAudioReceiver();
_incomingCall = false;
}
/// <summary>
/// Registers the SIP account to the PBX.
/// Calls cannot be made while the SIP account is not registered.
/// If the SIP account requires no registration, the RegisterPhoneLine() must be called too to register the SIP account to the ISoftPhone.
/// </summary>
public void Register(bool registrationRequired, string displayName, string userName, string authenticationId, string registerPassword, string domainHost, int domainPort)
{
try
{
// We need to handle the event, when we have an incoming call.
_softphone.IncomingCall += softphone_IncomingCall;
// To register to a PBX, we need to create a SIP account
var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
//Console.WriteLine("\nCreating SIP account {0}", account);
// With the SIP account and the NAT configuration, we can create a phoneline.
_phoneLine = _softphone.CreatePhoneLine(account);
//Console.WriteLine("Phoneline created.");
// The phoneline has states, we need to handle the event, when it is being changed.
_phoneLine.RegistrationStateChanged += phoneLine_PhoneLineStateChanged;
// If our phoneline is created, we can register that.
_softphone.RegisterPhoneLine(_phoneLine);
// For further information about the calling of the ConnectMedia(), please check the implementation of this method.
ConnectMedia();
}
catch (Exception ex)
{
Console.WriteLine("Error during SIP registration: " + ex);
}
}
/// <summary>
/// This will be called when the registration state of the phone line has changed.
/// </summary>
private void phoneLine_PhoneLineStateChanged(object sender, RegistrationStateChangedArgs e)
{
DispatchAsync(() =>
{
var handler = PhoneLineStateChanged;
if (handler != null)
handler(this, e);
});
}
/// <summary>
/// Starts the capturing and playing audio/video devices.
/// Other devices can be used (and started), for example: WebCamera or WaveStreamPlayback.
/// </summary>
private void StartDevices()
{
if (_microphone != null)
{
_microphone.Start();
}
if (_speaker != null)
{
_speaker.Start();
}
}
/// <summary>
/// Stops the capturing and playing audio/video devices.
/// Other devices can be stopped, for example: WebCamera.
/// </summary>
private void StopDevices()
{
if (_microphone != null)
{
_microphone.Stop();
}
if (_speaker != null)
{
_speaker.Stop();
}
}
#region Media handling guide
/*
To send our voice through the microphone to the other client's speaker, we need to connect them.
We send our voice through the mediaSender, and we get the other client's voice through the mediaSender to our speaker object.
To disconnect these handlers, we will use the DisconnectMedia() method.
It is possible to use other mediahandlers with the connector, for example we can connect a WaveStreamPlayback or an MP3StreamPlayback object to the MediaSender, so we can play music/voice
during the call. For exmaple: when can create an IVR (Interactive Voice Response), we can create voice recorder etc.
For example:
We can connect an .mp3 file player (which plays an mp3 file into the voice call) by the "connector.Connect(Mp3StreamPlayback, mediaSender); " line.
(We should also create an MP3StreamPlayback object: "MP3StreamPlayback Mp3StreamPlayback; "
and we need to tell to this object the details (what to play into the speaker, etc.))
*/
#endregion
/// <summary>
/// Connects the audio handling devices to each other.
/// The audio data will flow from the source to the destination.
/// </summary>
private void ConnectMedia()
{
if (_microphone != null)
{
_connector.Connect(_microphone, _mediaSender);
}
if (_speaker != null)
{
_connector.Connect(_mediaReceiver, _speaker);
}
}
/// <summary>
/// Disconnects the audio handling devices from each other.
/// </summary>
private void DisconnectMedia()
{
if (_microphone != null)
{
_connector.Disconnect(_microphone, _mediaSender);
}
if (_speaker != null)
{
_connector.Disconnect(_mediaReceiver, _speaker);
}
// You can close all of the connections by using: connector.Dispose();
}
/// <summary>
/// Subscribes to the events of a call to receive notifications such as the state of the call has changed.
/// In this sample subscribes only to the state changed and error occurred events.
/// </summary>
private void WireUpCallEvents()
{
_call.CallStateChanged += (call_CallStateChanged);
}
/// <summary>
/// Unsubscribes from the events of a call.
/// </summary>
private void WireDownCallEvents()
{
_call.CallStateChanged -= (call_CallStateChanged);
}
/// <summary>
/// This will be called when an incoming call received.
/// To receive notifications from the call (eg. ringing), the program need to subscribe to the events of the call.
/// </summary>
private void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
{
_call = e.Item;
WireUpCallEvents();
_incomingCall = true;
DispatchAsync(() =>
{
var handler = IncomingCall;
if (handler != null)
handler(this, EventArgs.Empty);
});
}
/// <summary>
/// This will be called when the state of the call call has changed.
/// </summary>
/// <remarks>
/// In this sample only three states will be handled: Answered, InCall, Ended
///
/// Answered: when the call has been answered, the audio devices will be started and attached to the call.
/// It is required to comminicate with the other party and hear them.
/// The devices are connected at softphone initialization time,
/// so no need to connect them every time when a call is being answered.
///
/// InCall: when the call is in an active state, the audio deveices will be started.
///
/// Ended: when the call ends, the audio devices will be stopped and detached from the call.
/// </remarks>
private void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
// the call has been answered
if (e.State == CallState.Answered)
{
StartDevices();
_mediaReceiver.AttachToCall(_call);
_mediaSender.AttachToCall(_call);
}
// the call is in active communication state
// IMPORTANT: this state can occur multiple times. for example when answering the call or the call has been taken off hold.
if (e.State == CallState.InCall)
{
StartDevices();
}
// the call has ended
if (e.State.IsCallEnded())
{
if (_call != null)
{
CallFinished();
}
}
DispatchAsync(() =>
{
var handler = CallStateChanged;
if (handler != null)
handler(this, e);
});
}
/// <summary>
/// Starts calling the specified number.
/// In this sample an outgoing call can be made if there is no current call (outgoing or incoming) on the phone line.
/// </summary>
public void StartCall(string numberToDial)
{
if (_call == null)
{
_call = _softphone.CreateCallObject(_phoneLine, numberToDial);
WireUpCallEvents();
// To make a call simply call the Start() method of the call object.
_call.Start();
}
}
/// <summary>
/// Answers the current incoming call.
/// </summary>
public void AcceptCall()
{
// when the value of the incomingCall member is true, there is an incoming call
if (_incomingCall == true)
{
_incomingCall = false;
***_call.Answer();***
}
}
/// <summary>
/// Hangs up the current call.
/// </summary>
public void HangUp()
{
if (_call != null)
{
_call.HangUp();
_call = null;
}
}
/// <summary>
/// If the call ends, we won't need our speaker and microphone anymore to communicate,
/// until we enter into a call again, so we are calling the StopDevices() method.
/// The mediaHandlers are getting detached from the call object
/// (since we are not using our microphone and speaker, we have no media to send).
/// We won't need the call's events anymore, becouse our call is about to be ended,
/// and with setting the call to null, we are ending it.
/// </summary>
public void CallFinished()
{
StopDevices();
_mediaReceiver.Detach();
_mediaSender.Detach();
WireDownCallEvents();
_call = null;
}
/// <summary>
/// This method is used to solve the task blockings.
/// </summary>
private void DispatchAsync(Action action)
{
var task = new WaitCallback(o => action.Invoke());
ThreadPool.QueueUserWorkItem(task);
}
}
}
My GUI code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Ozeki.VoIP;
namespace Consola2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Softphone _mySoftphone; // softphone object
/// <summary>
/// The entry point of the program.
/// - initializes the softphone
/// - shows a greeting message
/// - registers the SIP account
/// </summary>
///
private static void InitSoftphone()
{
_mySoftphone = new Softphone();
_mySoftphone.PhoneLineStateChanged += mySoftphone_PhoneLineStateChanged;
_mySoftphone.CallStateChanged += mySoftphone_CallStateChanged;
_mySoftphone.IncomingCall += mySoftphone_IncomingCall;
}
/// <summary>
/// This will be called when the registration state of the phone line has changed.
/// </summary>
static void mySoftphone_PhoneLineStateChanged(object sender, RegistrationStateChangedArgs e)
{
Console.WriteLine("Phone line state changed: {0}", e.State);
if (e.State == RegState.Error || e.State == RegState.NotRegistered)
{
MessageBox.Show("Datos Invalidos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (e.State == RegState.RegistrationSucceeded)
{
MessageBox.Show("¡Registro Exitoso!");
//StartToDial();
}
}
/// <summary>
/// This will be called when an incoming call received.
/// In this sample when an incoming call receveived, it will be answered automatically.
/// </summary>
static void mySoftphone_IncomingCall(object sender, EventArgs e)
{
MessageBox.Show("Llamada entrante");
_mySoftphone.AcceptCall();
MessageBox.Show("Llamada aceptada:");
}
/// <summary>
/// This will be called when the state of the call has changed. (eg. ringing, answered, rejected)
/// </summary>
private static void mySoftphone_CallStateChanged(object sender, CallStateChangedArgs e)
{
Console.WriteLine("Call state changed: {0}", e.State);
if (e.State.IsCallEnded())
{
MessageBox.Show("Llamada terminada:");
}
if (e.State == CallState.Error)
{
Console.WriteLine("Call error occured. {0}", e.Reason);
}
}
public MainWindow()
{
InitializeComponent();
InitSoftphone();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string id = textBox1.Text;
string user = textBox2.Text;
string displayed = textBox3.Text;
string pass = textBox4.Text;
string domain = textBox5.Text;
int port = Convert.ToInt32(textBox6.Text);
bool check = false;
if (checkBox1.IsChecked == true)
{
check = true;
}
_mySoftphone.Register(check, id, user, displayed, pass, domain, port);
}
private void Llamar_Click(object sender, RoutedEventArgs e)
{
string numero = textBox7.Text;
_mySoftphone.StartCall(numero);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
_mySoftphone.AcceptCall();
}
}
}
With that, I can register into a PBX and make calls , the problem is that when you receive calls an error message appears "NullReferenceException was unhandled" In my class principal "AcceptCall()" method in the line "_call,Answer();".
Where is the problem? i'm not sure about de references with the "Ozeki" library.
I appreciate your help, or an example to make and receive calls.
Thanks.
Your call button (Llamar) instantiates _call inside StartCall. However, when AcceptCall is run, _call is still null, and cannot be used.

Define timeout to specific line in C# WPF application while running command prompt .exe application

in my main WPF application code i need to run .exe app with command prompt. this action is executed inside backgroundworker i have the following code. the code is running readlines.exe app with command prompt and read the output lines into a string (str).
string str;
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.UseShellExecute = true;
proc.FileName = #"readlines.exe";
proc.Arguments = #"";
proc.UseShellExecute = false;
proc.RedirectStandardOutput = true;
proc.CreateNoWindow = true;
proc.RedirectStandardInput = true;
Process proc1 = Process.Start(proc);
proc1.StandardInput.WriteLine("");
str = proc1.StandardOutput.ReadToEnd();
i want to ad timeout to the below line so when the timeout will be finised the procces will be canceled (as CTR+C) and "str" will get the output text until this point.
str = proc1.StandardOutput.ReadToEnd();
is it possible?
Although the previous answer has already been accepted here is a maybe more useful, secure and performant solution. Further it does not make use of the ReadLine() method which would block until there has been one line written (which may never occur). It uses an instance of StringBuilder and reads from the stream in specifyable data blocks (default size is 128 characters). Furthermore it supports event based notification of read data.
The usage of the class stays the same.
ProcessOutputReader por = new ProcessOutputReader(proc1);
por.StartReading();
// Do whatever you want here
// (e.g. sleep or whatever)
por.StopReading();
// Now you have everything that has been read in por.Data
However I've added the OnDataRead event which is fired every time new data has been read. You can access the data by using e.g. following code:
...
// Subscribe to the event
por.OnDataRead += OnDataReadEventHandler;
...
The callback method / event handler would look something like this:
private void OnDataReadEventHandler(object sender, ProcessOutputReaderEventArgs e)
{
// e.IntermediateDataStore points to the StringBuilder instance which holds
// all the data that has been received until now.
string completeData = e.IntermediateDataStore.ToString();
// e.NewData points to a string which contains the data that has been received
// since the last triggered event (because the event is triggered on each read).
string newData = e.NewData;
}
The modified ProcessOutputReader class looks like this:
/// <summary>
/// Represents the ProcessOutputReader class.
/// </summary>
public class ProcessOutputReader
{
/// <summary>
/// Represents the instance of the thread arguments class.
/// </summary>
private ProcessOutputReaderWorkerThreadArguments threadArguments;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessOutputReader"/> class.
/// </summary>
/// <param name="process">The process which's output shall be read.</param>
/// <exception cref="System.ArgumentOutOfRangeException">Is thrown if the specified process reference is null.</exception>
public ProcessOutputReader(Process process)
{
if (process == null)
{
throw new ArgumentOutOfRangeException("process", "The parameter \"process\" must not be null");
}
this.Process = process;
this.IntermediateDataStore = new StringBuilder();
this.threadArguments = new ProcessOutputReaderWorkerThreadArguments(this.Process, this.IntermediateDataStore);
}
/// <summary>
/// Is fired whenever data has been read from the process output.
/// </summary>
public event EventHandler<ProcessOutputReaderEventArgs> OnDataRead;
/// <summary>
/// Gets or sets the worker thread.
/// </summary>
private Thread ReaderThread
{
get;
set;
}
/// <summary>
/// Gets or sets the intermediate data store.
/// </summary>
private StringBuilder IntermediateDataStore
{
get;
set;
}
/// <summary>
/// Gets the data collected from the process output.
/// </summary>
public string Data
{
get
{
return this.IntermediateDataStore.ToString();
}
}
/// <summary>
/// Gets the process.
/// </summary>
public Process Process
{
get;
private set;
}
/// <summary>
/// Stars reading from the process output.
/// </summary>
public void StartReading()
{
if (this.ReaderThread != null)
{
if (this.ReaderThread.IsAlive)
{
return;
}
}
this.ReaderThread = new Thread(new ParameterizedThreadStart(ReaderWorker));
this.threadArguments.Exit = false;
this.ReaderThread.Start(this.threadArguments);
}
/// <summary>
/// Stops reading from the process output.
/// </summary>
public void StopReading()
{
if (this.ReaderThread != null)
{
if (this.ReaderThread.IsAlive)
{
this.threadArguments.Exit = true;
this.ReaderThread.Join();
}
}
}
/// <summary>
/// Fires the OnDataRead event.
/// </summary>
/// <param name="newData">The new data that has been read.</param>
protected void FireOnDataRead(string newData)
{
if (this.OnDataRead != null)
{
this.OnDataRead(this, new ProcessOutputReaderEventArgs(this.IntermediateDataStore, newData));
}
}
/// <summary>
/// Represents the worker method.
/// </summary>
/// <param name="data">The thread arguments, must be an instance of the <see cref="ProcessOutputReaderWorkerThreadArguments"/> class.</param>
private void ReaderWorker(object data)
{
ProcessOutputReaderWorkerThreadArguments args;
try
{
args = (ProcessOutputReaderWorkerThreadArguments)data;
}
catch
{
return;
}
try
{
char[] readBuffer = new char[args.ReadBufferSize];
while (!args.Exit)
{
if (args.Process == null)
{
return;
}
if (args.Process.HasExited)
{
return;
}
if (args.Process.StandardOutput.EndOfStream)
{
return;
}
int readBytes = this.Process.StandardOutput.Read(readBuffer, 0, readBuffer.Length);
args.IntermediateDataStore.Append(readBuffer, 0, readBytes);
this.FireOnDataRead(new String(readBuffer, 0, readBytes));
}
}
catch (ThreadAbortException)
{
if (!args.Process.HasExited)
{
args.Process.Kill();
}
}
}
}
In addition you need the ProcessOutputReaderWorkerThreadArguments class which looks like this:
/// <summary>
/// Represents the ProcessOutputReaderWorkerThreadArguments class.
/// </summary>
public class ProcessOutputReaderWorkerThreadArguments
{
/// <summary>
/// Represents the read buffer size,
/// </summary>
private int readBufferSize;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessOutputReaderWorkerThreadArguments"/> class.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="intermediateDataStore">The intermediate data store.</param>
public ProcessOutputReaderWorkerThreadArguments(Process process, StringBuilder intermediateDataStore)
{
this.ReadBufferSize = 128;
this.Exit = false;
this.Process = process;
this.IntermediateDataStore = intermediateDataStore;
}
/// <summary>
/// Gets or sets a value indicating whether the thread shall exit or not.
/// </summary>
public bool Exit
{
get;
set;
}
/// <summary>
/// Gets or sets the read buffer size in bytes.
/// </summary>
/// <exception cref="System.ArgumentOutOfRangeException">Is thrown if the specified value is not greather than 0.</exception>
public int ReadBufferSize
{
get
{
return this.readBufferSize;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException("value", "The specified value for \"ReadBufferSize\" must be greater than 0.");
}
this.readBufferSize = value;
}
}
/// <summary>
/// Gets the process.
/// </summary>
public Process Process
{
get;
private set;
}
/// <summary>
/// Gets the intermediate data store.
/// </summary>
public StringBuilder IntermediateDataStore
{
get;
private set;
}
}
And the ProcessOutputReaderEventArgs class which looks like this:
/// <summary>
/// Represents the ProcessOutputReaderEventArgs class.
/// </summary>
public class ProcessOutputReaderEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="ProcessOutputReaderEventArgs"/> class.
/// </summary>
/// <param name="intermediateDataStore">The reference to the intermediate data store.</param>
/// <param name="newData">The new data that has been read.</param>
public ProcessOutputReaderEventArgs(StringBuilder intermediateDataStore, string newData)
{
this.IntermediateDataStore = intermediateDataStore;
this.NewData = newData;
}
/// <summary>
/// Gets the reference to the intermediate data store.
/// </summary>
public StringBuilder IntermediateDataStore
{
get;
private set;
}
/// <summary>
/// Gets the new data that has been read.
/// </summary>
public string NewData
{
get;
private set;
}
}
Some example how to achieve this (attention, code not tested and can be improved)
ProcessOutputReader por = new ProcessOutputReader(proc1);
por.StartReading();
// Do whatever you want here
// (e.g. sleep or whatever)
por.StopReading();
// Now you have everything that has been read in por.Lines
The class would look like:
public class ProcessOutputReader
{
public ProcessOutputReader(Process process)
{
this.Process = process;
this.Lines = new List<string>();
}
public List<string> Lines
{
get;
private set;
}
public Process Process
{
get;
private set;
}
private Thread ReaderThread
{
get;
set;
}
public void StartReading()
{
if (this.ReaderThread == null)
{
this.ReaderThread = new Thread(new ThreadStart(ReaderWorker));
}
if (!this.ReaderThread.IsAlive)
{
this.ReaderThread.Start();
}
}
public void StopReading()
{
if (this.ReaderThread != null)
{
if (this.ReaderThread.IsAlive)
{
this.ReaderThread.Abort();
this.ReaderThread.Join();
}
}
}
private void ReaderWorker()
{
try
{
while (!this.Process.HasExited)
{
string data = this.Process.StandardOutput.ReadLine();
this.Lines.Add(data);
}
}
catch (ThreadAbortException)
{
if (!this.Process.HasExited)
{
this.Process.Kill();
}
}
}
}

How can I invoke a method within another process instance of my WinForms app?

I am working on an application that uses a Mutex to ensure that it is the only instance of the application running on the system.
When another instance of the application attempts to start, I want a method to run in the original instance.
Can I invoke a specific method in my application from another instance of the application?
I've found some examples using RegisterWindowMessage/PostMessage Win32 APIs by sending the message to HWND_BROADCAST, but I couldn't get them to work, and I've read elsewhere that using HWND_BROADCAST can be dangerous.
Is there a better way to do this that doesn't involve the app needing to be run in privileged mode?
Here's a little helper I wrote.
To use it:
var pipeListener = new NamedPipeListener<String>(); // instantiate an instance
pipeListener.MessageReceived += (sender, e) => MessageBox.Show(e.Message); // when a message is received, show a messagebox with the message
pipeListener.Error += (sender, e) => MessageBox.Show("Error ({0}): {1}", e.ErrorType, e.Exception.Message); // oh noes!
pipeListener.Start(); // when you're ready, start listening
From another process:
NamedPipeListener<String>.SendMessage("Howdy howdy howdy");
Note that it uses the full name of the PipeListener as the default name of the pipe. If you need to get more discrete than that, use the constructor overload that takes a pipe name.
Here's the class:
using System;
using System.IO.Pipes;
using System.Runtime.Serialization.Formatters.Binary;
namespace FunWithNamedPipes
{
/// <summary>Contains event data for <see cref="NamedPipeMessageReceiveHandler{TMessage}" /> events.</summary>
/// <typeparam name="TMessage"></typeparam>
public class NamedPipeListenerMessageReceivedEventArgs<TMessage> : EventArgs
{
/// <summary>Initializes an instance of <see cref="NamedPipeListenerMessageReceivedEventArgs{TMessage}" /> with the specified <paramref name="message" />.</summary>
/// <param name="message">The message passed by the event.</param>
public NamedPipeListenerMessageReceivedEventArgs(TMessage message)
{
this.Message = message;
}
/// <summary>Gets the message passed by the event.</summary>
public TMessage Message { get; private set; }
}
/// <summary>Contains event data for <see cref="NamedPipeListenerErrorEventHandler" /> events.</summary>
public class NamedPipeListenerErrorEventArgs : EventArgs
{
/// <summary>Initializes an instance of <see cref="NamedPipeListenerErrorEventArgs" /> with the specified <paramref name="errorType" /> and <paramref name="exception" />.</summary>
/// <param name="errorType">A <see cref="NamedPipeListenerErrorType" /> describing the part of the listener process where the error was caught.</param>
/// <param name="ex">The <see cref="Exception" /> that was thrown.</param>
public NamedPipeListenerErrorEventArgs(NamedPipeListenerErrorType errorType, Exception ex)
{
this.ErrorType = errorType;
this.Exception = ex;
}
/// <summary>Gets a <see cref="NamedPipeListenerErrorType" /> describing the part of the listener process where the error was caught.</summary>
public NamedPipeListenerErrorType ErrorType { get; private set; }
/// <summary>Gets the <see cref="Exception" /> that was caught.</summary>
public Exception Exception { get; private set; }
}
/// <summary>Represents a method that will handle an event where a message is received via named pipes.</summary>
/// <typeparam name="TMessage">The type of message that will be received.</typeparam>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event data passed by the event, which includes the message that was received.</param>
public delegate void NamedPipeMessageReceivedHandler<TMessage>(Object sender, NamedPipeListenerMessageReceivedEventArgs<TMessage> e);
/// <summary>Represents a method that will handle an event that is fired when an exception is caught.</summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event data passed by the event, included the error type and exception that was caught.</param>
public delegate void NamedPipeMessageErrorHandler(Object sender, NamedPipeListenerErrorEventArgs e);
/// <summary>Includes different types of errors that describe where in the listening process an exception was caught.</summary>
public enum NamedPipeListenerErrorType : byte
{
/// <summary>Indicates that an exception was caught while calling <see cref="NamedPipeServerStream.BeginWaitForConnection" />.</summary>
BeginWaitForConnection = 1,
/// <summary>Indicates that an exception was caught while calling <see cref="NamedPipeServerStream.EndWaitForConnection" />.</summary>
EndWaitForConnection = 2,
/// <summary>Indicates that an exception was caught while deserializing a message received from the named pipe.</summary>
DeserializeMessage = 3,
/// <summary>Indicates that an exception was caught while closing or disposing a used named pipe.</summary>
CloseAndDisposePipe = 4,
/// <summary>Indicates that an exception was caught while invoking the <see cref="NamedPipeListener{TMessage}.MessageReceived"/> event.</summary>
NotifyMessageReceived = 5
}
/// <summary>A helper class for sending and receiving messages using named pipes.</summary>
/// <typeparam name="TMessage">The type of message that will be sent or received.</typeparam>
public class NamedPipeListener<TMessage> : IDisposable
{
/// <summary>Occurs when a message is received.</summary>
public event NamedPipeMessageReceivedHandler<TMessage> MessageReceived;
/// <summary>Occurs when an exception is caught.</summary>
public event NamedPipeMessageErrorHandler Error;
static readonly String DEFAULT_PIPENAME = typeof(NamedPipeListener<TMessage>).FullName;
static readonly BinaryFormatter formatter = new BinaryFormatter();
NamedPipeServerStream pipeServer;
/// <summary>Initializes a new instance of <see cref="NamedPipeListener{TMessage}" /> using the specified <paramref name="pipeName" />.</summary>
/// <param name="pipeName">The name of the named pipe that will be used to listen on.</param>
public NamedPipeListener(String pipeName)
{
this.PipeName = pipeName;
}
/// <summary>Initializes a new instance of <see cref="NamedPipeListener{TMessage}" /> using the default pipe name.</summary>
/// <remarks>The default pipe name is the full name of the type of the instance.</remarks>
public NamedPipeListener()
: this(DEFAULT_PIPENAME) { }
/// <summary>The name of the named pipe that will be used to listen on.</summary>
public String PipeName { get; private set; }
/// <summary>Starts listening on the named pipe specified for the instance.</summary>
internal void Start()
{
if (pipeServer == null) pipeServer = new NamedPipeServerStream(DEFAULT_PIPENAME, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
try { pipeServer.BeginWaitForConnection(new AsyncCallback(PipeConnectionCallback), null); }
catch (Exception ex) { this.OnError(NamedPipeListenerErrorType.BeginWaitForConnection, ex); }
}
private void PipeConnectionCallback(IAsyncResult result)
{
try
{
pipeServer.EndWaitForConnection(result);
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.EndWaitForConnection, ex);
return;
}
TMessage message;
try
{
message = (TMessage)formatter.Deserialize(pipeServer);
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.DeserializeMessage, ex);
return;
}
try
{
this.OnMessageReceived(new NamedPipeListenerMessageReceivedEventArgs<TMessage>(message));
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.NotifyMessageReceived, ex);
return;
}
if (this.End())
{
this.Start();
}
}
internal Boolean End()
{
try
{
pipeServer.Close();
pipeServer.Dispose();
pipeServer = null;
return true;
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.CloseAndDisposePipe, ex);
return false;
}
}
private void OnMessageReceived(TMessage message)
{
this.OnMessageReceived(new NamedPipeListenerMessageReceivedEventArgs<TMessage>(message));
}
protected virtual void OnMessageReceived(NamedPipeListenerMessageReceivedEventArgs<TMessage> e)
{
if (this.MessageReceived != null)
{
this.MessageReceived(this, e);
}
}
private void OnError(NamedPipeListenerErrorType errorType, Exception ex)
{
this.OnError(new NamedPipeListenerErrorEventArgs(errorType, ex));
}
protected virtual void OnError(NamedPipeListenerErrorEventArgs e)
{
if (this.Error != null)
{
this.Error(this, e);
}
}
void IDisposable.Dispose()
{
if(pipeServer != null)
{
try { pipeServer.Disconnect(); }
catch { }
try { pipeServer.Close(); }
catch { }
try { pipeServer.Dispose(); }
catch { }
}
}
/// <summary>Sends the specified <paramref name="message" /> to the default named pipe for the message.</summary>
/// <param name="message">The message to send.</param>
public static void SendMessage(TMessage message)
{
NamedPipeListener<TMessage>.SendMessage(DEFAULT_PIPENAME, message);
}
/// <summary>Sends the specified <paramref name="message" /> to the specified named pipe.</summary>
/// <param name="pipeName">The name of the named pipe the message will be sent to.</param>
/// <param name="message">The message to send.</param>
public static void SendMessage(String pipeName, TMessage message)
{
using (var pipeClient = new NamedPipeClientStream(".", DEFAULT_PIPENAME, PipeDirection.Out, PipeOptions.None))
{
pipeClient.Connect();
formatter.Serialize(pipeClient, message);
pipeClient.Flush();
pipeClient.WaitForPipeDrain();
pipeClient.Close();
}
}
}
}
I've done research on this before - you can use a memory mapped file, demonstrated in this article http://www.codeproject.com/KB/cs/singleinstanceapplication.aspx, or you can do what I did (the easy way) and take advantage of vb.net features (specifically, one that lets you make single instance apps and calls a method in the currently running instance that passes on command line args [so you could use it to invoke the method in your application]). I know using VB classes in C# sounds a bit poor but it's the most abstract and easy way. Link to the relevant articles - http://www.codeproject.com/KB/cs/CSSIApp.aspx, last part of http://msdn.microsoft.com/en-us/magazine/cc163741.aspx

Categories

Resources