Raised This Month: $51 Target: $400
 12% 

[HELP] Lag caused by sockets(Get Country &City)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 09-03-2016 , 17:55   [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #1

So i'm making a plugin that show's on connect Country and City.
Before you ask me why don't you use geoip it's because it returns always an error in country and city.
i will give you an example:
My plugin will return about 10% error in City 100% correct country.
Using geoip will return 60% error in City 80% Error in the country.
( IT'S JUST AN EXAMPLE )

So is there a way to remove the lag/bug or to reduce it? ( I've tested on a full server 32/32 ) and when 2 players connect at the same time server bug and freeze for 3 or 2 seconds.

Code=
Code:
/* Plugin generated by AMXX-EDITOR v1.9*/

#include <amxmodx>
#include <amxmisc>
#include <sockets>
#include <regex>

#define REPEAT_TIME 15
#define hostConnect "freegeoip.net"
   
new const errorReturn[]            =    "error";
new gSocket, count = 0

new const GeoIP_country_name[][] =
{ "N/A",
    "Asia/Pacific Region",                         "Europe",
    "Andorra",                                     "United Arab Emirates",
    "Afghanistan",                                 "Antigua and Barbuda",
    "Anguilla",
    "Albania",                                     "Armenia",
    "Curacao",
    "Angola",
    "Antarctica",                                  "Argentina",
    "American Samoa",                              "Austria",
    "Australia",                                   "Aruba",
    "Azerbaijan",
    "Bosnia and Herzegovina",                      "Barbados",
    "Bangladesh",
    "Belgium",                                     "Burkina Faso",
    "Bulgaria",                                    "Bahrain",
    "Burundi",                                     "Benin",
    "Bermuda",
    "Brunei Darussalam",                           "Bolivia",
    "Brazil",
    "Bahamas",                                     "Bhutan",
    "Bouvet Island",                               "Botswana",
    "Belarus",                                     "Belize",
    "Canada",
    "Cocos (Keeling) Islands",
    "Congo, The Democratic Republic of the",
    "Central African Republic",
    "Congo",                                       "Switzerland",
    "Cote D'Ivoire",                               "Cook Islands",
    "Chile",                                       "Cameroon",
    "China",
    "Colombia",                                    "Costa Rica",
    "Cuba",
    "Cape Verde",                                  "Christmas Island",
    "Cyprus",                                      "Czech Republic",
    "Germany",                                     "Djibouti",
    "Denmark",
    "Dominica",                                    "Dominican Republic",
    "Algeria",
    "Ecuador",                                     "Estonia",
    "Egypt",                                       "Western Sahara",
    "Eritrea",                                     "Spain",
    "Ethiopia",
    "Finland",                                     "Fiji",
    "Falkland Islands (Malvinas)",
    "Micronesia, Federated States of",             "Faroe Islands",
    "France",                                      "Sint Maarten (Dutch part)",
    "Gabon",                                       "United Kingdom",
    "Grenada",
    "Georgia",                                     "French Guiana",
    "Ghana",
    "Gibraltar",                                   "Greenland",
    "Gambia",                                      "Guinea",
    "Guadeloupe",                                  "Equatorial Guinea",
    "Greece",
    "South Georgia and the South Sandwich Islands","Guatemala",
    "Guam",
    "Guinea-Bissau",                               "Guyana",
    "Hong Kong",
    "Heard Island and McDonald Islands",
    "Honduras",                                    "Croatia",
    "Haiti",
    "Hungary",                                     "Indonesia",
    "Ireland",
    "Israel",                                      "India",
    "British Indian Ocean Territory",              "Iraq",
    "Iran, Islamic Republic of",                   "Iceland",
    "Italy",
    "Jamaica",                                     "Jordan",
    "Japan",
    "Kenya",                                       "Kyrgyzstan",
    "Cambodia",                                    "Kiribati",
    "Comoros",                                     "Saint Kitts and Nevis",
    "Korea, Democratic People's Republic of",      "Korea, Republic of",
    "Kuwait",
    "Cayman Islands",
    "Kazakhstan",
    "Lao People's Democratic Republic",
    "Lebanon",                                     "Saint Lucia",
    "Liechtenstein",                               "Sri Lanka",
    "Liberia",
    "Lesotho",                                     "Lithuania",
    "Luxembourg",
    "Latvia",                                      "Libya",
    "Morocco",                                     "Monaco",
    "Moldova, Republic of",                        "Madagascar",
    "Marshall Islands",                            "Macedonia",
    "Mali",
    "Myanmar",
    "Mongolia",                                    "Macau",
    "Northern Mariana Islands",                    "Martinique",
    "Mauritania",                                  "Montserrat",
    "Malta",
    "Mauritius",                                   "Maldives",
    "Malawi",
    "Mexico",                                      "Malaysia",
    "Mozambique",                                  "Namibia",
    "New Caledonia",                               "Niger",
    "Norfolk Island",
    "Nigeria",                                     "Nicaragua",
    "Netherlands",
    "Norway",                                      "Nepal",
    "Nauru",                                       "Niue",
    "New Zealand",                                 "Oman",
    "Panama",
    "Peru",                                        "French Polynesia",
    "Papua New Guinea",
    "Philippines",                                 "Pakistan",
    "Poland",                                      "Saint Pierre and Miquelon",
    "Pitcairn Islands",                            "Puerto Rico",
    "Palestinian Territory",                       "Portugal",
    "Palau",
    "Paraguay",
    "Qatar",                                       "Reunion",
    "Romania",                                     "Russian Federation",
    "Rwanda",                                      "Saudi Arabia",
    "Solomon Islands",                             "Seychelles",
    "Sudan",
    "Sweden",
    "Singapore",                                   "Saint Helena",
    "Slovenia",                                    "Svalbard and Jan Mayen",
    "Slovakia",                                    "Sierra Leone",
    "San Marino",
    "Senegal",                                     "Somalia",
    "Suriname",
    "Sao Tome and Principe",
    "El Salvador",                                 "Syrian Arab Republic",
    "Swaziland",                                   "Turks and Caicos Islands",
    "Chad",                                        "French Southern Territories",
    "Togo",
    "Thailand",
    "Tajikistan",
    "Tokelau",                                     "Turkmenistan",
    "Tunisia",                                     "Tonga",
    "Timor-Leste",                                 "Turkey",
    "Trinidad and Tobago",                         "Tuvalu",
    "Taiwan",
    "Tanzania, United Republic of",
    "Ukraine",                                     "Uganda",
    "United States Minor Outlying Islands",        "United States",
    "Uruguay",                                     "Uzbekistan",
    "Holy See (Vatican City State)",
    "Saint Vincent and the Grenadines",
    "Venezuela",
    "Virgin Islands, British",
    "Virgin Islands, U.S.",                        "Vietnam",
    "Vanuatu",                                     "Wallis and Futuna",
    "Samoa",                                       "Yemen",
    "Mayotte",
    "Serbia",                                      "South Africa",
    "Zambia",
    "Montenegro",                                  "Zimbabwe",
    "Anonymous Proxy",                             "Satellite Provider",
    "Other",                                       "Aland Islands",
    "Guernsey",
    "Isle of Man",                                 "Jersey",
    "Saint Barthelemy",
    "Saint Martin",
    "Bonaire, Saint Eustatius and Saba",
    "South Sudan",                                 "Other" 
};  

public plugin_init(){
    register_plugin(
        .plugin_name = "Connect Plugin based on Web API",
        .version = "1.0",
        .author = "Spawner/Freezo")
   
}

public client_connect(id)
{
    if(!is_user_bot(id))
        loadSocket(id)
}

public loadSocket(id){
   
    new errorReturned;
   
    gSocket = socket_open(hostConnect, 80, SOCKET_TCP, errorReturned)
   
   
    if(errorReturned == 0){
       
        new sBuffer[128], IP[24] 
       
        get_user_ip(
                            id,
                            IP,
                            charsmax(IP),
                            .without_port= 1
        )
       
        format(sBuffer, 127, "GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n", IP, hostConnect)
       
        if(socket_send(gSocket, sBuffer, 127)){
           
            set_task(1.0, "answerSocket", id, _, _, "a", REPEAT_TIME)
        }
       
    }
   
    return PLUGIN_HANDLED
}

public answerSocket(id){
   
    if(count != REPEAT_TIME){
        if(socket_change(gSocket)){
            new socketData[1024]
           
            socket_recv(gSocket, socketData, charsmax(socketData))
            new szName[32]
            get_user_name(id, szName, charsmax(szName))
           
            new szSteam[32]
            get_user_authid(id, szSteam, charsmax(szSteam))
           
            new num, error[128]
            new pattern[] = "city.+(?=zip)"
            new Regex:regexref = regex_match(socketData,pattern,num,error,127)
            new matchString[64]
           
            if(regexref >= REGEX_OK)
            {
                for(new i=0; i<num; i++)
                {
                    regex_substr(regexref, i, matchString, charsmax(matchString))

                    new const replaceC[][] = { "^"" , "city", ",", ":"}

                    for(new i = 0; i < sizeof(replaceC); i++)
                        replace_all(matchString, charsmax(matchString), replaceC[i], "")
                   
                }
                if(equal(matchString,""))    matchString = errorReturn;
               
                regex_free(regexref)
            }
           
           
           
            for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
                if(containi(socketData, GeoIP_country_name[i]) != -1 ){
                    ChatColor(0, "!g[NG] !yPlayer !g%s!t[!g%s!t] !yhas connected from !t[!g%s!t]!y->!t[!g%s!t]",szName, szSteam,  GeoIP_country_name[i], matchString )
                   
                    break;
                }
            }
           
           

            socket_close(gSocket)
            remove_task(id)
        }
    }
    else{
        socket_close(gSocket)
        remove_task(id)
    }
    count++
   
    return PLUGIN_HANDLED
}  
stock ChatColor(const id, const input[], any:...)
{
    new count2 = 1, players[32]
    static msg[191]
    vformat(msg, 190, input, 3)
   
    replace_all(msg, 190, "!g", "^4") // Green Color
    replace_all(msg, 190, "!y", "^1") // Default Color
    replace_all(msg, 190, "!t", "^3") // Team Color
    replace_all(msg, 190, "!t2", "^0") // Team2 Color
   
    if (id) players[0] = id; else get_players(players, count2, "ch")
    {
    for (new i = 0; i < count2; i++)
    {
        if (is_user_connected(players[i]))
        {
            message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
            write_byte(players[i]);
            write_string(msg);
            message_end();
        }
    }
}
}

Last edited by Freezo Begin; 09-07-2016 at 07:28.
Freezo Begin is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 09-03-2016 , 18:45   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #2

Sorry but that's dumb, you REALLY should use the module. I don't know why you're getting so many errors when a player connects because GeoIP database is updated once a month.
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 09-03-2016 , 18:54   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #3

Sorry this must be in Scripting help.
Yea i agree with you, but this is the only solution that i (have/did think of).

geoip returns :

Code:
Name | Country
KraDenss^  | error
Ruch 1B !!?  | error
DeaDLocWK_'  | error
PRoDo  | Morocco
medwo  | error
...

Last edited by Freezo Begin; 09-03-2016 at 18:55.
Freezo Begin is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 09-03-2016 , 19:04   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #4

Can you show me your code or show the IP address of one of the players that shows a error?
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
pupdebox
Senior Member
Join Date: Feb 2016
Location: Wakanda
Old 09-03-2016 , 19:08   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #5

Instead of getting data at the same time, place players in queue? This will reduce the lag. Great plugin btw.

Edit: Something like that. I also think that you should check is_user_connected for "public answerSocket(id)" Players can drop anytime
PHP Code:
/* Plugin generated by AMXX-EDITOR v1.9*/

#include <amxmodx>
#include <amxmisc>
#include <sockets>
#include <regex>

#define REPEAT_TIME 15
#define hostConnect "freegeoip.net"
   
new const errorReturn[]            =    "error";
new 
gSocketcount 0
new bool:gwait

new const GeoIP_country_name[][] =
"N/A",
    
"Asia/Pacific Region",                         "Europe",
    
"Andorra",                                     "United Arab Emirates",
    
"Afghanistan",                                 "Antigua and Barbuda",
    
"Anguilla",
    
"Albania",                                     "Armenia",
    
"Curacao",
    
"Angola",
    
"Antarctica",                                  "Argentina",
    
"American Samoa",                              "Austria",
    
"Australia",                                   "Aruba",
    
"Azerbaijan",
    
"Bosnia and Herzegovina",                      "Barbados",
    
"Bangladesh",
    
"Belgium",                                     "Burkina Faso",
    
"Bulgaria",                                    "Bahrain",
    
"Burundi",                                     "Benin",
    
"Bermuda",
    
"Brunei Darussalam",                           "Bolivia",
    
"Brazil",
    
"Bahamas",                                     "Bhutan",
    
"Bouvet Island",                               "Botswana",
    
"Belarus",                                     "Belize",
    
"Canada",
    
"Cocos (Keeling) Islands",
    
"Congo, The Democratic Republic of the",
    
"Central African Republic",
    
"Congo",                                       "Switzerland",
    
"Cote D'Ivoire",                               "Cook Islands",
    
"Chile",                                       "Cameroon",
    
"China",
    
"Colombia",                                    "Costa Rica",
    
"Cuba",
    
"Cape Verde",                                  "Christmas Island",
    
"Cyprus",                                      "Czech Republic",
    
"Germany",                                     "Djibouti",
    
"Denmark",
    
"Dominica",                                    "Dominican Republic",
    
"Algeria",
    
"Ecuador",                                     "Estonia",
    
"Egypt",                                       "Western Sahara",
    
"Eritrea",                                     "Spain",
    
"Ethiopia",
    
"Finland",                                     "Fiji",
    
"Falkland Islands (Malvinas)",
    
"Micronesia, Federated States of",             "Faroe Islands",
    
"France",                                      "Sint Maarten (Dutch part)",
    
"Gabon",                                       "United Kingdom",
    
"Grenada",
    
"Georgia",                                     "French Guiana",
    
"Ghana",
    
"Gibraltar",                                   "Greenland",
    
"Gambia",                                      "Guinea",
    
"Guadeloupe",                                  "Equatorial Guinea",
    
"Greece",
    
"South Georgia and the South Sandwich Islands","Guatemala",
    
"Guam",
    
"Guinea-Bissau",                               "Guyana",
    
"Hong Kong",
    
"Heard Island and McDonald Islands",
    
"Honduras",                                    "Croatia",
    
"Haiti",
    
"Hungary",                                     "Indonesia",
    
"Ireland",
    
"Israel",                                      "India",
    
"British Indian Ocean Territory",              "Iraq",
    
"Iran, Islamic Republic of",                   "Iceland",
    
"Italy",
    
"Jamaica",                                     "Jordan",
    
"Japan",
    
"Kenya",                                       "Kyrgyzstan",
    
"Cambodia",                                    "Kiribati",
    
"Comoros",                                     "Saint Kitts and Nevis",
    
"Korea, Democratic People's Republic of",      "Korea, Republic of",
    
"Kuwait",
    
"Cayman Islands",
    
"Kazakhstan",
    
"Lao People's Democratic Republic",
    
"Lebanon",                                     "Saint Lucia",
    
"Liechtenstein",                               "Sri Lanka",
    
"Liberia",
    
"Lesotho",                                     "Lithuania",
    
"Luxembourg",
    
"Latvia",                                      "Libya",
    
"Morocco",                                     "Monaco",
    
"Moldova, Republic of",                        "Madagascar",
    
"Marshall Islands",                            "Macedonia",
    
"Mali",
    
"Myanmar",
    
"Mongolia",                                    "Macau",
    
"Northern Mariana Islands",                    "Martinique",
    
"Mauritania",                                  "Montserrat",
    
"Malta",
    
"Mauritius",                                   "Maldives",
    
"Malawi",
    
"Mexico",                                      "Malaysia",
    
"Mozambique",                                  "Namibia",
    
"New Caledonia",                               "Niger",
    
"Norfolk Island",
    
"Nigeria",                                     "Nicaragua",
    
"Netherlands",
    
"Norway",                                      "Nepal",
    
"Nauru",                                       "Niue",
    
"New Zealand",                                 "Oman",
    
"Panama",
    
"Peru",                                        "French Polynesia",
    
"Papua New Guinea",
    
"Philippines",                                 "Pakistan",
    
"Poland",                                      "Saint Pierre and Miquelon",
    
"Pitcairn Islands",                            "Puerto Rico",
    
"Palestinian Territory",                       "Portugal",
    
"Palau",
    
"Paraguay",
    
"Qatar",                                       "Reunion",
    
"Romania",                                     "Russian Federation",
    
"Rwanda",                                      "Saudi Arabia",
    
"Solomon Islands",                             "Seychelles",
    
"Sudan",
    
"Sweden",
    
"Singapore",                                   "Saint Helena",
    
"Slovenia",                                    "Svalbard and Jan Mayen",
    
"Slovakia",                                    "Sierra Leone",
    
"San Marino",
    
"Senegal",                                     "Somalia",
    
"Suriname",
    
"Sao Tome and Principe",
    
"El Salvador",                                 "Syrian Arab Republic",
    
"Swaziland",                                   "Turks and Caicos Islands",
    
"Chad",                                        "French Southern Territories",
    
"Togo",
    
"Thailand",
    
"Tajikistan",
    
"Tokelau",                                     "Turkmenistan",
    
"Tunisia",                                     "Tonga",
    
"Timor-Leste",                                 "Turkey",
    
"Trinidad and Tobago",                         "Tuvalu",
    
"Taiwan",
    
"Tanzania, United Republic of",
    
"Ukraine",                                     "Uganda",
    
"United States Minor Outlying Islands",        "United States",
    
"Uruguay",                                     "Uzbekistan",
    
"Holy See (Vatican City State)",
    
"Saint Vincent and the Grenadines",
    
"Venezuela",
    
"Virgin Islands, British",
    
"Virgin Islands, U.S.",                        "Vietnam",
    
"Vanuatu",                                     "Wallis and Futuna",
    
"Samoa",                                       "Yemen",
    
"Mayotte",
    
"Serbia",                                      "South Africa",
    
"Zambia",
    
"Montenegro",                                  "Zimbabwe",
    
"Anonymous Proxy",                             "Satellite Provider",
    
"Other",                                       "Aland Islands",
    
"Guernsey",
    
"Isle of Man",                                 "Jersey",
    
"Saint Barthelemy",
    
"Saint Martin",
    
"Bonaire, Saint Eustatius and Saba",
    
"South Sudan",                                 "Other" 
};  

public 
plugin_init(){
    
register_plugin(
        .
plugin_name "Connect Plugin based on Web API",
        .
version "1.0",
        .
author "Spawner/Freezo")
   
}

public 
client_connect(id)
{
    if(!
is_user_bot(id) && !gwait){
    
loadSocket(id)
    
gwait true
    
}
    else if(!
is_user_bot(id) && gwait){
    
set_task(1.0"is_ok"id__"b")
    }
}

public 
is_ok(id)
{
    if(
is_user_connected(id) && !gwait){
    
remove_task(id)
    
gwait true
    loadSocket
(id)
    }
    else if(!
is_user_connected(id))
    
remove_task(id)
}

public 
loadSocket(id){
   
    new 
errorReturned;
   
    
gSocket socket_open(hostConnect80SOCKET_TCPerrorReturned)
   
   
    if(
errorReturned == 0){
       
        new 
sBuffer[128], IP[24
       
        
get_user_ip(
                            
id,
                            
IP,
                            
charsmax(IP),
                            .
without_port1
        
)
       
        
format(sBuffer127"GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n"IPhostConnect)
       
        if(
socket_send(gSocketsBuffer127)){
           
            
set_task(1.0"answerSocket"id__"a"REPEAT_TIME)
        }
       
    }
   
    return 
PLUGIN_HANDLED
}

public 
answerSocket(id){
   
    if(
count != REPEAT_TIME){
        if(
socket_change(gSocket)){
            new 
socketData[1024]
           
            
socket_recv(gSocketsocketDatacharsmax(socketData))
            new 
szName[32]
            
get_user_name(idszNamecharsmax(szName))
           
            new 
szSteam[32]
            
get_user_authid(idszSteamcharsmax(szSteam))
           
            new 
numerror[128]
            new 
pattern[] = "city.+(?=zip)"
            
new Regex:regexref regex_match(socketData,pattern,num,error,127)
            new 
matchString[64]
           
            if(
regexref >= REGEX_OK)
            {
                for(new 
i=0i<numi++)
                {
                    
regex_substr(regexrefimatchStringcharsmax(matchString))

                    new const 
replaceC[][] = { "^"" , "city", ",", ":"}

                    for(new i = 0; i < sizeof(replaceC); i++)
                        replace_all(matchString, charsmax(matchString), replaceC[i], "")
                   
                }
                if(equal(matchString,""))    matchString = errorReturn;
               
                regex_free(regexref)
            }
           
           
           
            for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
                if(containi(socketData, GeoIP_country_name[i]) != -1 ){
                    ChatColor(0, "
!g[NG] !yPlayer !g%s!t[!g%s!t] !yhas connected from !t[!g%s!t]!y->!t[!g%s!t]",szName, szSteam,  GeoIP_country_name[i], matchString )
                   
                    break;
                }
            }
           
           

            socket_close(gSocket)
            remove_task(id)
        }
    }
    else{
        socket_close(gSocket)
        remove_task(id)
    }
    count++
    gwait = false
   
    return PLUGIN_HANDLED
}  
stock ChatColor(const id, const input[], any:...)
{
    new count2 = 1, players[32]
    static msg[191]
    vformat(msg, 190, input, 3)
   
    replace_all(msg, 190, "
!g", "^4") // Green Color
    replace_all(msg, 190, "
!y", "^1") // Default Color
    replace_all(msg, 190, "
!t", "^3") // Team Color
    replace_all(msg, 190, "
!t2", "^0") // Team2 Color
   
    if (id) players[0] = id; else get_players(players, count2, "
ch")
    {
    for (new i = 0; i < count2; i++)
    {
        if (is_user_connected(players[i]))
        {
            message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("
SayText"), _, players[i])
            write_byte(players[i]);
            write_string(msg);
            message_end();
        }
    }
}

__________________
Quote:
Originally Posted by wickedd View Post
Make me or STFU boy

Last edited by pupdebox; 09-03-2016 at 21:49.
pupdebox is offline
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 09-04-2016 , 13:40   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #6

Quote:
Originally Posted by pupdebox View Post
Instead of getting data at the same time, place players in queue? This will reduce the lag. Great plugin btw.

Edit: Something like that. I also think that you should check is_user_connected for "public answerSocket(id)" Players can drop anytime
PHP Code:
/* Plugin generated by AMXX-EDITOR v1.9*/

#include <amxmodx>
#include <amxmisc>
#include <sockets>
#include <regex>

#define REPEAT_TIME 15
#define hostConnect "freegeoip.net"
   
new const errorReturn[]            =    "error";
new 
gSocketcount 0
new bool:gwait

new const GeoIP_country_name[][] =
"N/A",
    
"Asia/Pacific Region",                         "Europe",
    
"Andorra",                                     "United Arab Emirates",
    
"Afghanistan",                                 "Antigua and Barbuda",
    
"Anguilla",
    
"Albania",                                     "Armenia",
    
"Curacao",
    
"Angola",
    
"Antarctica",                                  "Argentina",
    
"American Samoa",                              "Austria",
    
"Australia",                                   "Aruba",
    
"Azerbaijan",
    
"Bosnia and Herzegovina",                      "Barbados",
    
"Bangladesh",
    
"Belgium",                                     "Burkina Faso",
    
"Bulgaria",                                    "Bahrain",
    
"Burundi",                                     "Benin",
    
"Bermuda",
    
"Brunei Darussalam",                           "Bolivia",
    
"Brazil",
    
"Bahamas",                                     "Bhutan",
    
"Bouvet Island",                               "Botswana",
    
"Belarus",                                     "Belize",
    
"Canada",
    
"Cocos (Keeling) Islands",
    
"Congo, The Democratic Republic of the",
    
"Central African Republic",
    
"Congo",                                       "Switzerland",
    
"Cote D'Ivoire",                               "Cook Islands",
    
"Chile",                                       "Cameroon",
    
"China",
    
"Colombia",                                    "Costa Rica",
    
"Cuba",
    
"Cape Verde",                                  "Christmas Island",
    
"Cyprus",                                      "Czech Republic",
    
"Germany",                                     "Djibouti",
    
"Denmark",
    
"Dominica",                                    "Dominican Republic",
    
"Algeria",
    
"Ecuador",                                     "Estonia",
    
"Egypt",                                       "Western Sahara",
    
"Eritrea",                                     "Spain",
    
"Ethiopia",
    
"Finland",                                     "Fiji",
    
"Falkland Islands (Malvinas)",
    
"Micronesia, Federated States of",             "Faroe Islands",
    
"France",                                      "Sint Maarten (Dutch part)",
    
"Gabon",                                       "United Kingdom",
    
"Grenada",
    
"Georgia",                                     "French Guiana",
    
"Ghana",
    
"Gibraltar",                                   "Greenland",
    
"Gambia",                                      "Guinea",
    
"Guadeloupe",                                  "Equatorial Guinea",
    
"Greece",
    
"South Georgia and the South Sandwich Islands","Guatemala",
    
"Guam",
    
"Guinea-Bissau",                               "Guyana",
    
"Hong Kong",
    
"Heard Island and McDonald Islands",
    
"Honduras",                                    "Croatia",
    
"Haiti",
    
"Hungary",                                     "Indonesia",
    
"Ireland",
    
"Israel",                                      "India",
    
"British Indian Ocean Territory",              "Iraq",
    
"Iran, Islamic Republic of",                   "Iceland",
    
"Italy",
    
"Jamaica",                                     "Jordan",
    
"Japan",
    
"Kenya",                                       "Kyrgyzstan",
    
"Cambodia",                                    "Kiribati",
    
"Comoros",                                     "Saint Kitts and Nevis",
    
"Korea, Democratic People's Republic of",      "Korea, Republic of",
    
"Kuwait",
    
"Cayman Islands",
    
"Kazakhstan",
    
"Lao People's Democratic Republic",
    
"Lebanon",                                     "Saint Lucia",
    
"Liechtenstein",                               "Sri Lanka",
    
"Liberia",
    
"Lesotho",                                     "Lithuania",
    
"Luxembourg",
    
"Latvia",                                      "Libya",
    
"Morocco",                                     "Monaco",
    
"Moldova, Republic of",                        "Madagascar",
    
"Marshall Islands",                            "Macedonia",
    
"Mali",
    
"Myanmar",
    
"Mongolia",                                    "Macau",
    
"Northern Mariana Islands",                    "Martinique",
    
"Mauritania",                                  "Montserrat",
    
"Malta",
    
"Mauritius",                                   "Maldives",
    
"Malawi",
    
"Mexico",                                      "Malaysia",
    
"Mozambique",                                  "Namibia",
    
"New Caledonia",                               "Niger",
    
"Norfolk Island",
    
"Nigeria",                                     "Nicaragua",
    
"Netherlands",
    
"Norway",                                      "Nepal",
    
"Nauru",                                       "Niue",
    
"New Zealand",                                 "Oman",
    
"Panama",
    
"Peru",                                        "French Polynesia",
    
"Papua New Guinea",
    
"Philippines",                                 "Pakistan",
    
"Poland",                                      "Saint Pierre and Miquelon",
    
"Pitcairn Islands",                            "Puerto Rico",
    
"Palestinian Territory",                       "Portugal",
    
"Palau",
    
"Paraguay",
    
"Qatar",                                       "Reunion",
    
"Romania",                                     "Russian Federation",
    
"Rwanda",                                      "Saudi Arabia",
    
"Solomon Islands",                             "Seychelles",
    
"Sudan",
    
"Sweden",
    
"Singapore",                                   "Saint Helena",
    
"Slovenia",                                    "Svalbard and Jan Mayen",
    
"Slovakia",                                    "Sierra Leone",
    
"San Marino",
    
"Senegal",                                     "Somalia",
    
"Suriname",
    
"Sao Tome and Principe",
    
"El Salvador",                                 "Syrian Arab Republic",
    
"Swaziland",                                   "Turks and Caicos Islands",
    
"Chad",                                        "French Southern Territories",
    
"Togo",
    
"Thailand",
    
"Tajikistan",
    
"Tokelau",                                     "Turkmenistan",
    
"Tunisia",                                     "Tonga",
    
"Timor-Leste",                                 "Turkey",
    
"Trinidad and Tobago",                         "Tuvalu",
    
"Taiwan",
    
"Tanzania, United Republic of",
    
"Ukraine",                                     "Uganda",
    
"United States Minor Outlying Islands",        "United States",
    
"Uruguay",                                     "Uzbekistan",
    
"Holy See (Vatican City State)",
    
"Saint Vincent and the Grenadines",
    
"Venezuela",
    
"Virgin Islands, British",
    
"Virgin Islands, U.S.",                        "Vietnam",
    
"Vanuatu",                                     "Wallis and Futuna",
    
"Samoa",                                       "Yemen",
    
"Mayotte",
    
"Serbia",                                      "South Africa",
    
"Zambia",
    
"Montenegro",                                  "Zimbabwe",
    
"Anonymous Proxy",                             "Satellite Provider",
    
"Other",                                       "Aland Islands",
    
"Guernsey",
    
"Isle of Man",                                 "Jersey",
    
"Saint Barthelemy",
    
"Saint Martin",
    
"Bonaire, Saint Eustatius and Saba",
    
"South Sudan",                                 "Other" 
};  

public 
plugin_init(){
    
register_plugin(
        .
plugin_name "Connect Plugin based on Web API",
        .
version "1.0",
        .
author "Spawner/Freezo")
   
}

public 
client_connect(id)
{
    if(!
is_user_bot(id) && !gwait){
    
loadSocket(id)
    
gwait true
    
}
    else if(!
is_user_bot(id) && gwait){
    
set_task(1.0"is_ok"id__"b")
    }
}

public 
is_ok(id)
{
    if(
is_user_connected(id) && !gwait){
    
remove_task(id)
    
gwait true
    loadSocket
(id)
    }
    else if(!
is_user_connected(id))
    
remove_task(id)
}

public 
loadSocket(id){
   
    new 
errorReturned;
   
    
gSocket socket_open(hostConnect80SOCKET_TCPerrorReturned)
   
   
    if(
errorReturned == 0){
       
        new 
sBuffer[128], IP[24
       
        
get_user_ip(
                            
id,
                            
IP,
                            
charsmax(IP),
                            .
without_port1
        
)
       
        
format(sBuffer127"GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n"IPhostConnect)
       
        if(
socket_send(gSocketsBuffer127)){
           
            
set_task(1.0"answerSocket"id__"a"REPEAT_TIME)
        }
       
    }
   
    return 
PLUGIN_HANDLED
}

public 
answerSocket(id){
   
    if(
count != REPEAT_TIME){
        if(
socket_change(gSocket)){
            new 
socketData[1024]
           
            
socket_recv(gSocketsocketDatacharsmax(socketData))
            new 
szName[32]
            
get_user_name(idszNamecharsmax(szName))
           
            new 
szSteam[32]
            
get_user_authid(idszSteamcharsmax(szSteam))
           
            new 
numerror[128]
            new 
pattern[] = "city.+(?=zip)"
            
new Regex:regexref regex_match(socketData,pattern,num,error,127)
            new 
matchString[64]
           
            if(
regexref >= REGEX_OK)
            {
                for(new 
i=0i<numi++)
                {
                    
regex_substr(regexrefimatchStringcharsmax(matchString))

                    new const 
replaceC[][] = { "^"" , "city", ",", ":"}

                    for(new i = 0; i < sizeof(replaceC); i++)
                        replace_all(matchString, charsmax(matchString), replaceC[i], "")
                   
                }
                if(equal(matchString,""))    matchString = errorReturn;
               
                regex_free(regexref)
            }
           
           
           
            for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
                if(containi(socketData, GeoIP_country_name[i]) != -1 ){
                    ChatColor(0, "
!g[NG] !yPlayer !g%s!t[!g%s!t] !yhas connected from !t[!g%s!t]!y->!t[!g%s!t]",szName, szSteam,  GeoIP_country_name[i], matchString )
                   
                    break;
                }
            }
           
           

            socket_close(gSocket)
            remove_task(id)
        }
    }
    else{
        socket_close(gSocket)
        remove_task(id)
    }
    count++
    gwait = false
   
    return PLUGIN_HANDLED
}  
stock ChatColor(const id, const input[], any:...)
{
    new count2 = 1, players[32]
    static msg[191]
    vformat(msg, 190, input, 3)
   
    replace_all(msg, 190, "
!g", "^4") // Green Color
    replace_all(msg, 190, "
!y", "^1") // Default Color
    replace_all(msg, 190, "
!t", "^3") // Team Color
    replace_all(msg, 190, "
!t2", "^0") // Team2 Color
   
    if (id) players[0] = id; else get_players(players, count2, "
ch")
    {
    for (new i = 0; i < count2; i++)
    {
        if (is_user_connected(players[i]))
        {
            message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("
SayText"), _, players[i])
            write_byte(players[i]);
            write_string(msg);
            message_end();
        }
    }
}

same thing lag...
i have also change the timeout to 0 or 1 but it's worst.
Freezo Begin is offline
SomewhereLost
AlliedModders Donor
Join Date: Mar 2014
Location: Tomorrowland
Old 09-04-2016 , 15:58   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #7

Seems nice tho. Fixed one would be great
__________________
SomewhereLost is offline
Send a message via Skype™ to SomewhereLost
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 09-05-2016 , 18:21   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #8

Quote:
Originally Posted by SomewhereLost View Post
Seems nice tho. Fixed one would be great
Added ISP. Now City return = 100% true + Country = 100% true ( i changed the host ).
Fixed using sockets_hz, thank's to neygomon and the author of sockets_hz.

Code:
/* Plugin generated by AMXX-EDITOR v1.9*/

#include <amxmodx>
#include <amxmisc>
#include <sockets_hz>
#include <regex>

#define REPEAT_TIME 15
#define hostConnect "ip-api.com"

new const errorReturn[]            =    "error";
new gSocket, count = 0

new const GeoIP_country_name[][] =
{ "N/A",
	"Asia/Pacific Region",                         "Europe",
	"Andorra",                                     "United Arab Emirates",
	"Afghanistan",                                 "Antigua and Barbuda",
	"Anguilla",
	"Albania",                                     "Armenia",
	"Curacao",
	"Angola",
	"Antarctica",                                  "Argentina",
	"American Samoa",                              "Austria",
	"Australia",                                   "Aruba",
	"Azerbaijan",
	"Bosnia and Herzegovina",                      "Barbados",
	"Bangladesh",
	"Belgium",                                     "Burkina Faso",
	"Bulgaria",                                    "Bahrain",
	"Burundi",                                     "Benin",
	"Bermuda",
	"Brunei Darussalam",                           "Bolivia",
	"Brazil",
	"Bahamas",                                     "Bhutan",
	"Bouvet Island",                               "Botswana",
	"Belarus",                                     "Belize",
	"Canada",
	"Cocos (Keeling) Islands",
	"Congo, The Democratic Republic of the",
	"Central African Republic",
	"Congo",                                       "Switzerland",
	"Cote D'Ivoire",                               "Cook Islands",
	"Chile",                                       "Cameroon",
	"China",
	"Colombia",                                    "Costa Rica",
	"Cuba",
	"Cape Verde",                                  "Christmas Island",
	"Cyprus",                                      "Czech Republic",
	"Germany",                                     "Djibouti",
	"Denmark",
	"Dominica",                                    "Dominican Republic",
	"Algeria",
	"Ecuador",                                     "Estonia",
	"Egypt",                                       "Western Sahara",
	"Eritrea",                                     "Spain",
	"Ethiopia",
	"Finland",                                     "Fiji",
	"Falkland Islands (Malvinas)",
	"Micronesia, Federated States of",             "Faroe Islands",
	"France",                                      "Sint Maarten (Dutch part)",
	"Gabon",                                       "United Kingdom",
	"Grenada",
	"Georgia",                                     "French Guiana",
	"Ghana",
	"Gibraltar",                                   "Greenland",
	"Gambia",                                      "Guinea",
	"Guadeloupe",                                  "Equatorial Guinea",
	"Greece",
	"South Georgia and the South Sandwich Islands","Guatemala",
	"Guam",
	"Guinea-Bissau",                               "Guyana",
	"Hong Kong",
	"Heard Island and McDonald Islands",
	"Honduras",                                    "Croatia",
	"Haiti",
	"Hungary",                                     "Indonesia",
	"Ireland",
	"Israel",                                      "India",
	"British Indian Ocean Territory",              "Iraq",
	"Iran, Islamic Republic of",                   "Iceland",
	"Italy",
	"Jamaica",                                     "Jordan",
	"Japan",
	"Kenya",                                       "Kyrgyzstan",
	"Cambodia",                                    "Kiribati",
	"Comoros",                                     "Saint Kitts and Nevis",
	"Korea, Democratic People's Republic of",      "Korea, Republic of",
	"Kuwait",
	"Cayman Islands",
	"Kazakhstan",
	"Lao People's Democratic Republic",
	"Lebanon",                                     "Saint Lucia",
	"Liechtenstein",                               "Sri Lanka",
	"Liberia",
	"Lesotho",                                     "Lithuania",
	"Luxembourg",
	"Latvia",                                      "Libya",
	"Morocco",                                     "Monaco",
	"Moldova, Republic of",                        "Madagascar",
	"Marshall Islands",                            "Macedonia",
	"Mali",
	"Myanmar",
	"Mongolia",                                    "Macau",
	"Northern Mariana Islands",                    "Martinique",
	"Mauritania",                                  "Montserrat",
	"Malta",
	"Mauritius",                                   "Maldives",
	"Malawi",
	"Mexico",                                      "Malaysia",
	"Mozambique",                                  "Namibia",
	"New Caledonia",                               "Niger",
	"Norfolk Island",
	"Nigeria",                                     "Nicaragua",
	"Netherlands",
	"Norway",                                      "Nepal",
	"Nauru",                                       "Niue",
	"New Zealand",                                 "Oman",
	"Panama",
	"Peru",                                        "French Polynesia",
	"Papua New Guinea",
	"Philippines",                                 "Pakistan",
	"Poland",                                      "Saint Pierre and Miquelon",
	"Pitcairn Islands",                            "Puerto Rico",
	"Palestinian Territory",                       "Portugal",
	"Palau",
	"Paraguay",
	"Qatar",                                       "Reunion",
	"Romania",                                     "Russian Federation",
	"Rwanda",                                      "Saudi Arabia",
	"Solomon Islands",                             "Seychelles",
	"Sudan",
	"Sweden",
	"Singapore",                                   "Saint Helena",
	"Slovenia",                                    "Svalbard and Jan Mayen",
	"Slovakia",                                    "Sierra Leone",
	"San Marino",
	"Senegal",                                     "Somalia",
	"Suriname",
	"Sao Tome and Principe",
	"El Salvador",                                 "Syrian Arab Republic",
	"Swaziland",                                   "Turks and Caicos Islands",
	"Chad",                                        "French Southern Territories",
	"Togo",
	"Thailand",
	"Tajikistan",
	"Tokelau",                                     "Turkmenistan",
	"Tunisia",                                     "Tonga",
	"Timor-Leste",                                 "Turkey",
	"Trinidad and Tobago",                         "Tuvalu",
	"Taiwan",
	"Tanzania, United Republic of",
	"Ukraine",                                     "Uganda",
	"United States Minor Outlying Islands",        "United States",
	"Uruguay",                                     "Uzbekistan",
	"Holy See (Vatican City State)",
	"Saint Vincent and the Grenadines",
	"Venezuela",
	"Virgin Islands, British",
	"Virgin Islands, U.S.",                        "Vietnam",
	"Vanuatu",                                     "Wallis and Futuna",
	"Samoa",                                       "Yemen",
	"Mayotte",
	"Serbia",                                      "South Africa",
	"Zambia",
	"Montenegro",                                  "Zimbabwe",
	"Anonymous Proxy",                             "Satellite Provider",
	"Other",                                       "Aland Islands",
	"Guernsey",
	"Isle of Man",                                 "Jersey",
	"Saint Barthelemy",
	"Saint Martin",
	"Bonaire, Saint Eustatius and Saba",
	"South Sudan",                                 "Other" 
};  

// new bool:check = false;

public plugin_init(){
	register_plugin(
	.plugin_name = "Connect Plugin based on Web API",
	.version = "1.0",
	.author = "Spawner/Freezo")
	
	// register_clcmd("enable", "enable")
	// register_clcmd("disable", "disable")
	
}

// public enable(id)	check = true
// public disable(id)	check = false

public client_putinserver(id)
{
		if(!is_user_bot(id) /*check*/ ){
			loadSocket(id)
		}
}

public loadSocket(id){
	
	new errorReturned;
	
	gSocket = socket_open(hostConnect, 80, SOCKET_TCP, errorReturned)
	
	if(errorReturned == 0){
		
		new sBuffer[128], IP[24] 
		
		get_user_ip(
			id,
			IP,
			charsmax(IP),
			.without_port= 1
		)
		
		format(sBuffer, 127, "GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n", IP, hostConnect)
		
		if(socket_send2(gSocket, sBuffer, 127)){
			
			socket_unblock(gSocket);

			set_task(1.0 , "answerSocket", id, _, _, "a", REPEAT_TIME)
		}
		
	}
	
	return PLUGIN_HANDLED
}

public answerSocket(id){
	
	if(count != REPEAT_TIME){
		if(socket_change(gSocket)){
			
			new socketData[1024]
			
			socket_recv(gSocket, socketData, charsmax(socketData))
			new szName[32]
			get_user_name(id, szName, charsmax(szName))
			
			new szSteam[32]
			get_user_authid(id, szSteam, charsmax(szSteam))
			
			new num, error[128]
			
			new  pattern[2][] = {"city.+(?=^"country^")", "^"isp^".+(?=^"lat)" }
			
			new Regex:regexref = regex_match(socketData,pattern[0],num,error,127)
			new Regex:regexref2 = regex_match(socketData,pattern[1],num,error,127)
			
			new matchStringCity[64]
			new matchStringISP[64]
			
			if(regexref >= REGEX_OK || regexref2 >= REGEX_OK)
			{
				for(new i=0; i<num; i++)
				{
					regex_substr(regexref, i, matchStringCity, charsmax(matchStringCity))
					regex_substr(regexref2, i, matchStringISP, charsmax(matchStringISP))
					
					new const replaceC[][] = { "^"" , "city", ",", ":", "isp" }
					
					for(new i = 0; i < sizeof(replaceC); i++){
						replace_all(matchStringCity, charsmax(matchStringCity), replaceC[i], "")
						replace_all(matchStringISP, charsmax(matchStringISP), replaceC[i], "")	
					}
				}
				if(equal(matchStringCity,""))    matchStringCity = errorReturn;
				if(equal(matchStringISP,""))    matchStringISP = errorReturn;

				if(containi(matchStringISP,"Office") != -1)    matchStringISP = "Maroc Telecom";
				
				regex_free(regexref)
				regex_free(regexref2)
			}
			
			
			for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
				if(containi(socketData, GeoIP_country_name[i]) != -1 ){
					ChatColor(0, "!g[NG] !y%s :!g%s !t|| !yCountry :!g%s !t|| !yCity :!g%s !t|| !yISP :!g[%s]",is_user_admin(get_user_index(szName)) ? "Admin" : "User" ,szName,  GeoIP_country_name[i], matchStringCity,matchStringISP )
					
					break;
				}
			}
			
			
			
			socket_close(gSocket)
			remove_task(id)
			
		}
	}
	else{
		socket_close(gSocket)
		remove_task(id)
	}
	
	
	count++
	
	return PLUGIN_HANDLED
}  
stock ChatColor(const id, const input[], any:...)
{
	new count2 = 1, players[32]
	static msg[300]
	vformat(msg, charsmax(msg), input, 3)
	
	replace_all(msg, 190, "!g", "^4") // Green Color
	replace_all(msg, 190, "!y", "^1") // Default Color
	replace_all(msg, 190, "!t", "^3") // Team Color
	replace_all(msg, 190, "!t2", "^0") // Team2 Color
	
	if (id) players[0] = id; else get_players(players, count2, "ch")
{
	for (new i = 0; i < count2; i++)
	{
		if (is_user_connected(players[i]))
		{
			message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
			write_byte(players[i]);
			write_string(msg);
			message_end();
		}
	}
}
}
EDIT: Lag is fixed a little bit, the problem is if 2 players try to connect at the same time, i will try to fix it, then you can try it with no worries.

Last edited by Freezo Begin; 09-05-2016 at 18:38.
Freezo Begin is offline
SomewhereLost
AlliedModders Donor
Join Date: Mar 2014
Location: Tomorrowland
Old 09-05-2016 , 20:02   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #9

Nice. I'll definitely try out on one of my servers.
__________________
SomewhereLost is offline
Send a message via Skype™ to SomewhereLost
pupdebox
Senior Member
Join Date: Feb 2016
Location: Wakanda
Old 09-06-2016 , 05:56   Re: [HELP] Lag caused by sockets(Get Country &City)
Reply With Quote #10

Great job man!
__________________
Quote:
Originally Posted by wickedd View Post
Make me or STFU boy
pupdebox is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:05.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode