Shadow Network Framework  0.0.1-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
Tutorials

Curreently you can only set up new OPCodes

Setting up new OPCodes

Important
Although you can define new opcode, the current version doesnt let you make use of user-defined Commands as of 0.0.1-alpha

One thing to note is that you do not define whole OPCodes but you define each part of the OPCode one by one with a String definitoin of each Member, which means you define the Category, then the Sub-Category and so on

Warning
Never Add/Modify Base OPCode Members!
Note
Every OPCode Member that is of Command Rank, it will have always by default a Detail 0 called SNF_OPCODE_BASE_DET_UNDETAILED

Example Let's Say we have to define this hierachy

  • Category 1 its definition is "Category for Vehicle Commands"
    • Sub Category 1 "Category for Vehicle Screen Commands"
      • Command 1 "Change Vehicle Screen Bightness Command"
        • Detail 1 "Maximum Brightness"
        • Detail 2 "Minimum Brightness"
      • Command 2 "Toggle Vehicle Screen Command"
#include <SNF.h>
#include <stdlib.h>
int main() {
/*
Since we are going to change the OPCodes, we need to initialize
the opcode structure before adding anything
*/
// Define Category 1 (Categories Start with 1
// as Category 0(Base) Exists already)
0x01, // The Category's code
"Category for Vehicle Commands"
);
// Defining Sub Category 1
0x01, // The Category's code
0x00, // the Sub Category's Code
"Category for Vehicle Screen Commands"
);
// Defining The latter Sub Category's Command 1
0x01, // The Category's code
0x00, // the Sub Category's Code
0x00, // the Command's Code
"Change Vehicle Screen Bightness Command"
);
// Defining The latter Command's Detail 1 (Detail 0 (Undetailed) Already Exist)
0x01, // The Category's code
0x00, // the Sub Category's Code
0x00, // the Command's Code
0x01, // the Detail's Code
"Maximum Brightness"
);
// Defining The latter Command's Detail 2
0x01, // The Category's code
0x00, // the Sub Category's Code
0x00, // the Command's Code
0x02, // the Detail's Code
"Minimum Brightness"
);
// Defining The latter Sub Category's Command 2
0x01, // The Category's code
0x00, // the Sub Category's Code
0x01, // the Command's Code
"Toggle Vehicle Screen Command"
);
return EXIT_SUCCESS;
}
int snf_opcode_define_category(SNF_opcode_mmbr_t Code, const char *Definition)
Defines an opcode Category.
int snf_opcode_define_detail(SNF_opcode_mmbr_t Category, SNF_opcode_mmbr_t SubCategory, SNF_opcode_mmbr_t Command, SNF_opcode_mmbr_t Code, const char *Definition)
Defines an opcode Detail.
int snf_opcode_define_sub_category(SNF_opcode_mmbr_t Category, SNF_opcode_mmbr_t Code, const char *Definition)
Defines an opcode Sub-Category.
int snf_opcode_init()
Initializes the SNF's opcodes.
int snf_opcode_define_command(SNF_opcode_mmbr_t Category, SNF_opcode_mmbr_t SubCategory, SNF_opcode_mmbr_t Code, const char *Definition)
Defines an opcode Command.

Getting an OPCode Member

Assuming that we use the OPCodes Defined in Setting up new OPCodes if we want to get the OPCode Member we use snf_opcode_get_{Rank}(..)

Note
if you want to get a OPCode Member use snf_opcode_get_base_{Rank}(..)

Example:

#include <SNF.h>
#include <stdlib.h>
int main() {
/*
Intialize OPCode & Define your OPCodes First
*/
// Getting a Category
0x01
);
// If you want to Getting a base category
return EXIT_SUCCESS;
}
SNF_opcode_LL_item * snf_opcode_get_category(SNF_opcode_mmbr_t Category)
Fetches the opcode Category from the opcode's data structure.
SNF_opcode_LL_item * snf_opcode_get_base_category()
Fetches the base opcode Category from the opcode's data structure.
Structure used to save registred opcode members.
Definition opcode.h:60
Warning
Do not free any SNF_opcode_LL_item !!

Generating an OPCode

if we want to get a fully structured OPCode we use snf_opcode_get for Detailed Commands, and snf_opcode_getu

Note
if you want to get a Base OPCode use snf_opcode_get_base and snf_opcode_getu_base respectively

Example:

#include <SNF.h>
#include <stdlib.h>
int main() {
/*
Intialize OPCode & Define your OPCodes First
*/
// Getting The OPcode "Change Vehicle Screen Bightness Command"
// with "Maximum Brightness" Detail
0x01, // Category
0x00, // Sub Category
0x00, // Command
0x01 // Detail
);
// Getting The OPcode "Change Vehicle Screen Bightness Command"
// with no Detail
0x01, // Category
0x00, // Sub Category
0x00, // Command
);
return EXIT_SUCCESS;
}
SNF_opcode * snf_opcode_getu(SNF_opcode_mmbr_t Category, SNF_opcode_mmbr_t SubCategory, SNF_opcode_mmbr_t Command)
Fetches a fully structured OPcode using the default Detail.
SNF_opcode * snf_opcode_get(SNF_opcode_mmbr_t Category, SNF_opcode_mmbr_t SubCategory, SNF_opcode_mmbr_t Command, SNF_opcode_mmbr_t Detail)
Fetches a fully structured OPcode.
Structure for the opcode.
Definition opcode.h:29

Generating a Request

Generating Request depends on either it's as a response request, or as a server request to the client, See SNF_Request_t::UID for more detail about the difference, to apply either we'll split this into Two parts:

A Response Request

To generate a response Request we use one of the following

  • snf_request_gen_response
    SNF_RQST *ClientReq; // Assuming this is the request client has sent.
    // Generate your Request's Response OPCode
    // Generate your Request's Argument ( Assuming we only have one argument)
    // Remember that Content Must be a string for this version
    SNF_RQST_ARG * arg = snf_request_arg_gen("<Argument Content>");
    ClientReq, // Client's Original Request
    op, // Your Request's OPCode
    arg // Your Request's Content
    );
    /*
    Do Whatever you want to do with Server Response
    */
    SNF_RQST * snf_request_gen_response(SNF_RQST *Original, SNF_opcode *OPCODE, SNF_RQST_ARG *Args)
    Generates a new response request.
    SNF_RQST_ARG * snf_request_arg_gen(const char *arg)
    Generates a new Argumment.
    Definition request.h:48
    The Structure for saving Requests.
    Definition request.h:29
  • snf_request_gen_wUID
    Note
    UID of the response request MUST be the same as the Client's original request so preferrably use snf_request_gen_response as it's simpler to use unles this is what you want to use
    SNF_RQST *ClientReq; // Assuming this is the request client has sent.
    // Generate your Request's Response OPCode
    // Generate your Request's Argument ( Assuming we only have one argument)
    // Remember that Content Must be a string for this version
    SNF_RQST_ARG * arg = snf_request_arg_gen("<Argument Content>");
    SNF_RQST *ServerResponse = snf_request_gen_wUID(
    ClientReq -> UID
    );
    ServerResponse ->OPCODE = op;
    snf_request_arg_insert(ServerResponse, arg);
    /*
    Do Whatever you want to do with Server Response
    */
    SNF_RQST * snf_request_gen_wUID(const char UID[16])
    Generates a new empty request that has an UID.
    void snf_request_arg_insert(SNF_RQST *Request, SNF_RQST_ARG *arg)
    Inserts an arguments to the end of Request's SNF_RQST_ARG List.
    SNF_opcode * OPCODE
    Defines The OPCODE of the request.
    Definition request.h:42
  • snf_request_gen_base (In Case you want to generate a response Using a Base OPCode Command )
    SNF_RQST *ClientReq; // Assuming this is the request client has sent.
    // Generate your Request's Argument ( Assuming we only have one argument)
    // Remember that Content Must be a string for this version
    SNF_RQST_ARG * arg = snf_request_arg_gen("<Argument Content>");
    SNF_RQST *ServerResponse = snf_request_gen_base(
    ClientReq,
    0xXX, // Base Command
    0xXX // Base Detail
    );
    snf_request_arg_insert(ServerResponse, arg);
    /*
    Do Whatever you want to do with Server Response
    */
    SNF_RQST * snf_request_gen_base(SNF_RQST *Original, SNF_opcode_mmbr_t Command, SNF_opcode_mmbr_t Detail)
    Generates a response request using base OPCode.
  • snf_request_genu_base (In Case you want to generate a response Using an Undetailed Base OPCode )
    SNF_RQST *ClientReq; // Assuming this is the request client has sent.
    // Generate your Request's Argument ( Assuming we only have one argument)
    // Remember that Content Must be a string for this version
    SNF_RQST_ARG * arg = snf_request_arg_gen("<Argument Content>");
    SNF_RQST *ServerResponse = snf_request_genu_base(
    ClientReq,
    0xXX, // Base Command
    );
    snf_request_arg_insert(ServerResponse, arg);
    /*
    Do Whatever you want to do with Server Response
    */
    SNF_RQST * snf_request_genu_base(SNF_RQST *Original, SNF_opcode_mmbr_t Command)
    Generates a response request using undetailed base OPCode.

A Server Request

Assuming you want to sent a request to a client (eg: Broadcasting a message or a Server Event) then we can formulate a Server Request using:

Generating Arguments and adding them into a Request

Generating Requests is by simply calling snf_request_arg_gen

SNF_RQST_ARG * arg = snf_request_arg_gen("<Argument Content>");

even adding them is simple just use snf_request_arg_insert

snf_request_arg_insert(ServerRequest, arg);
Note
how ever the more arguments your request already have, the more time it would take to add new arguments you could how even only require to use snf_request_arg_insert once the following way
// First Argument
SNF_RQST_ARG * Head = snf_request_arg_gen("<Argument Content>");
SNF_RQST_ARG * P = Head;
// Argument 2
P ->next = snf_request_arg_gen("<Argument Content>");
P = P ->next;
.
.
.
// Argument N
P ->next = snf_request_arg_gen("<Argument Content>");
P = P ->next;
// Only Add the Head, and Done!
snf_request_arg_insert(ServerRequest, Head);
SNF_RQST_ARG * next
The next argument.
Definition request.h:52

Sending the request

To send the request use snf_request_send

snf_request_send(Client, ServerRequest);
void snf_request_send(SNF_CLT *Client, SNF_RQST *Request)
Send a request to a Client.

Starting the Framework

To Start the Framwork all you have to do is simply call snf_network_init and it will do everything on the background

Warning
Set Everything BEFORE running this function
// Setup your Framework
// Start The Framework
// Do whatever you want.
void snf_network_init()
Initializes the Network Framwork.