MetaSharp.NET framework
From MetaSharp
Article Author(s): Audric Thevenet
All Rights Reserved.
Contents |
Introduction
MetaSharp.NET framework is born in 2005. Its purpose is about providing strong code to do tasks we often do while being as compliant as possible with Microsoft.NET framework.
The framework is composed of 2 components:
- a network framework (to create easily a client/server application)
- some utilities
Download MetaSharp .NET framework for VS2005
Network Framework
Namespace MetaSharp.Net
Server Sample
ArrayList connectionsList = new ArrayList(); NetManager nm = new NetManager(5, 10); nm.Log.Informed += new EventHandler<MessageEventArgs>(DisplayNetManagerLogs); ConnectionInfo listener = nm.AddConnection(ConnectionType.Listener, ProtocolType.IP, "127.0.0.1", 12345); listener.Accepted += new EventHandler<ConnectionEventArgs>(NewConnection); listener.Closed += new EventHandler<ConnectionEventArgs>(ConnectionClosed);
This server example will create a network manager consisting of 5 threads of 10 network connections each on port 12345. We also setup the Informed event and created a clients connections array (connectionsList). Whenever a new connection will happen, the NewConnection method will be called:
private void NewConnection(object sender, ConnectionEventArgs e)
{
e.ChildInfo.Closed += new EventHandler<ConnectionEventArgs>(ConnectionClosed);
e.ChildInfo.Incoming += new EventHandler<ConnectionEventArgs>(Reception);
e.ChildInfo.Outgoing += new EventHandler<ConnectionEventArgs>(Emission);
connectionsList.Add(e.ChildInfo);
}
As we can see here, we immediately set up the connection events so that we'll be informed whenever an event happens. We also stored the ConnectionInfo so that we can reuse it at a later time and also because we are the ones that MUST dispose this object as follows for example:
private void ConnectionClosed(object sender, ConnectionEventArgs e)
{
connectionsList.Remove(e.CurrentInfo);
e.CurrentInfo.Dispose();
}
Don't forget to dispose your NetManager object in the end:
nm.Dispose();
Client Sample
NetManager nm = new NetManager(1, 5);
ConnectionInfo info = nm.AddConnection(ConnectionType.Classic, ProtocolType.IP, "127.0.0.1", 12345);
info.Closed += new EventHandler<ConnectionEventArgs>(ConnectionClosed);
info.Incoming += new EventHandler<ConnectionEventArgs>(Reception);
info.Outgoing += new EventHandler<ConnectionEventArgs>(Emission);
Console.WriteLine("Type some text to send then press return");
info.PushOutgoingData(Console.ReadLine());
Console.WriteLine("Demo is over, Closing Client...");
info.Dispose();
nm.Dispose();
As you see, the client works the exact same way the server does. Easy.
Utilities
Namespace MetaSharp
GetFarType
Gets the type of an object even if the object is located in another assembly.
public static Type GetFarType(string farType);
ShowMessageBox
Similar to MessageBox.Show() with fxcop compliancy.
public static DialogResult ShowMessageBox(object sender, string message, string caption); public static DialogResult ShowMessageBox(object sender, string message, string caption, MessageBoxButtons buttons);
Namespace MetaSharp.Data
CsvMatrix
A CSV (Comma Separated Values) matrix object is a matrix of strings that can be constructed from a string object containing the requirred information. You can access its data using myCsvMatrix[row,column] for example.
FifoBuffer
The FifoBuffer object is a "First In First Out" (a queue) of bytes. You can Push() or Pop() data from it, get the actual Length or look at the rawData (its length might often be longer than the Length property).
Logger
Creating a Logger object (timestamped or not) lets you register methods on its Informed event. For example you register a method logging to a file the messages sent by the Logger through its Informed event.
private Logger log; log.Informed += new MessageEventHandler(LogToFile);
Then you can call the methods to log some informations of different critical levels:
log.Error("this is a painful error");
log.Info("this is a plain information");
log.Warning("this is a warning, program won't crash, but the error wasn't expected");
