CRM32Pro SDK
v5.22
|
INetwork. More...
INetwork.
Network interface.
Public Member Functions | |
unsigned char | ConnectTo (char *host, unsigned short port, char *user, unsigned int passwd) |
Connect to a server. More... | |
unsigned char | CreateServer (unsigned short port, unsigned int passwd, unsigned char dedicated) |
Create a server. More... | |
int | Features (int) |
Set/Get advance feature flags for the network system. More... | |
void | FreeData (char *&buf) |
Free data received from ReceiveData() once you have processed it. You must always use it to avoid memory leaks. More... | |
unsigned char | GetClientsInfo (sClientInfo **cinfo) |
Get current information stored locally about all connected clients. More... | |
unsigned int | GetMyIP () |
Get my IP if any. | |
char * | GetMyUserName () |
Get my user name when I am a client and I am already connected to a server. | |
void | Info () |
Print a lot of useful information. More... | |
unsigned char | Init (char m_log) |
Initialize the network system. More... | |
unsigned char | IsClient () |
Check if we are running as client. | |
unsigned char | IsServer () |
Check if we are running as server or if the server is active. | |
unsigned char | QueryClientsInfo () |
Query to get all current clients with updated information. More... | |
unsigned char | QueryKillClient () |
Query to kill this client. More... | |
unsigned char | QueryKillServer () |
Query to close the server (and all the connected clients) More... | |
void | Quit () |
Close the network system. | |
unsigned char | ReceiveData (char **buf, unsigned int *size) |
Receive data from the server. More... | |
unsigned char | SendData (char *buff, int size, char isquery=1) |
Send data to server using a blocking method. More... | |
void | SetCoreServerCallback (int(*myCoreServer)(void *data, int size)) |
Set a callback function to hook into server thread to evaluate the sent data. More... | |
void | SetSimDelay (int iMs, int iMode=0) |
Simulate a connection delay. More... | |
void | SetTimeOut (int ms) |
Set the connection timeout. More... | |
unsigned char CRM32Pro_INetwork::ConnectTo | ( | char * | host, |
unsigned short | port, | ||
char * | user, | ||
unsigned int | passwd | ||
) |
Connect to a server.
It will try to connect during the current timeout (set by SetTimeOut()) before to return with error. With ESCAPE key it halts the attempt.
host | host name or IP of your server |
port | port number where is listening your server |
user | name of your client. Must be unique. Maximum of 16 characters. |
passwd | password to connect to your server |
unsigned char CRM32Pro_INetwork::CreateServer | ( | unsigned short | port, |
unsigned int | passwd, | ||
unsigned char | dedicated | ||
) |
Create a server.
You only can create one server on your software.
port | the port to listen all the incoming connections (clients) |
passwd | integer with the password to connect to this server |
dedicated | 1 to stop the execution flow of your program and running only as server. 0 to execute it in a separated thread and go on with your program execution flow. |
int CRM32Pro_INetwork::Features | ( | int | features | ) |
Set/Get advance feature flags for the network system.
Check INETFEATURE_x defines to know which features are currently supported.
To avoid to modify any current flag and just get the current ones, use the value of -1 (default value)
features | Integer with desired flags enabled. It performs a XOR so trying to enable an already enabled flag will disable it. |
void CRM32Pro_INetwork::FreeData | ( | char *& | buf | ) |
Free data received from ReceiveData() once you have processed it. You must always use it to avoid memory leaks.
Do not free by yourself this buffer, you must to use this member to free it.
buf | pointer to received data |
unsigned char CRM32Pro_INetwork::GetClientsInfo | ( | sClientInfo ** | cinfo | ) |
Get current information stored locally about all connected clients.
When a new client is connected or disconnected, its informormation will be updated automatically.
So the information table returned by this function will be always valid but the latency.
If you want to know the real latency on a client, you have to call to QueryClientsInfo() before to call this method.
However, if the client is running together with the server, it does not require to call QueryClientsInfo().
cinfo | a pointer to the information. See sClientInfo struct to further information. |
void CRM32Pro_INetwork::Info | ( | ) |
Print a lot of useful information.
Show Client and/or Server internal information: DataNets and bytes sent,received,refused. Incoming queue status...
unsigned char CRM32Pro_INetwork::Init | ( | char | mode_log | ) |
Initialize the network system.
mode_log | Internal logging mode: LOG_NULL,LOG_CONSOLE,LOG_FILE,LOG_FILEAPPEND |
unsigned char CRM32Pro_INetwork::QueryClientsInfo | ( | ) |
Query to get all current clients with updated information.
unsigned char CRM32Pro_INetwork::QueryKillClient | ( | ) |
Query to kill this client.
unsigned char CRM32Pro_INetwork::QueryKillServer | ( | ) |
Query to close the server (and all the connected clients)
unsigned char CRM32Pro_INetwork::ReceiveData | ( | char ** | buff, |
unsigned int * | size | ||
) |
Receive data from the server.
This data may be generated by the server itself or by others clients which send its data to the server.
Incoming data is stored on an internal queue so you can fetch all data elements calling this function until you get a return value of 0.
Important note: data elements are automatically allocated and deallocated but once you have receive any of it using this function, INetwork do not manage anymore its automatic deletion.
So you MUST CALL FreeData() member when you finish to work with it to free it and to avoid a memory leaks.
An optional safe check, on your application side, to avoid data overflows, is to test if received data size is the same as your anticipated data size.
buff | pointer to received data |
size | pointer to integer with the size of received data (if any) |
unsigned char CRM32Pro_INetwork::SendData | ( | char * | buff, |
int | size, | ||
char | isquery = 1 |
||
) |
Send data to server using a blocking method.
If you are on Authoritative Server Mode you have two ways of sending data:
- by default, the sent data will be evaluated by CoreServer to decide if refused the data or to send it to all clients.
- the sent data will bypass the CoreServer as if we were on Non-Authoritative Server Mode.
On Non-Authoritative Server Mode, the data will be always sent to all clients.
buff | pointer to your data |
size | size in bytes of your data |
isquery | it only has sense when you are on Authoritative Server Mode. By default is 1 and means that the data will be evaluated by CoreServer callback. Any other value will bypass the CoreServer callback. |
void CRM32Pro_INetwork::SetCoreServerCallback | ( | int(*)(void *data, int size) | myCoreServer | ) |
Set a callback function to hook into server thread to evaluate the sent data.
If you use a callback function, the INetwork will work on Authoritative Server Mode.
But if you dont set it or set it to NULL, the INetwork will work on Non-Authoritative Server Mode.
The code of this function will be executed on the server thread so keep this in mind.
myCoreServer | your callback function. It takes a pointer to the sent data and its size. A safe method is always check that size is equal to your sent struct data. It has to return 0 when the sent data is evaluated to denied so the data will not sent to all clients. And when the data is evaluated to validated, it has to return 1. On both cases, the source client will be notified with the denied(INETWORK_DATADENIED) or validated(INETWORK_DATAVALIDATED) signal Any other value will do nothing. |
void CRM32Pro_INetwork::SetSimDelay | ( | int | iMs, |
int | iMode = 0 |
||
) |
Simulate a connection delay.
It only works for clients. Could be changed at any time
iMs | simulated connection delay in milliseconds with this range [0,2500]. By default it's set to 0. |
iMode | Reserved for future usages |
void CRM32Pro_INetwork::SetTimeOut | ( | int | ms | ) |
Set the connection timeout.
By default, the timeout is set to 2000ms
ms | desired timeout in milliseconds. Minimum is 500ms. |