//////////////////////////////////////////////////
// Created by Ivan Latunov - IvanWeb.com //
//----------------------------------------------//
// This program is free software. You can //
// redistribute it and/or modify it as you wish //
//////////////////////////////////////////////////
using System;
using System.IO;
using sscs.communication.win.InterProcessComm;
namespace sscs.communication.win.NamedPipes {
#region Comments
///
/// Used by client applications to communicate with server ones by using named pipes.
///
#endregion
public sealed class ClientPipeConnection : APipeConnection {
#region Comments
///
/// The network name of the server where the server pipe is created.
///
///
/// If "." is used as a server name then the pipe is connected to the local machine.
///
#endregion
private string Server = ".";
#region Comments
///
/// Closes a client named pipe connection.
///
///
/// A client pipe connection is closed by closing the underlying pipe handle.
///
#endregion
public override void Close() {
CheckIfDisposed();
NamedPipeWrapper.Close(this.Handle);
}
#region Comments
///
/// Connects a client pipe to an existing server one.
///
#endregion
public override void Connect() {
CheckIfDisposed();
this.Handle = NamedPipeWrapper.ConnectToPipe(this.Name, this.Server);
}
#region Comments
///
/// Attempts to establish a connection to the a server named pipe.
///
///
/// If the attempt is successful the method creates the
/// PipeHandle object
/// and assigns it to the Handle
/// field.
/// This method is used when it is not known whether a server pipe already exists.
///
/// True if a connection is established.
#endregion
public bool TryConnect() {
CheckIfDisposed();
bool ReturnVal = NamedPipeWrapper.TryConnectToPipe(this.Name, this.Server, out this.Handle);
return ReturnVal;
}
#region Comments
///
/// Creates an instance of the ClientPipeConnection assuming that the server pipe
/// is created on the same machine.
///
///
/// The maximum bytes to read from the client is set to be Int32.MaxValue.
///
/// The name of the server pipe.
#endregion
public ClientPipeConnection(string name) {
this.Name = name;
this.Server = ".";
this.maxReadBytes = Int32.MaxValue;
}
#region Comments
///
/// Creates an instance of the ClientPipeConnection specifying the network name
/// of the server.
///
///
/// The maximum bytes to read from the client is set to be Int32.MaxValue.
///
/// The name of the server pipe.
/// The network name of the machine, where the server pipe is created.
#endregion
public ClientPipeConnection(string name, string server) {
this.Name = name;
this.Server = server;
this.maxReadBytes = Int32.MaxValue;
}
#region Comments
///
/// Object destructor.
///
#endregion
~ClientPipeConnection() {
Dispose(false);
}
}
}