//******************************************************************************************************
// IClient.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/26/2006 - Pinal C. Patel
// Original version of source code generated.
// 09/06/2006 - J. Ritchie Carroll
// Added ReceiveRawDataFunction delegate to allow bypass optimizations for high-speed data access.
// 05/01/2007 - Pinal C. Patel
// Made WaitForConnection() functions that returns a boolean value indicating success or failure.
// 09/27/2007 - J. Ritchie Carroll
// Added disconnect timeout overload.
// 09/29/2008 - J. Ritchie Carroll
// Converted to C#.
// 11/07/2008 - Pinal C. Patel
// Edited code comments.
// 07/08/2009 - J. Ritchie Carroll
// Added WaitHandle return value from asynchronous connection.
// 08/05/2009 - Josh L. Patterson
// Edited Comments.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Text;
using System.Threading;
using GSF.Units;
namespace GSF.Communication
{
#region [ Enumerations ]
///
/// Indicates the current state of the client.
///
public enum ClientState
{
///
/// Client is establishing connection.
///
Connecting,
///
/// Client has established connection.
///
Connected,
///
/// Client connection is terminated.
///
Disconnected
}
#endregion
///
/// Defines a client involved in server-client communication.
///
public interface IClient : ISupportLifecycle, IProvideStatus
{
#region [ Members ]
// Events
///
/// Occurs when client is attempting connection to the server.
///
event EventHandler ConnectionAttempt;
///
/// Occurs when client connection to the server is established.
///
event EventHandler ConnectionEstablished;
///
/// Occurs when client connection to the server is terminated.
///
event EventHandler ConnectionTerminated;
///
/// Occurs when an is encountered during connection attempt to the server.
///
event EventHandler> ConnectionException;
///
/// Occurs when the client begins sending data to the server.
///
event EventHandler SendDataStart;
///
/// Occurs when the client has successfully sent data to the server.
///
event EventHandler SendDataComplete;
///
/// Occurs when an is encountered when sending data to the server.
///
///
/// is the encountered when sending data to the server.
///
event EventHandler> SendDataException;
///
/// Occurs when unprocessed data has been received from the server.
///
///
///
/// This event can be used to receive a notification that server data has arrived. The method can then be used
/// to copy data to an existing buffer. In many cases it will be optimal to use an existing buffer instead of subscribing to the
/// event, however, the data that is available after calling the method
/// will be the original unprocessed data received by the client, i.e., not optionally decrypted or decompressed data.
///
///
/// is the number of bytes received in the buffer from the server.
///
///
event EventHandler> ReceiveData;
///
/// Occurs when data received from the server has been processed and is ready for consumption.
///
///
/// is a new buffer containing post-processed data received from the server starting at index zero.
/// is the number of post-processed bytes received in the buffer from the server.
///
event EventHandler> ReceiveDataComplete;
///
/// Occurs when an is encountered when receiving data from the server.
///
///
/// is the encountered when receiving data from the server.
///
event EventHandler> ReceiveDataException;
///
/// Occurs when an is encountered in a user-defined function via an event dispatch.
///
///
/// is the thrown by the user-defined function.
///
event EventHandler> UnhandledUserException;
#endregion
#region [ Properties ]
///
/// Gets or sets the data required by the client to connect to the server.
///
string ConnectionString { get; set; }
///
/// Gets or sets the maximum number of times the client will attempt to connect to the server.
///
int MaxConnectionAttempts { get; set; }
///
/// Gets or sets the size of the buffer used by the client for receiving data from the server.
///
int SendBufferSize { get; set; }
///
/// Gets or sets the size of the buffer used by the client for receiving data from the server.
///
int ReceiveBufferSize { get; set; }
///
/// Gets or sets the to be used for the text sent to the server.
///
Encoding TextEncoding { get; set; }
///
/// Gets the server URI.
///
string ServerUri { get; }
///
/// Gets the current server index, when multiple server end points are defined.
///
int ServerIndex { get; }
///
/// Gets the current .
///
ClientState CurrentState { get; }
///
/// Gets the used by the client for the transportation of data with the server.
///
TransportProtocol TransportProtocol { get; }
///
/// Gets the for which the client has been connected to the server.
///
Time ConnectionTime { get; }
///
/// Gets the for the client connection.
///
TransportStatistics Statistics { get; }
#endregion
#region [ Methods ]
///
/// Connects client to the server synchronously.
///
void Connect();
///
/// Connects client to the server asynchronously.
///
/// for the asynchronous operation.
WaitHandle ConnectAsync();
///
/// Disconnects client from the server synchronously.
///
void Disconnect();
///
/// Sends data to the server synchronously.
///
/// The buffer that contains the binary data to be sent.
/// The zero-based position in the at which to begin sending data.
/// The number of bytes to be sent from starting at the .
void Send(byte[] data, int offset, int length);
///
/// Sends data to the server asynchronously.
///
/// The buffer that contains the binary data to be sent.
/// The zero-based position in the at which to begin sending data.
/// The number of bytes to be sent from starting at the .
/// for the asynchronous operation.
WaitHandle SendAsync(byte[] data, int offset, int length);
///
/// Reads a number of bytes from the current received data buffer and writes those bytes into a byte array at the specified offset.
///
/// Destination buffer used to hold copied bytes.
/// 0-based starting index into destination to begin writing data.
/// The number of bytes to read from current received data buffer and write into .
/// The number of bytes read.
///
/// This function should only be called from within the event handler. Calling this method outside this event
/// will have unexpected results.
///
int Read(byte[] buffer, int startIndex, int length);
///
/// Requests that the client attempt to move to the next .
///
/// true if request succeeded; otherwise, false.
///
/// Return value will only be true if changed.
///
bool RequestNextServerIndex();
#endregion
}
}