C# Class GSF.Communication.ClientBase

Base class for a client involved in server-client communication.
Inheritance: System.ComponentModel.Component, IClient, ISupportInitialize, IPersistSettings
Show file Open project: GridProtectionAlliance/gsf Class Usage Examples

Private Properties

Property Type Description
ReConnect void

Public Methods

Method Description
BeginInit ( ) : void

Performs necessary operations before the client properties are initialized.

BeginInit() should never be called by user-code directly. This method exists solely for use by the designer if the server is consumed through the designer surface of the IDE.

Connect ( ) : void

Connects the client to the server synchronously.

ConnectAsync ( ) : WaitHandle

Connects the client to the server asynchronously.

Derived classes are expected to override this method with protocol specific connection operations. Call the base class method to obtain an operational wait handle if protocol connection operation doesn't provide one already.

Create ( string connectionString ) : IClient

Create a communications client

Note that typical connection string should be prefixed with a "protocol=tcp", "protocol=udp", "protocol=serial" or "protocol=file"

Disconnect ( ) : void

When overridden in a derived class, disconnects client from the server synchronously.

EndInit ( ) : void

Performs necessary operations after the client properties are initialized.

EndInit() should never be called by user-code directly. This method exists solely for use by the designer if the server is consumed through the designer surface of the IDE.

Initialize ( ) : void

Initializes the client.

Initialize() is to be called by user-code directly only if the client is not consumed through the designer surface of the IDE.

LoadSettings ( ) : void

Loads saved client settings from the config file if the PersistSettings property is set to true.

Read ( byte buffer, int startIndex, int length ) : int

When overridden in a derived class, reads a number of bytes from the current received data buffer and writes those bytes into a byte array at the specified offset.

This function should only be called from within the ReceiveData event handler. Calling this method outside this event will have unexpected results.

SaveSettings ( ) : void

Saves client settings to the config file if the PersistSettings property is set to true.

Send ( byte data ) : void

Sends data to the server synchronously.

Send ( byte data, int offset, int length ) : void

Sends data to the server synchronously.

Send ( object serializableObject ) : void

Sends data to the server synchronously.

Send ( string data ) : void

Sends data to the server synchronously.

SendAsync ( byte data ) : WaitHandle

Sends data to the server asynchronously.

SendAsync ( byte data, int offset, int length ) : WaitHandle

Sends data to the server asynchronously.

SendAsync ( object serializableObject ) : WaitHandle

Sends data to the server asynchronously.

SendAsync ( string data ) : WaitHandle

Sends data to the server asynchronously.

Protected Methods

Method Description
ClientBase ( ) : System

Initializes a new instance of the client.

ClientBase ( TransportProtocol transportProtocol, string connectionString ) : System

Initializes a new instance of the client.

Dispose ( bool disposing ) : void

Releases the unmanaged resources used by the client and optionally releases the managed resources.

OnConnectionAttempt ( ) : void

Raises the ConnectionAttempt event.

OnConnectionEstablished ( ) : void

Raises the ConnectionEstablished event.

OnConnectionException ( Exception ex ) : void

Raises the ConnectionException event.

OnConnectionTerminated ( ) : void

Raises the ConnectionTerminated event.

OnReceiveData ( int size ) : void

Raises the ReceiveData event.

This event is automatically raised by call to OnReceiveDataComplete so that inheritors never need to worry about raising this event. This method is only included here in case any custom client implementations need to explicitly raise this event.

OnReceiveDataComplete ( byte data, int size ) : void

Raises the ReceiveDataComplete event.

OnReceiveDataException ( Exception ex ) : void

Raises the ReceiveDataException event.

OnSendDataComplete ( ) : void

Raises the SendDataComplete event.

OnSendDataException ( Exception ex ) : void

Raises the SendDataException event.

OnSendDataStart ( ) : void

Raises the SendDataStart event.

OnUnhandledUserException ( Exception ex ) : void

Raises the UnhandledUserException event.

SendDataAsync ( byte data, int offset, int length ) : WaitHandle

When overridden in a derived class, sends data to the server asynchronously.

UpdateBytesReceived ( int bytes ) : void

Updates the Statistics pertaining to bytes received.

UpdateBytesSent ( int bytes ) : void

Updates the Statistics pertaining to bytes sent.

ValidateConnectionString ( string connectionString ) : void

When overridden in a derived class, validates the specified connectionString.

Private Methods

Method Description
ReConnect ( ) : void

Re-connects the client if currently connected.

Method Details

BeginInit() public method

Performs necessary operations before the client properties are initialized.
BeginInit() should never be called by user-code directly. This method exists solely for use by the designer if the server is consumed through the designer surface of the IDE.
public BeginInit ( ) : void
return void

ClientBase() protected method

Initializes a new instance of the client.
protected ClientBase ( ) : System
return System

ClientBase() protected method

Initializes a new instance of the client.
protected ClientBase ( TransportProtocol transportProtocol, string connectionString ) : System
transportProtocol TransportProtocol One of the values.
connectionString string The data used by the client for connection to a server.
return System

Connect() public method

Connects the client to the server synchronously.
public Connect ( ) : void
return void

ConnectAsync() public method

Connects the client to the server asynchronously.
Derived classes are expected to override this method with protocol specific connection operations. Call the base class method to obtain an operational wait handle if protocol connection operation doesn't provide one already.
Server property in is invalid. Attempt is made to connect the client when it is not disconnected.
public ConnectAsync ( ) : WaitHandle
return System.Threading.WaitHandle

Create() public static method

Create a communications client
Note that typical connection string should be prefixed with a "protocol=tcp", "protocol=udp", "protocol=serial" or "protocol=file"
public static Create ( string connectionString ) : IClient
connectionString string Connection string for the client.
return IClient

Disconnect() public method

When overridden in a derived class, disconnects client from the server synchronously.
public Disconnect ( ) : void
return void

Dispose() protected method

Releases the unmanaged resources used by the client and optionally releases the managed resources.
protected Dispose ( bool disposing ) : void
disposing bool true to release both managed and unmanaged resources; false to release only unmanaged resources.
return void

EndInit() public method

Performs necessary operations after the client properties are initialized.
EndInit() should never be called by user-code directly. This method exists solely for use by the designer if the server is consumed through the designer surface of the IDE.
public EndInit ( ) : void
return void

Initialize() public method

Initializes the client.
Initialize() is to be called by user-code directly only if the client is not consumed through the designer surface of the IDE.
public Initialize ( ) : void
return void

LoadSettings() public method

Loads saved client settings from the config file if the PersistSettings property is set to true.
has a value of null or empty string.
public LoadSettings ( ) : void
return void

OnConnectionAttempt() protected method

Raises the ConnectionAttempt event.
protected OnConnectionAttempt ( ) : void
return void

OnConnectionEstablished() protected method

Raises the ConnectionEstablished event.
protected OnConnectionEstablished ( ) : void
return void

OnConnectionException() protected method

Raises the ConnectionException event.
protected OnConnectionException ( Exception ex ) : void
ex System.Exception Exception to send to event.
return void

OnConnectionTerminated() protected method

Raises the ConnectionTerminated event.
protected OnConnectionTerminated ( ) : void
return void

OnReceiveData() protected method

Raises the ReceiveData event.
This event is automatically raised by call to OnReceiveDataComplete so that inheritors never need to worry about raising this event. This method is only included here in case any custom client implementations need to explicitly raise this event.
protected OnReceiveData ( int size ) : void
size int Number of bytes received from the client.
return void

OnReceiveDataComplete() protected method

Raises the ReceiveDataComplete event.
protected OnReceiveDataComplete ( byte data, int size ) : void
data byte Data received from the client.
size int Number of bytes received from the client.
return void

OnReceiveDataException() protected method

Raises the ReceiveDataException event.
protected OnReceiveDataException ( Exception ex ) : void
ex System.Exception Exception to send to event.
return void

OnSendDataComplete() protected method

Raises the SendDataComplete event.
protected OnSendDataComplete ( ) : void
return void

OnSendDataException() protected method

Raises the SendDataException event.
protected OnSendDataException ( Exception ex ) : void
ex System.Exception Exception to send to event.
return void

OnSendDataStart() protected method

Raises the SendDataStart event.
protected OnSendDataStart ( ) : void
return void

OnUnhandledUserException() protected method

Raises the UnhandledUserException event.
protected OnUnhandledUserException ( Exception ex ) : void
ex System.Exception Exception to send to event.
return void

Read() public abstract method

When overridden in a derived class, reads a number of bytes from the current received data buffer and writes those bytes into a byte array at the specified offset.
This function should only be called from within the ReceiveData event handler. Calling this method outside this event will have unexpected results.
public abstract Read ( byte buffer, int startIndex, int length ) : int
buffer byte Destination buffer used to hold copied bytes.
startIndex int 0-based starting index into destination to begin writing data.
length int The number of bytes to read from current received data buffer and write into .
return int

SaveSettings() public method

Saves client settings to the config file if the PersistSettings property is set to true.
has a value of null or empty string.
public SaveSettings ( ) : void
return void

Send() public method

Sends data to the server synchronously.
public Send ( byte data ) : void
data byte The binary data that is to be sent.
return void

Send() public method

Sends data to the server synchronously.
public Send ( byte data, int offset, int length ) : void
data byte The buffer that contains the binary data to be sent.
offset int The zero-based position in the at which to begin sending data.
length int The number of bytes to be sent from starting at the .
return void

Send() public method

Sends data to the server synchronously.
public Send ( object serializableObject ) : void
serializableObject object The serializable object that is to be sent.
return void

Send() public method

Sends data to the server synchronously.
public Send ( string data ) : void
data string The plain-text data that is to be sent.
return void

SendAsync() public method

Sends data to the server asynchronously.
public SendAsync ( byte data ) : WaitHandle
data byte The binary data that is to be sent.
return System.Threading.WaitHandle

SendAsync() public method

Sends data to the server asynchronously.
public SendAsync ( byte data, int offset, int length ) : WaitHandle
data byte The buffer that contains the binary data to be sent.
offset int The zero-based position in the at which to begin sending data.
length int The number of bytes to be sent from starting at the .
return System.Threading.WaitHandle

SendAsync() public method

Sends data to the server asynchronously.
public SendAsync ( object serializableObject ) : WaitHandle
serializableObject object The serializable object that is to be sent.
return System.Threading.WaitHandle

SendAsync() public method

Sends data to the server asynchronously.
public SendAsync ( string data ) : WaitHandle
data string The plain-text data that is to be sent.
return System.Threading.WaitHandle

SendDataAsync() protected abstract method

When overridden in a derived class, sends data to the server asynchronously.
protected abstract SendDataAsync ( byte data, int offset, int length ) : WaitHandle
data byte The buffer that contains the binary data to be sent.
offset int The zero-based position in the at which to begin sending data.
length int The number of bytes to be sent from starting at the .
return System.Threading.WaitHandle

UpdateBytesReceived() protected method

Updates the Statistics pertaining to bytes received.
protected UpdateBytesReceived ( int bytes ) : void
bytes int
return void

UpdateBytesSent() protected method

Updates the Statistics pertaining to bytes sent.
protected UpdateBytesSent ( int bytes ) : void
bytes int
return void

ValidateConnectionString() protected abstract method

When overridden in a derived class, validates the specified connectionString.
protected abstract ValidateConnectionString ( string connectionString ) : void
connectionString string The connection string to be validated.
return void