Shadow Network Framework  0.0.2-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
SNF's Variables

◄ Previous: Tutorials

About Variables

The variables system made here is made to put for the purpose of gathering all variables required for the normal running of the core feeatures of SNF, it does not contain EVERY variable, as of this version do not expect to find all variables needed here. but it is not recommened to not touch any variable that isnt included in this structure as it will result in undefined behaviour.

How to

To use the core headers of snf you can include

#include <SNF.h>

or if you want to include this specific header file only

#include <SNF/vars.h>
this file Defines everything related to Globaly accessed variable

Precautions

The first thing to do before anything is to default the variables structure, which will of cause default the value but also allocated to strucutre,
we can default it by calling snf_var_default after that go and set what ever variable you see fit except those that are advised against manually Setting them

Available Variables

As of 0.0.2-alpha there are 11 variables stated in SNF_VARS

4 of which are advised to not manually be set, which are:

  1. SNF_VAR_THREADPOOL
  2. SNF_VAR_EPOLL_EVENTS
  3. SNF_VAR_INITIALIZED
  4. SNF_VAR_EPOLL_MAXEVENTS

and 1 variable that is not implementedbear no effect:

  1. SNF_VAR_OPCODE_INIS

and that leaves the rest of the variables being allowed to be changed by you -the developper-, which are:

  1. SNF_VAR_THREADS
  2. SNF_VAR_PORT
  3. SNF_VAR_MAX_QUEUE
  4. SNF_VAR_EPOLL_TIMEOUT
  5. SNF_VAR_CLTS_INITIAL (See notes)
  6. SNF_VAR_RQST_MAX_LENGTH

Setting Variables

After initializing you may want to set the variales to your needs and for that you can use snf_var_set Which requires two arguments:

  1. The variable's identifier
  2. The new variable's value

However setting the variables may depend if the type of your variable is registred as a pointer of a Data type or an instance of one, Luckily as of 0.0.2-alpha there will be no need to handle to see what type of the variable you're gonna be setting as you're only advised to change an instance of int ( Or _Atomic int).

Warning
There are Variables that are not avised to be changed after you started your SNF Server, so it's (optionally) recommended to set all your variables at once before anything and only change the variables only when it is allowed to.
for the sake for simplicity, no error handling was included in the example

Eg: let's say we want to set the amount of threads used by SNF to 15 Threads

#include <SNF/vars.h>
int main()
{
// Always make sure you default the variables.
int threads = 3;
...
}
@ SNF_VAR_THREADS
Variable for threads used by SNF
Definition vars.h:30
void snf_var_set(SNF_VARS VARNAME, void *Value)
Allows to set the value of a variable.
void snf_var_default()
Defaults and allocates the variables.

Getting Variables

To get variables there are multiple functions each serving a purpose

Note
If the variable you want to access is an instance of a data type ( Eg: it's type is either int, char, SNF_ht .. etc)
  • Check
    Else if the variable you want to access is a pointer to a data type ( Eg: SNF_thpool *, void *, char *... etc)
  • Check

1. snf_var_geta_void ( SNF_VARS VARNAME )

This is the base function which returns a void **, which refers to a pointer in the variables structure where your variables are stored.

Warning
Never free anything returned from this function manually

See more

2. snf_var_geta ( VARNAME, TYPE )

This is a wrapper macro for snf_var_geta_void which just casts it's return from void ** which cast it onto TYPE ** (TYPE must be a valid type)

Warning
Never free anything returned from this function manually

See more

3. snf_var_get ( VARNAME, TYPE )

This macro dereferences the return of snf_var_geta which returns a TYPE *
this is best used if you are using a variable that is a pointer to a data type. how ever if it was written in SNF_VARS that a variable's TYPE is DATA_TYPE * then put DATA_TYPE as the TYPE

Warning
for the sake for simplicity, no error handling was included in the example

Example we want to get the variable SNF_VAR_THREADPOOL which is of type SNF_thpool * we use

..
..
The structure for a Thread Pool.
Definition thpool.h:69
@ SNF_VAR_THREADPOOL
Variable for saving the threadpool instance
Definition vars.h:37
#define snf_var_get(VARNAME, TYPE)
Derenferences the return of snf_var_geta from TYPE** into TYPE *.
Definition vars.h:116

See more

4. snf_var_getv ( VARNAME, TYPE )

This macro dereferences the return of snf_var_get which returns a TYPE
this is best used if you are using a variable that is an instance of a data type.

Warning
for the sake for simplicity, no error handling was included in the example

Example we want to get the variable SNF_VAR_THREADS which is of type int we use

..
int theads = snf_var_getv(SNF_VAR_THREADS, int);
..
#define snf_var_getv(VARNAME, TYPE)
Dereferences the return of snf_var_get from TYPE * to TYPE.
Definition vars.h:121

See more


Next: SNF's OPCodes ►