CRM32Pro SDK  v5.22
List of all members | Public Member Functions
CRM32Pro_INetwork Class Reference

INetwork. More...

Detailed Description

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...
 

Member Function Documentation

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.

Parameters
hosthost name or IP of your server
portport number where is listening your server
username of your client. Must be unique. Maximum of 16 characters.
passwdpassword to connect to your server
Returns
-2 Failed: already connected to a server
-1 Failed: aborted due to ESCAPE key pressed
0 Failed: network is not initialized
1 Succeed (connected to the server)
2 Failed: specified port is not valid
3 Failed: specified server name is not valid
4 Failed: specified user name is not valid
5 Failed: internal error
6 Failed: can not resolve the server name
7,8 & 9 Failed: internal error
10 Failed: client name is not valid
11 Failed: client name is duplicated
12 Failed: clients limit reached
13 Failed: password is not valid
14 Failed: new logins are disabled
15,16,17,18 & 19 Failed: internal error
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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.

Parameters
portthe port to listen all the incoming connections (clients)
passwdinteger with the password to connect to this server
dedicated1 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.
Returns
0 Failed
1 Succeed
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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)

Parameters
featuresInteger with desired flags enabled. It performs a XOR so trying to enable an already enabled flag will disable it.
Returns
Integer with current features
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.

Parameters
bufpointer to received data
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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().

Parameters
cinfoa pointer to the information. See sClientInfo struct to further information.
Returns
number of clients. Used to browse through the received pointer to cinfo
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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...

Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
unsigned char CRM32Pro_INetwork::Init ( char  mode_log)

Initialize the network system.

Parameters
mode_logInternal logging mode: LOG_NULL,LOG_CONSOLE,LOG_FILE,LOG_FILEAPPEND
Returns
0 Failed
1 Succeed
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
unsigned char CRM32Pro_INetwork::QueryClientsInfo ( )

Query to get all current clients with updated information.

Returns
0 Failed.
1 Succeed.
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
unsigned char CRM32Pro_INetwork::QueryKillClient ( )

Query to kill this client.

Returns
0 Failed.
1 Succeed. Next ReceiveData() will return an INETWORK_ERROR
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
unsigned char CRM32Pro_INetwork::QueryKillServer ( )

Query to close the server (and all the connected clients)

Returns
0 Failed.
1 Succeed. Next ReceiveData() will return an INETWORK_ERROR
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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.

Parameters
buffpointer to received data
sizepointer to integer with the size of received data (if any)
Returns
0 if there isn't anydata received. buff and size are set to NULL and -1 respectively.
internal information: INETWORK_CLOSE, INETWORK_NEWCLIENT, INETWORK_QUITCLIENT, INETWORK_CLIENTSINFO, INETWORK_PING or INETWORK_ERROR
user data with INETWORK_DATA,INETWORK_DATAVALIDATED or INETWORK_DATADENIED
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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.

Parameters
buffpointer to your data
sizesize in bytes of your data
isqueryit 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.
Returns
0 Failed.
1 Succeed
Examples:
Example10_NetChat.cpp, and Example11_NetChat2.cpp.
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.

Parameters
myCoreServeryour 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.
Examples:
Example11_NetChat2.cpp.
void CRM32Pro_INetwork::SetSimDelay ( int  iMs,
int  iMode = 0 
)

Simulate a connection delay.

It only works for clients. Could be changed at any time

Parameters
iMssimulated connection delay in milliseconds with this range [0,2500]. By default it's set to 0.
iModeReserved for future usages
void CRM32Pro_INetwork::SetTimeOut ( int  ms)

Set the connection timeout.

By default, the timeout is set to 2000ms

Parameters
msdesired timeout in milliseconds. Minimum is 500ms.

The documentation for this class was generated from the following files: