◄ Previous: SNF's OPCodes
About Requests
Request are like the packages that would be transferred back and forth between client and server
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.
A Response Request (Standard)
a standard way under normal cicumstances requires 3 things
- Client's Original Request (See Add a command )
- The Response's OPCode ( See Generate OPCodes )
- The Response's argument.
- Note
- 1. Arguments could be NULL
2. For more about generating arguments that go along with the Request Check this
and then we'll call snf_request_gen_response
- Warning
- for the sake for simplicity, no error handling was included in the example
Example We want to write the respondtoclient
function defined in Add a command
{
Request,
op,
arg
);
return ServerResponse;
}
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.
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.
The Structure for saving Requests.
Definition request.h:27
Structure for the opcode.
Definition opcode.h:28
A Response Request (Using Base OPCode)
Instead of using the Standard function for creating a response request you could create a request using another method that requires 3 things
- Client's Original Request (See Add a command )
- The Base Command's defining byte
- The Base Command Detail's defining byte
- Note
- As for aguments follow Generating Arguments
and then we'll call snf_request_gen_base (Or snf_request_genu_base if you don't want the set a detail )
- Warning
- for the sake for simplicity, no error handling was included in the example
Example We want to write the respondtoclient
function defined in Add a command
{
Request,
0xFF,
0x01
);
return ServerResponse;
}
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.
A Custom Request
Assuming you want to sent a request to a client (eg: Broadcasting a message or a Server Event) then we can formulate a Custom Request using: UID will equal NULLREQUEST
- snf_request_gen_server_OPCODE
op
);
SNF_RQST * snf_request_gen_server_OPCODE(SNF_opcode *OPCODE)
Generates a server request.
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_request_gen_wUID
- Note
- 1. UID of the custom request MUST be equal to NULLREQUEST so preferrably use snf_request_gen_server_OPCODE as it's simpler to use unles this is what you want to use
2. If you set the UID of the custom request equal to a client Request's UID then it will make it a "Response Request with extra steps"
);
SNF_RQST * snf_request_gen_wUID(const char UID[16])
Generates a new empty request that has an UID.
#define NULLREQUEST
Requests's Default Request ID.
Definition request.h:15
SNF_opcode * OPCODE
Defines The OPCODE of the request.
Definition request.h:40
Generating Arguments
The arguments for snf 0.0.2-alpha currently require to be string representations, however this will definetly be changed in upcoming versions of the framework and protocol, so for addming you require 1 argument
- The Content
then we'll call snf_request_arg_gen which returns a SNF_RQST_ARG *
Adding Arguments
Adding arguments to a request is easy
For Requests generated using the "Standard Method"
if you want to create your request using the Standard Method you can directly add your first argument directly when declaring your request as the 3rd argument.
- Note
- It is possible to use For requests generated without the "Standard Method" to register your first argument assuming you put NULL as your 3rd argument
See this for adding multiple arguments
For requests generated without the "Standard Method"
Adding your arguments requires you to generate the request first and also your arguments before adding them
and then we add them by calling snf_request_arg_insert
Example
See this for adding multiple arguments
For multiple arguments
Technically you can use snf_request_arg_insert to add as many request as you want and not just the first argument, however if you're adding a bunch of arguments using said function is uneffective as it will keep rereading the Linked list saving the arguments over and over again,
So it's better to follow this algorithm to add them:
- Create your First Argument in a variable
- Add it into your request using either
- Treat the first argument as a head of a linked list and add new argument
..
..
SNF_RQST * snf_request_gen()
Generates a new empty request.
SNF_RQST_ARG * next
The next argument.
Definition request.h:52
- keep repeating Step 3. until you've added all of your arguments then go to Step 5.
- You're done! now your request contains all of your arguments!
Sending Requests
If you're writing an OPCode command's function (Aka. A response function ) then you shouldn't worry about how to send your request, just return it in that function and it will be handled automatically.
However if you're sending a custom request then you must send the request through snf_request_send which requires 2 arguments
- The Client
- The Request to send
- Important
- For snf 0.0.2-alpha you must LOCK the client before Sending any request and must UNLOCK after sending your request. and NEVER DO YOUR OWN TCP COMMUNICATION ON THE SOCKET MANUALLY
Example We want to send a custom request req
...
#include <pthread.h>
int main()
{
...
pthread_mutex_lock(&(clt->
mutex));
pthread_mutex_unlock(&(clt->
mutex));
...
}
void snf_request_send(SNF_CLT *Client, SNF_RQST *Request)
Send a request to a Client.
The structure for each saved client.
Definition clt.h:24
pthread_mutex_t mutex
Mutex used to avoid Race Condition.
Definition clt.h:26
Freeing Requests
If your request is being sent by returning said request, it will be automatically consumed and will be free'd, however for requests sent using snf_request_send must be free'd using snf_request_free which also free's arguments on it's own.
Example
int main()
{
...
...
}
void snf_request_free(SNF_RQST *Request)
Frees a SNF_RQST *.