◄ Previous: SNF's OPCodes
This shows you how to handle anything related to connections You can jump to Practical Example to see how it can be used and sat up.
To properly setup the connection instance(s) you have to go through 5 steps:
Instantiate a new Connection object
Never call new Connection()
to get a new object instance, instead you are advised to use either one of the following methods depending on your use case
In case of a Single Connection
If you only need one connection to one server then preferably use the global instantiator, and always use the methods made for the global instance for sending/receiving requests.
The Global instantiator method is SNFClient::Connection::Instantiate() which require no arguments and shall return a SNFClient::Connection 'The Global Instance Connection Instance'
- Note
- You must set the Global Instance's Host and port before continuing any further.
The method returns the instance so you can set them like the following example
Objects of this class are what controls and handles Connections to the Host.
Definition Connection.cs:14
Connection SetPort(int Port)
Sets the connection host's port for this instance.
Definition Connection.cs:250
Connection SetHost(string Host)
Set's the connection host for this instance.
Definition Connection.cs:230
static Connection Instantiate(string host, int Port)
Instantiates a new Global Connection Instance.
Definition Connection.cs:45
Definition Callbacks.cs:5
In case of multiple Connections
To create multiple Connections ( or Connection Instances ) you must use the SNFClient::Connection::Instantiate(string,int) Method like in this example
Set Event CallBacks
SNF Connections currenty has 5 Event Callbacks each serving a purpose
- Warning
- Never Set any Event Callback after(if it connected) and/or while Connecting
OnConnection
When is it Called?: This Event will raised when a connection was established and SNF's Protocol has finished the initial handshake
Type of CallBack: SimpleCB
How to Set: SetOnConnection(SimpleCB)
Method 1:
- Define the callback
void OnConnectCallBack() {
...
...
}
- Set the CallBack
Connection cnx;
...
...
Connection SetOnConnection(SimpleCB OnConnect)
Sets The callbackto be called Upon Connection.
Definition Connection.cs:268
Method 2:
- Define the callback and Set it directly
...
Connection cnx;
cnx.SetOnConnection(
() => {
...
...
}
);
...
OnException
When is it Called?: This Event is raised when a certain(and possibly unknown) exception was called and wasn't handled properly by SNFClient
Type of CallBack: ExceptionCB
How to Set: SetOnException(ExceptionCB)
Method 1:
- Define the callback
void OnExceptionCallBack(Exception ex) {
...
...
}
- Set the CallBack
Connection cnx;
...
cnx.SetOnException(OnExceptionCallBack);
...
Method 2:
- Define the callback and Set it directly
...
Connection cnx;
cnx.SetOnException(
(Exception ex) => {
...
...
}
);
...
OnSNFFail
When is it Called?: This Event is raised when SNF's Protocol has failed the initial handshake
Type of CallBack: SimpleCB
How to Set: SetOnSNFFail(SimpleCB)
Method 1:
- Define the callback
Connection cnx;
void OnSNFFailCallBack() {
...
...
}
- Set the CallBack
...
Connection cnx;
cnx.SetOnSNFFail(OnSNFFailCallBack);
...
Method 2:
- Define the callback and Set it directly
...
Connection cnx;
cnx.SetOnSNFFail(
() => {
...
...
}
);
...
OnSocketFail
When is it Called?: This Event is raised when the Socket Connection between the client and the Server has raised a SocketException
Type of CallBack: SocketExceptionCB
How to Set: SetOnSocketFail(SocketExceptionCB)
Method 1:
- Define the callback
void OnSocketFailCallBack(SocketException ex) {
...
...
}
- Set the CallBack
Connection cnx;
...
cnx.SetOnSocketFail(OnSocketFailCallBack);
...
Method 2:
- Define the callback and Set it directly
...
Connection cnx;
cnx.SetOnSNFFail(
(SocketException ex) => {
...
...
}
);
...
OnTimeout
When is it Called?: This Event is raised when the Connection attempt has exceeded the timeout limit set by AwaitConnection(..)
Type of CallBack: SimpleCB
How to Set: SetOnTimeout(SimpleCB)
Method 1:
- Define the callback
void OnSocketFailCallBack(SocketException ex) {
...
...
}
- Set the CallBack
Connection cnx;
...
cnx.SetOnSocketFail(OnSocketFailCallBack);
...
Method 2:
- Define the callback and Set it directly
...
Connection cnx;
cnx.SetOnSNFFail(
(SocketException ex) => {
...
...
}
);
...
Connect
To Connect is pretty Simple you, just call Connect
- Note
- Always configure the Connection and the event Callback before calling Connect
- Warning
- Connect May Return
null
so be Aware
...
Connection cnx;
cnx.Connect();
...
Wait for the Connection
There are 2 Methods made to Await util the Connection is made,
- AwaitConnection(int) Which Counts in Miliseconds
- AwaitConnection(TimeSpan) that uses
TimeSpan
.
I will only give an example of the former
- Warning
- Always wait for the connection after calling Connect
...
cnx.AwaitConenction(5000);
...
Send Requests
Assuming you have instantiated a new Request Object and Set it properly, we can send it according to your use case
- Note
- Preferably Always Send after the Connection Instance has Connected (eg isConnected returns
true
)
In case of a non-Global Instance
For this case (Assuming cnx
is your Connection Instance and that Rqst
is your Request instance that you want to send ) you can use Send(Request)
In case of a Global Instance
For this case you can use the same Method as the In case of a non-Global Instance which is pecefic to Global Instances (Method 1) or the Function ( Method 2 )
- Note
- We'll assume that
Rqst
is your Request instance that you want to Send
Method 1 Using InstanceSend(Request)
...
Connection.InstanceSend(Rqst);
...
Method 2 Using Send(Request)
...
Connection cnx = Connection.get();
cnx.Send(Rqst)
Connection.get().Send(Rqst);
...
Practical Example
In this Example I'm gonna create a Console Client that creates a globally instantiated Connection that connects into a SNF Server hosted in the same machine on port 9114
namespace Example
{
public class Program
{
public static void Main(string[] args)
{
() => {
Console.WriteLine("Connected");
}
)
.SetOnSocketFail(
(ex) => {
Console.WriteLine("SocketFailed: " + ex.ToString());
}
)
.SetOnSNFFail(
() => {
Console.WriteLine("SNF HandShake Failed");
}
)
.SetOnException(
(ex) =>
{
Console.WriteLine("Unknown Exception" + ex.ToString());
}
)
.Connect()?
...
}
}
}
Connection AwaitConnection(TimeSpan timeout)
Block Until the instance is Connected.
Definition Connection.cs:361
This Class Defined THe Base OPCode Structure for Basic SNF Functioning.
Definition OPCode.cs:769
static void Initialize()
Initializes OPCode.Base.
Definition OPCode.cs:848
Objects of this class Define the action to do to the host.
Definition OPCode.cs:12