Shadow Network Framework  0.0.1-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
network.h
Go to the documentation of this file.
1//////////////////////////////////////////////////////////////
2///
3/// \file network.h
4/// This file Defines everything related to networking
5///
6/// //////////////////////////////////////////////////////////
7#ifndef NETWORK_H
8#define NETWORK_H
9
10/// @brief Defines the library's version
11#define _SNF_VER "0.0.1-alpha"
12
13#include <stdlib.h>
14#include <stdio.h>
15#include <stdint.h>
16#include <strings.h>
17#include <arpa/inet.h>
18
19#include <SNF/SNF.h>
20#include <SNF/clt.h>
21#include <SNF/thpool.h>
22
23/// @brief The thread pool that controls the working of the Framework
24/// @warning Do not modify the contents unless by using Appropriate functions.
25extern thpool *Ntwrk;
26/// @brief Defines the Framework's TCP/IP's port
27extern int SNF_PORT;
28/// @brief Max Queue allowed to wait to be accepted at once
29extern int SNF_MAX_QUEUE;
30
31/// @brief the main Socket that accepts new incoming connections
32/// @warning Do not modify it's contents unless you know what you're doing.
33extern int SNF_SERVER_SOCKET;
34/// @brief Structure that saves the Server's sockaddr_in
35/// @warning Do not modify it's contents unless you know what you're doing.
36extern struct sockaddr_in SNF_SERVER_ADDR;
37/// @brief Structure that saves the Client's sockaddr_in
38/// @warning Do not modify it's contents unless you know what you're doing.
39extern struct sockaddr_in SNF_CLIENT_ADDR;
40/// @brief Used for accept's length argument.
41/// @warning Do not modify it's contents unless you know what you're doing.
42extern socklen_t SNF_CLIENT_LEN;
43
44/// @brief Macro that defines the Total data tranferred so far
45/// @warning Innacurate for now
46#define SNF_Total_Data (SNF_Total_Data_Rcv + SNF_Total_Data_Snt)
47/// @brief Saves the Total data received so far
48/// @warning Innacurate for now
49extern _Atomic uint64_t SNF_Total_Data_Rcv;
50/// @brief Saves the Total data sent so far
51/// @warning Innacurate for now
52extern _Atomic uint64_t SNF_Total_Data_Snt;
53
54/// @brief Character that sceparates each argument (To be Removed)
55#define UNIT_SCEPARATOR "\x1F"
56
57/// @brief Initializes the Network Framwork
58extern void snf_network_init();
59/// @brief See \ref snf_thpool_join (Works on the \ref Ntwrk Thread Pool)
60extern void snf_network_join();
61
62/// @brief Sends a Buffer to Client's \ref SNF_CLT::sock
63/// @param Client Pointer to the Client's SNF_CLT instance
64/// @param Buffer Buffer to send
65/// @param _Size Size of the Buffer (See Note )
66/// @return The following possible results:
67/// * **-1** **Client** Could be NULL, or In case of an Error , Check **errno** and compare it to **send**'s possible Errors, if errno == EPIPE then ignore it as it was already dealt with.
68/// * **int** The amount of data sent.
69/// @note The <strong>_Size</strong> parameter could react differently depeding on it's value
70/// * If <strong>_Size</strong> < 0 then it will assume the **Buffer** is a string and send 4 bytes indicating the size (See \ref snf_uint32_to_bytes on how those 4 bytes are written ) and then send the buffer.
71/// * else it will send the **Buffer** and the length will be <strong>_Size</strong>
72extern int snf_snd(SNF_CLT *Client, const char *Buffer, int _Size);
73
74/// @brief Receives a Buffer from Client's \ref SNF_CLT::sock
75/// @param Client Pointer to the Client's SNF_CLT instance
76/// @param Buffer Received Buffer
77/// @param _Size Expected Size
78/// @param _Flags Flags that will be passed to recv()
79/// @return The following possible results:
80/// * **-1** **Client** Could be NULL, or In case of an Error , Check **errno** and compare it to **send**'s possible Errors, if errno == EPIPE then ignore it as it was already dealt with.
81/// * **int** The amount of data sent.
82/// @warning If you receive *n* amount of bytes and is lower than <strong>_Size</strong>, it will assume that recv has failed and will check errno, as it could disconnect the client in somecases and the function will return **-1**.
83/// @note There is are re-definitions of snf_recv_ and are:
84/// * <strong> \ref snf_rcv() </strong><br>
85/// * <strong> \ref snf_rcv_PEEK() </strong><br>
86extern int snf_rcv_(SNF_CLT *Client, void *Buffer, int _Size, int _Flags);
87///@brief Same as \ref snf_rcv_ how ever <strong>_Flags</strong> will be 0
88#define snf_rcv(Client, Buffer, _Size) snf_rcv_(Client, Buffer, _Size, 0)
89///@brief Same as \ref snf_rcv_ how ever <strong>_Flags</strong> will be MSG_PEEK
90#define snf_rcv_PEEK(Client, Buffer, _Size) snf_rcv_(Client, Buffer, _Size, MSG_PEEK)
91
92#endif
This file includes necessary Checks and includes the the nesessary libraries of the library.
This file Defines everything related to handling Clients.
thpool * Ntwrk
The thread pool that controls the working of the Framework.
_Atomic uint64_t SNF_Total_Data_Snt
Saves the Total data sent so far.
struct sockaddr_in SNF_CLIENT_ADDR
Structure that saves the Client's sockaddr_in.
struct sockaddr_in SNF_SERVER_ADDR
Structure that saves the Server's sockaddr_in.
int snf_snd(SNF_CLT *Client, const char *Buffer, int _Size)
Sends a Buffer to Client's SNF_CLT::sock.
_Atomic uint64_t SNF_Total_Data_Rcv
Saves the Total data received so far.
int SNF_MAX_QUEUE
Max Queue allowed to wait to be accepted at once.
socklen_t SNF_CLIENT_LEN
Used for accept's length argument.
int SNF_SERVER_SOCKET
the main Socket that accepts new incoming connections
int snf_rcv_(SNF_CLT *Client, void *Buffer, int _Size, int _Flags)
Receives a Buffer from Client's SNF_CLT::sock.
void snf_network_init()
Initializes the Network Framwork.
int SNF_PORT
Defines the Framework's TCP/IP's port.
void snf_network_join()
See snf_thpool_join (Works on the Ntwrk Thread Pool)
The structure for each saved client.
Definition clt.h:25
The structure for a Thread Pool.
Definition thpool.h:47
this file Defines everything related to ThreadPool