Raised This Month: $32 Target: $400
 8% 

Listenning socket


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hackziner
Senior Member
Join Date: Sep 2006
Location: France
Old 10-24-2007 , 14:07   Listenning socket
Reply With Quote #1

You need socket_hz module first.
http://forums.alliedmods.net/showthread.php?t=60026

Clean code: Alka


Sample with UDP

PHP Code:
#include <amxmodx>
#include <sockets_hz>

#define PLUGIN "Multi Server Chat System"
#define VERSION "0.1"
#define AUTHOR "hackziner"

#define DEFAULT_SOCKET_PORT 18107

new ListeningSocket;
new 
SendingSocket;

new 
HostName[32];
new 
PartnerServerIp[32];
new 
PartnerServerCvar;

public 
plugin_init() {
    
    
register_plugin(PLUGINVERSIONAUTHOR);
    
    new 
Error;
    
ListeningSocket socket_listen("127.0.0.1"DEFAULT_SOCKET_PORTSOCKET_UDPError);
    
socket_unblock(ListeningSocket);
    
    
register_clcmd("say""HandleSay");
    
PartnerServerCvar register_cvar("partner_server""82.232.102.55"FCVAR_SERVER); //Partner Server.
    
    
get_cvar_string("hostname"HostNamesizeof HostName 1);
    
get_pcvar_string(PartnerServerCvarPartnerServerIpsizeof PartnerServerIp 1);
    
    
set_task(1.0"RecvMessage"0""0"b");
}

public 
plugin_end() 
    
socket_close(ListeningSocket);

public 
HandleSay(id)
{
    static 
Args[256], Name[32], Buffer[512], Error;
    
read_args(Argssizeof Args 1);
    
    
get_user_name(idNamesizeof Name 1);
    
    
format(Buffersizeof Buffer 1"[%s]%s : %s"HostNameNameArgs);
    
    
SendingSocket socket_open(PartnerServerIpDEFAULT_SOCKET_PORTSOCKET_UDPError);
    
    
socket_send(SendingSocketBuffersizeof Buffer 1);
    
socket_close(SendingSocket);
}

public 
RecvMessage()
{
    static 
Message[512];
    if(
socket_change(ListeningSocket,1))
    {
        
socket_recv(ListeningSocketMessagesizeof Message 1);
        
client_print(0print_chat"%s"Message);
    }

An other example with TCP listenning socket

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sockets_hz>

#define HTTPPORT 8081
#define AcceptInterval 0.1
#define FakeThreadInterval 0.1
#define PacketSize 5120



new listening_socket
new dlthread[50][4]

public 
plugin_init() {
    new 
error //you're the error
    
register_plugin("Http server""0.1""hackziner")
    
server_print("LISTENNING ANY ON PORT %d",HTTPPORT)
    
register_cvar("http_listenning_socket","0",FCVAR_SERVER)
    
listening_socket=get_cvar_num("http_listenning_socket")
    if(
listening_socket==0)listening_socket socket_listen("127.0.0.1",HTTPPORT,SOCKET_TCP,error)
    
socket_unblock(listening_socket)
    
set_cvar_num("http_listenning_socket",listening_socket)
    
    
set_task(AcceptInterval,"auto_accept_reply",0,"",0,"b")
    
set_task(FakeThreadInterval,"fake_mt_send",0,"",0,"b")
}


public 
fake_mt_send() {
new 
i
new j
new fsize
static tampax[PacketSize+16// Yes I know, it's a very bad joke
for(i=0;i<50;i++)
    {
    if (
dlthread[i][0]>0)
        {
        
fsize=dlthread[i][3]                
                if ((
fsize-dlthread[i][2])>PacketSize)
                {
                    for(
j=0;j<PacketSize;j++)
                        
tampax[j]=fgetc(dlthread[i][1])
                    
socket_send(dlthread[i][0],tampax,PacketSize)
                    
dlthread[i][2]=dlthread[i][2]+PacketSize
                
}
                else
                {
                    for(
j=0;j<(fsize-dlthread[i][2]) && !feof(dlthread[i][1]);j++)
                        
tampax[j]=fgetc(dlthread[i][1])
                        
socket_send(dlthread[i][0],tampax,(fsize-dlthread[i][2]))
                        
socket_close(dlthread[i][0])
                    
dlthread[i][0]=0
                
}
        }

            
    
        
    }

}

public 
auto_accept_reply() {
new 
this_chaussette;

if((
this_chaussette=socket_accept(listening_socket))<0)
    {
    }
else
    {
        
socket_unblock(this_chaussette)
        
set_task(1.0"http_request_run"this_chaussette
    }

}
public 
http_request_run(socketball)
{
        static 
cmd[20480]
        static 
data[20480]
        new 
request[1024]
        new 
requestedfile[128]
        new 
requestedtype[32]
        
        
socket_recv(socketball,request,1023)
        
parse_http_request(request,requestedfile,requestedtype)
        
        
        if(
strcmp("/",requestedfile,1)==|| strcmp("/index.html",requestedfile,1)==0)
            {
                
format(requestedtype,31,"text/html")
                
build_page(data)
                
build_response(cmd,data,requestedtype)
                
socket_send(socketball,cmd,20479)
            }
        else
            {
                
                if(
containi(requestedfile,".")<0)
                    {
                    
format(requestedtype,31,"text/html")
                    if(
convert_dir(requestedfile))
                        {
                        
format(data,1024,"ACCESS DENIED<br>^r^nhackziner amxmodx webserver!")
                        
build_response(cmd,data,requestedtype)
                        
socket_send(socketball,cmd,strlen(cmd))
                        
socket_close(socketball)
                        }
                        else
                        {
                        
build_dir_view(requestedfile,data)
                        
build_response(cmd,data,requestedtype)
                        
socket_send(socketball,cmd,strlen(cmd))
                        
socket_close(socketball)
                        }
                    }
                else
                    {
                        if(
convert_dir(requestedfile))
                        {
                        
format(data,1024,"ACCESS DENIED<br>^r^nhackziner amxmodx webserver!")
                        
build_response(cmd,data,requestedtype)
                        
socket_send(socketball,cmd,strlen(cmd))
                        
socket_close(socketball)
                        }
                        else
                        {
                        if(
file_exists(requestedfile))
                            {
                                
file_header_data(requestedtype,requestedfile,socketball)
                            }
                        else
                            {
                                
format(data,1024,"404 File Not Found<br>^r^nhackziner amxmodx webserver!")
                                
build_response(cmd,data,requestedtype)
                                
socket_send(socketball,cmd,strlen(cmd))
                                
socket_close(socketball)
                            }
                        }
                    }
            }
        

        
}
public 
parse_http_request(request[1024],requestedfile[128],requestedtype[32])
{
    
copyc(requestedfile,128,request,'^r')
    
server_print("Client fullrequest: %s",requestedfile)
    if(
containi(requestedfile,"..")>-1)
        
format(requestedfile,31,"GET /")
            
    
strtok(requestedfile,requestedtype,31,requestedfile,127,' ')
    
copyc(requestedfile,128,requestedfile,' ')
    
server_print("Client request: %s",requestedfile)
    
format(requestedtype,31,"application/octet-stream")
    if(
containi(requestedfile,".txt")>-1)
        
format(requestedtype,31,"text/html")
    if(
containi(requestedfile,".htm")>-1)
        
format(requestedtype,31,"text/html")
    if(
containi(requestedfile,".css")>-1)
        
format(requestedtype,31,"text/css")
    if(
containi(requestedfile,".png")>-1)
        
format(requestedtype,31,"image/x-png")
    if(
containi(requestedfile,".gif")>-1)
        
format(requestedtype,31,"image/gif")
    if(
containi(requestedfile,".jp")>-1)
        
format(requestedtype,31,"image/jpeg")
}

public 
convert_dir(ddir[128])
{
    new 
basedir[128]
    
get_basedir(basedir,127)
    
    if(
containi(ddir,"cstrike/maps")>-1)    
        
format(ddir127"%s/../../..%s",basedir,ddir
    if(
containi(ddir,"cstrike/sound")>-1)    
        
format(ddir127"%s/../../..%s",basedir,ddir
    if(
containi(ddir,"cstrike/models")>-1)    
        
format(ddir127"%s/../../..%s",basedir,ddir
    if(
containi(ddir,"motd.txt")>-1)    
        
format(ddir127"%s/../../motd.txt",basedir
    if(
containi(ddir,"wsite/")>-1)    
        
format(ddir127"%s/data/%s",basedir,ddir
    
server_print("Converted dir is %s",ddir)
    if(
containi(ddir,"cstrike")==-&& containi(ddir,"wsite")==-1)    
        {
            
format(ddir127"unknow/unknow"
            return 
1
        
}
    return 
0
}
 
public 
build_dir_view(ddir[128],page[20480])
{
    new 
dirh
    
new filename[64]
    new 
tp
    
new big[256]
    new 
inc
    format
(page,20480,"<html><head><title>Hackziner Amxmodx Webserver</title></head>")
    
format(page,20480,"%s<body>^r^n",page)
    
format(page,20480,"%s<h1>Hackziner Amxmodx Webserver</h1><br>^r^n",page)
    
format(page,20480,"%s<h2>Dir view : %s</h2><br>^r^n",page,ddir)
    
format(page,20480,"%s<table border=1>^r^n",page)
    
format(page,20480,"%s<tr><td>File Name</td><td>Size</td></tr>^r^n",page)
    
inc=0
    
if(dirh=open_dir(ddir,filename,63))
        {
            
format(page,20479,"%s<tr><td><a href='%s'>%s</a></td></tr>^r^n",page,filename,filename)
        }
    while(
next_file dirhfilename63 ) && inc<50)
        {
        
inc=inc+1
        
if(containi(filename,".")<|| inc==1)
            
format(page,20479,"%s<tr><td><a href='%s/'>%s (Dir)</a></td></tr>^r^n",page,filename,filename)
        else
            {
            
            
format(big,255,"%s%s",ddir,filename)
            
tp=file_size(big)
            
format(page,20479,"%s<tr><td><a href='%s'>%s</a></td><td>%d Bytes</td></tr>^r^n",page,filename,filename,tp)
            }
        }
    
close_dir(dirh)
    
format(page,20479,"%s</table>^r^n",page)
    
format(page,20479,"%s</body></html>^r^n",page)
}
public 
build_page(page[20480])
{
    new 
info[128]
    new 
i,j
    format
(page,20479,"<html><head><title>Hackziner Amxmodx Webserver</title></head>")
    
format(page,20479,"%s<body>^r^n",page)
    
format(page,20479,"%s<h1>Hackziner Amxmodx Webserver</h1><br>^r^n",page)
    
get_cvar_string ("hostname",info,127 )
    
format(page,20479,"%s<h2>Server info : %s</h2><br>^r^n",page,info)
    
format(page,20479,"%s<h3>HTTP</h3><br>^r^n",page)
    
i=get_charge()
    
format(page,20479,"%sHTTP CHARGE : %d<br>^r^n",page,i)
    
    
format(page,20479,"%s<h3>Game: Players :</h3><br>^r^n",page,info)
    
format(page,20479,"%s<table border=1>^r^n",page)
    
format(page,20479,"%s<tr><td>Name</td><td>Team</td><td>Time</td><td>HP</td></tr>^r^n",page)
    new 
maxplayers
    
new pname[32]
    
maxplayers get_maxplayers()
    for(
<= maxplayers i++)
        if(
is_user_connected(i))
            {
            
format(page,20479,"%s<tr>^r^n",page)
            
get_user_name(i,pname,31)
            
format(page,20479,"%s<td>%s</td>^r^n",page,pname)
            
get_user_team(i,pname,31)
            
format(page,20479,"%s<td>%s</td>^r^n",page,pname)
            
j=get_user_time(i)
            
format(page,20479,"%s<td>%d</td>^r^n",page,j)
            
j=get_user_health(i)
            
format(page,20479,"%s<td>%d</td>^r^n",page,j)
            
format(page,20479,"%s</tr>^r^n",page)    
            }
    
format(page,20479,"%s</table>^r^n",page)
    
format(page,20479,"%s<h3>Web Dir :</h3><br>^r^n",page,info)
    
format(page,20479,"%s<a href='wsite/'>Site</li><br></a>^r^n",page,info)
    
format(page,20479,"%s<h3>Download Dir :</h3><br>^r^n",page,info)
    
format(page,20479,"%s<a href='cstrike/maps/'>maps</li><br></a>^r^n",page,info)
    
format(page,20479,"%s<a href='cstrike/sound/'>sound</li><br></a>^r^n",page,info)
    
format(page,20479,"%s<a href='cstrike/models/'>models</li><br></a>^r^n",page,info)
    
format(page,20479,"%s</body></html>^r^n",page)
}

public 
build_response(response[20480],data[20480],requestedtype[32])
{
    new 
fdate[64]
    
get_time("%a, %d %b %Y %X GMT",fdate,63)

    
format(response,20479,"HTTP/1.1 200 OK^r^n")
    
format(response,20479,"%sDate: %s^r^n",response,fdate)
    
format(response,20479,"%sContent-Length: %d^r^n",response,strlen(data))
    
format(response,20479,"%sKeep-Alive: timeout=15, max=99^r^n",response)
    
format(response,20479,"%sConnection: Keep-Alive^r^n",response)
    
format(response,20479,"%sContent-Type: %s^r^n",response,requestedtype)
    
format(response,20479,"%s^r^n",response)
    
format(response,20479,"%s%s^n",response,data)
}

public 
file_header_data(requestedtype[32],filel[128],CSocket)
{
    new 
fdate[64]
    new 
response[1024]
    new 
ii
    get_time
("%a, %d %b %Y %X GMT",fdate,63)

    
format(response,1024,"HTTP/1.1 200 OK^r^n")
    
format(response,1024,"%sDate: %s^r^n",response,fdate)
    
format(response,1024,"%sContent-Length: %d^r^n",response,file_size(filel))
    
format(response,1024,"%sKeep-Alive: timeout=15, max=99^r^n",response)
    
format(response,1024,"%sConnection: Keep-Alive^r^n",response)
    
format(response,1024,"%sContent-Type: %s^r^n",response,requestedtype)
    
format(response,1024,"%s^r^n",response)
    
socket_send(CSocket,response,strlen(response))
    
ii=0
    
while(dlthread[ii][0]>&& ii<50)ii=ii+1
    dlthread
[ii][0]=CSocket
    dlthread
[ii][1]=fopen(filel,"rb")
    
dlthread[ii][2]=0
    dlthread
[ii][3]=file_size(filel)
    
server_print("HTTP: Adding fake thread for %s",filel)
}


public 
get_charge()
{
new 
i,ii
i
=0
while (ii<50)
    {
    if(
dlthread[ii][0]>0)i=i+1
    ii
=ii+1

    
    
}
return 
i*2

important part are

PHP Code:
    if(listening_socket==0)listening_socket socket_listen("127.0.0.1",HTTPPORT,SOCKET_TCP,error)
    
socket_unblock(listening_socket
PHP Code:
public auto_accept_reply() {
new 
this_chaussette;

if((
this_chaussette=socket_accept(listening_socket))<0)
    {
    }
else
    {
        
socket_unblock(this_chaussette)
        
set_task(1.0"http_request_run"this_chaussette
    }


You need to unblock the listenning socket because you can do a socket_change to check if there is a pending connection and
When you accept a connection, you get a new socket connected to the client.
__________________
hackziner is offline
Send a message via ICQ to hackziner Send a message via AIM to hackziner Send a message via MSN to hackziner Send a message via Yahoo to hackziner Send a message via Skype™ to hackziner
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-24-2007 , 14:44   Re: Listenning socket
Reply With Quote #2

Huh, nice "Fast Download Server" through amxmodx...gj anyway! o_O
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
Ramono
Veteran Member
Join Date: Nov 2005
Location: Netherlands
Old 10-25-2007 , 07:12   Re: Listenning socket
Reply With Quote #3

cool!

Finish that fast download server and release !
One thing i would like to see in it is, bandwidth limiter in kb/s
__________________
Um, hi.
Ramono is offline
hackziner
Senior Member
Join Date: Sep 2006
Location: France
Old 10-25-2007 , 07:16   Re: Listenning socket
Reply With Quote #4

This plugin is just a sample. It's not supposed to be use in prod
__________________
hackziner is offline
Send a message via ICQ to hackziner Send a message via AIM to hackziner Send a message via MSN to hackziner Send a message via Yahoo to hackziner Send a message via Skype™ to hackziner
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 10-25-2007 , 11:24   Re: Listenning socket
Reply With Quote #5

I have something similar if anyone wants to see it.
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 10-25-2007 , 11:31   Re: Listenning socket
Reply With Quote #6

Quote:
Originally Posted by Ramono View Post
cool!

Finish that fast download server and release !
One thing i would like to see in it is, bandwidth limiter in kb/s

What if theres already an httpd listening on 80?
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-25-2007 , 12:37   Re: Listenning socket
Reply With Quote #7

Quote:
Originally Posted by Podunk View Post
I have something similar if anyone wants to see it.
Share the wealth? :> That will be cool, and is working ?
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
hackziner
Senior Member
Join Date: Sep 2006
Location: France
Old 10-25-2007 , 13:00   Re: Listenning socket
Reply With Quote #8

Quote:
#define HTTPPORT 8081
default port of the plugin is 8081

If the port is already in use ... It won't create the socket and nothing happens ... maybe you have to check the error when the socket is created ...
__________________
hackziner is offline
Send a message via ICQ to hackziner Send a message via AIM to hackziner Send a message via MSN to hackziner Send a message via Yahoo to hackziner Send a message via Skype™ to hackziner
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 10-26-2007 , 13:24   Re: Listenning socket
Reply With Quote #9

This is something Ive been working on for a while now, it still needs a lot of work though, I may make a module for it someday


It's still in critical development, I can't post it (your eyes would bleed)
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-26-2007 , 13:32   Re: Listenning socket
Reply With Quote #10

Quote:
Originally Posted by Podunk View Post
I can't post it (your eyes would bleed)
Lawl !
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
Reply


Thread Tools
Display Modes

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


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