Skip to main content

Messaging

The network messaging system in FigNet is based on a Request & Response paradigm. A network request object is called an Operation, and a network response object is called a Handler. An application node can send Operations to a server node and vice versa.

note

Server-to-Server and Client-to-Server communication use the same syntax.

How to create an Operation

using FigNetProviders;
using System.Collections.Generic;

namespace DemoClient.Messages.Operations
{
class EchoOperation
{
const ushort OP_CODE = 1;
public static Operation GetOperation(string msg)
{
Dictionary<byte, object> parameters = new Dictionary<byte, object>
{
{ 0, msg }
};
Operation op = new Operation(OP_CODE, parameters, null);
return op;
}
}
}

How to create a Handler

using FigNet.Core;

namespace DemoServer.Messages.Handlers
{
class EchoMessageHandler : IHandler
{
public ushort OpCode = 1;
public void HandleMessage(IMessage message, uint PeerId)
{
// echo back message to sender
var sender = FN.PeerCollection.GetPeerByID(PeerId);
message.Parameters.Add(1, PeerId); // append sender id at the end
FN.Server.SendMessage(sender, message, DeliveryMethod.Reliable, 0);
}
}
}
note

FN.HandlerCollection.RegisterHandler(new EchoMessageHandler()); - Register Handler