No products in the cart.
👉documentation
This document is a functional description of some functions that are not commonly used in the GameShield sdk. These functions may be useful for your project in specific scenarios. Before reading this document, please read the sdk description of another general function, which describes the principles of the sdk and how to integrate it more clearly. We have also made sample projects for advanced functions, see the projects under “demos advanced functions”.
👉Resolving Port Conflicts
After years of observation and statistics, the port conflict problem only occurs in individual client devices and the probability of occurrence and low. However, in order to solve this problem completely, we still recommend you to use our advanced functions to solve this problem completely. Because our sdk is based on your configuration of the “forwarding rules” in the client device to open the appropriate port, which has a very low probability of individual client port conflict.
There are two types of port conflict scenarios:
✨The first scenario: A port you have configured is already occupied by another application on an individual client, so that user cannot use your application normally
✨Second scenario: you have multiple apps using the same instance, and users sometimes open multiple of your apps at the same time on the same device; the first app the user opens has all the ports of your instance occupied, and the second app opens without being able to open those ports.
This problem is fine on PC, the second open app business is normal, because the second app will connect to the sdk through the first app, but on mobile there will be a problem, the second app is normal at the beginning, because the second app will connect to the sdk through the first app, the first app will be cut to the background while the user is using the second app, and the first app may be hibernated by the system after a while. The first app will be cut to the background while the user is using the second app, after a while the first app may be hibernated by the system and then the second app will not be able to connect.
✅Solving this problem is also very simple, the code process is as follows👇:
dunSetAutoChangePort(1);//
int ret = clinkStart(key);//Start shield, some systems use start, see separate document for details, only need to call it once
//Call the following line of code when you need to create a new connection to get the new port
int new_tcpPort = dunGetCurrentTCPPort("127.0.10.21", 600);//Get the port corresponding to the new tcp
//Next use new_tcpPort to connect to the application
//connect("127.0.10.21", new_tcpPort) For example, this line of pseudo-code
For example, if the forwarding rule is configured to forward 127.0.10.21:600 to 202.23.56.9:600, the pseudo-code for the comparison of the port conflict before and after integrating Shield and resolving the port conflict is as follows👇:
Connection before shield integration connect("202.23.56.9",600)
Connection after integrating the shield connect("127.0.10.21", dunGetCurrentTCPPort("202.23.56.9",600))
👉Set whether to automatically replace conflicting ports.
This function must be called before calling shield start to be valid.
function prototype:void dunSetAutoChangePort(int val)
Parameter: val: Whether to change automatically.
✨Function: When val is set to 1, if a port is detected to be occupied or not opened at startup, a random port will be automatically generated and associated with the original port, so that your program can get the new port by using dunGetCurrentTCPPort or dunGetCurrentUDPPort functions provided by sdk to get the new port and use it to connect. dunGetCurrentUDPPort function provided by sdk to get the newly generated port, and use the new port to connect, which solves the port conflict problem.
Below are the sdk function definitions for each system, please refer to the items in “demo Advanced Functions” for specific call examples.👇:
✅Windows:
extern "C" CLINKAPI_API void dunSetAutoChangePort(int val);
✅android:
类:com.dun.clinkapi.Api
public native void dunSetAutoChangePort(int val);
✅IOS static library:
类:ClinkAPI
void dunSetAutoChangePort(int val);
✅IOS dynamic library:
extern "C" void dunSetAutoChangePort(int val);
✅Unity
类:ClinkSDKForUnity
public static void dunSetAutoChangePort(int val)
✅Uni-app
Plugin name: dunClinkApiForUni
plugin function: dunSetAutoChangePort(val)
👉Get the current tcp port
Before making a new tcp connection you need to call this function to get the new port corresponding to the port you are going to connect to, because your original port may have a port conflict and sdk has replaced it with another port.
function prototype:int dunGetCurrentTCPPort(const char* ip, int port)
Parameters: ip: IP of the client connection in the forwarding rule, that is, the IP beginning with 127.
port: port of the client connection in the forwarding rule, that is, the corresponding port of the IP beginning with 127.
return: the new port, if there is no conflict in the incoming port or the forwarding rule does not exist, the returned port will be the same as the incoming port, if there is a conflict, the returned port will be the new port after the change.
The following are the corresponding sdk function definitions for each system, please refer to the items in the “demo Advanced Functions” for specific call examples.👇:
✅Windows:
extern "C" CLINKAPI_API int dunGetCurrentTCPPort(const char* ip, int port);
✅Android:
类:com.dun.clinkapi.Api
public native int dunGetCurrentTCPPort(String ip,int port);
✅IOS Static Libraries:
类:ClinkAPI
int dunGetCurrentTCPPort(const char* ip, int port);
✅IOS Dynamic Libraries:
extern "C" void dunGetCurrentTCPPort(int val);
✅Unity
类:ClinkSDKForUnity
public static int dunGetCurrentTCPPort(string ip, int port)
✅Uni-app
Plugin name: dunClinkApiForUni
plugin function: dunGetCurrentTCPPort(ip,port)
👉Get the current udp port
Before making a new udp connection you need to call this function to get the new port corresponding to the port you are going to connect to, because your old port may have a port conflict and sdk has replaced it with another port.
function prototype:int dunGetCurrentUDPPort(const char* ip, int port)
Parameters: ip: IP of the client connection in the forwarding rule, that is, the IP starting with 127.
port: port of the client connection in the forwarding rule, that is, the corresponding port of the IP starting with 127.
return: the new port, if there is no conflict in the incoming port or the forwarding rule does not exist, the returned port will be the same as the incoming port, if there is a conflict, the returned port will be the new port after the change.
The following are the corresponding sdk function definitions for each system, please refer to the items in the “demo Advanced Functions” for specific call examples👇:
✅Windows:
extern "C" CLINKAPI_API int dunGetCurrentUDPPort(const char* ip, int port);
✅Android:
类:com.dun.clinkapi.Api
public native int dunGetCurrentUDPPort(String ip,int port);
✅IOS static libraries:
类:ClinkAPI
int dunGetCurrentUDPPort(const char* ip, int port);
✅IOS Dynamic Libraries:
extern "C" void dunGetCurrentUDPPort(int val);
✅Unity
类:ClinkSDKForUnity
public static int dunGetCurrentUDPPort(string ip, int port)
✅Uni-app
plugin name: dunClinkApiForUni
plugin function: dunGetCurrentUDPPort(ip,port)
👉Get the client IP:
It is used to get the current IP of the client in the client side. no matter what the “Get IP method” in the forwarding rule is set to, this interface is valid, this is obtained in the client side, and the forwarding rule is obtained for your server side, there is no relationship between the two, only the IP of the client is the same.
🌐For example, you can send the login information together with the client IP to the login server when the user logs in, so that you can get the real IP of the client if your back-end server has forwarded it many times.
Function: long long dunGetClientIP()
Returns: a signed 64-bit integer IP value, which can be converted to a string IP by other functions, and returns 0, indicating that the shield has not started or has not started successfully.
The algorithm code for converting an integer IP value to a string IP value is as follows:👇
string ipstr = ip / 256 / 256 / 256 % 256 + "." + ip / 256 / 256 % 256 + "." + ip / 256 % 256 + "." + ip % 256;
Below are the sdk function definitions for each system, please refer to the items in “demo Advanced Functions” for specific call examples.👇:
✅Windows:
extern "C" CLINKAPI_API long long dunGetClientIP();
✅Android:
类:com.dun.clinkapi.Api
public native long dunGetClientIP();
✅IOS Static Libraries:
类:ClinkAPI
long long dunGetClientIP();
✅IOS Dynamic Libraries:
extern "C" long long dunGetClientIP();
✅Unity
类:ClinkSDKForUnity
public static long dunGetClientIP()
✅Uni-app
Plugin name: dunClinkApiForUni
plugin function: dunGetClientIP