SharpNetworking is a C# networking library. It can be used in .NET environments.
Server provides functionality for establishing connections and sending data back and forth, leaving control over sending and receiving packets to you.
Grab the SharpNetworking.dll file from the latest release and simply add a reference inside your project.
The server life cycle consists of seven stages:
Idle
- server was just createdInitializing
- Initialize method was calledInitialized
- the Initialize method was successfulLaunching
- Launch method was calledLaunched
- the Launch method was successfulTerminating
- Terminate method was calledTerminated
- the Terminate method was successful
Core methods:
Initialize
- creates new TcpListener and UdpListener each time when invoked, creates IdPool and Clients instances only when invoked the first time. Can be invoked only when server status equals Idle or TerminatedLaunch
- starts TcpListener and UdpListener that listen on their corresponding ports. Can be invoked only when server status equals InitializedTerminate
- stops TcpListener and UdpListener and disconnects all clients. Can be invoked only when server status equals Launched
You can subscribe to the following events:
- Server
NewMessage
- invoked when the server broadcasts a new message (about internal things that happen inside server)StatusChanged
- invoked when the server changed its statusInitializing
- invoked when initializing the server (used to run your own logic before changing status to Initialized)Launching
- invoked when launching the server (used to run your own logic before changing status to Launched)Terminating
- invoked when terminating the server (used to run your own logic before changing status to Terminated)PacketReceived
- invoked when packet is received from a clientUdpPacketReceived
- invoked when UDP packet is received from unbound clientBound
- invoked when the connection with the client has been established and an identifier (int) has been assigned to the clientDisconnected
- invoked when a client disconnectsintFailedToBind
- invoked when failed to get NetworkStream from TcpSocket (connection is dropped)FailedToSendPacket
- invoked when failed to send packet to clientFailedToSendUdpPacket
- invoked when failed to send UDP packet to unbound clientFailedToReceivePacket
- invoked when failed to receive packet from clientFailedToReceiveUdpPacket
- invoked when failed to receive UDP packet from unbound client
- Client
PacketReceived
- invoked when a packet is received from a serverConnected
- invoked when a connection to the server is establishedBound
- invoked when a connection to the server is establishedDisconnected
- invoked when disconnected from the serverFailedToConnect
- invoked when failed to connect to the serverFailedToBind
- invoked when failet to bind Client to SocketFailedToSend
- invoked when failed to send packet
Packets are created like:
Request request = new Request(<senderId>, <messageId>);
Response response = new Response(<request>, <senderId>, <messageId>);
<senderId>
corresponds to the Enum value that identifies the sender<messageId>
corresponds to the Enum value that identifies a given packet<request>
corresponds to the request to which this Response will be sent
To add data to your packet, use:
request.Write(value);
response.Write(value);
value
can be any of the following types: byte
, byte[]
, bool
, short
, ushort
, int
, uint
, long
, ulong
, float
, double
, string
, BigInteger
, Enum
, DateTime
.
You can always create extension methods to support any custom type.
To send your packet, use one of the following:
client.Tcp.Send(packet); // Sends TCP packet to client
client.Udp.Send(packet); // Sends UDP packet to client
To handle packet, simply subscribe OnPacketReceived method to PacketReceived Server event. For example:
private void OnPacketReceived(Client client, Packet packet)
{
if (packet is Request) Handle(client, packet as Request);
else if (packet is Response) Handle(client, packet as Response);
}
private void Handle(Client client, Request request)
{
int someInt = request.ReadInt();
bool someBool = request.ReadBool();
if (request.TryRead(out double someDouble))
{
// Do something
}
else
{
// Do something
}
}
private void Handle(Client client, Response response)
{
// Similar to the above
}
When handling packet remember that it contains additional information like from who and what packet it is (SenderId and Id, respectively)