oonetwork lib 1.00a

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

oonetwork lib 1.00a

Post by idle »

ooNetWork lib

Aims to provide an easy to use client server platform Supporting


Multiple clients
with multiple connections (attempt to reduce latency effects)
Authentication via Elliptic curve key exchange (thanks java bean for timely post)
AES encryption



usage commented in server and client example

Basically you provide a callback function in you app and process the requests as they arrive
given the client, the reason the data buffer and it's length

Things todo

fix up error resolution (other than #wsa would block #wsa_disconnect)
Load balancing
data Queues
Time out resolution

download http://www.idlearts.com/oonetwork1.zip

If you find any bugs or make any improvements to the lib please post them.
Last edited by idle on Sun May 02, 2010 9:44 pm, edited 4 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oonetwork lib 0.9a

Post by idle »

Basic chat server client example

Server

Code: Select all

;basic chat server 
;
;1) include ooNetWork

XIncludeFile "PureNetWork.pbi"

;2) Define and create a net work object
;   New_NetWork(*obj,MaxClients,MaxConnections,AmServer)  
;   Where Max Clients is the maximum number of clients accepted on the server 
;   Max Connections is the additional number of connections each client may have
;   #True for I am the server
;   note to enable multiple clients from a single IP (same machine)
;   set MaxConnections To 0 otherwise only one client from a single IP may connect 

Global net.NetWork_obj = New_NetWork(@net,10,1,#True) ; 

 

#nick = 10
#Messege = 20
#data = 30 
;3) Create a callback procedure with these parameters
Procedure Listen(client.i,reason.i,*mdata,len.i) 

Protected *tdata,ts.s,nlen.i,text.s,a,aret.i,tlen,name.s  
Static ct

;6) process a requests from an authenitcated client

Select Reason 

Case #nick 
    
    net\Set_UserID(client,PeekS(*mdata,len))

Case #data 

    text = Str(ct) +" Recived " + Str(reason) + " " + Str(len) 
    AddGadgetItem(0,0,Text)    
     
    aret = Net\BroadCast(client,*mdata,Len)
    ct+1    
Case #Messege,#NetWork_Authorized

    text = Str(ct) +" Recived " + Str(reason) + " " + Str(len) 
    AddGadgetItem(0,0,Text)
    
    *tdata = AllocateMemory(len+64)
    ;who said what 
    name = net\Get_UserId(client)
    PokeS(*tdata,name,64)
    ;copy the message 
    CopyMemory(*mdata,*tdata+64,len)
    
    ;7) send message to all clients
    aret = Net\BroadCast(client,*tdata,len+64)
    FreeMemory(*tdata)  
    ct+1
    
    
EndSelect 

If ct > 2000 
  ClearGadgetItems(0)
  ct=0
EndIf   

EndProcedure 
Procedure Status(*Status)
  Static lstatus.s 
  Protected status.s
  status = PeekS(*status) 
  If Status <> lStatus 
    AddGadgetItem(0,0,status)
    StatusBarText(1,0,Status)
    lStatus = Status
  EndIf 
EndProcedure   

;4) Set the Server CallBack Address  
net\Set_ServerCallBack(@listen())
net\Set_StatusCallBack(@Status())
;5) Start the server with the port and password 
Define port.s,pass.s 
port = InputRequester("server","Set Port","7500")
pass = InputRequester("server","Set PassWord","qwerty")

net\InitNetServer(Val(port),pass) 

Define EV

OpenWindow(0,0,0,250,250,"Server test",#PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_TitleBar)
ListViewGadget(0,3,3,246,226)
CreateStatusBar(1,WindowID(0))
AddStatusBarField(200)

Repeat   
  
  EV = WaitWindowEvent() 
  
Until EV = #WM_CLOSE 

Debug "ended"

;8) Close and destroy the server object 
If net 
  net\Destroy(@net)
EndIf 


client

Code: Select all

;basic chat client example

;1) include ooNetWork

XIncludeFile "PureNetwork.pbi"

;2) Define and create a net work object
;   New_NetWork(*obj,MaxClients,MaxConnections,AmServer)  
;   Where Max Clients is the maximum number of clients accepted on the server 
;   Max Connections is the additional number of connections each client may have
;   #True for I am the server
;   note to enable multiple clients from a single IP (same machine)
;   set MaxConnections To 0 other wise only one client from a single IP may connect 

;Global net.NetWork_obj = rNew_NetWork(@net,0,5,#False) 6 tcp connections 

Global net.NetWork_obj; = New_NetWork(@net,0,1,#False) ;single tcp connection 

;need to keep track of the connection number lcon
Global lcon,ct,nickname.s,gsent.i
#nick = 10
#Messege = 20
#data = 30 
;3) Create a callback procedure with these parameters
Procedure Listen(client.i,reason.i,*mdata,len.i) 
Protected *tdata,nlen.i,text.s,a.i,aret.i,tlen,name.s 
;6) process a messages from the server

lcon=client 

Select Reason

Case #Data 
  
  Text = PeekS(*mdata,len) ;"Data Recieved" 
  AddGadgetItem(0,ct,Text)
  ct+1  
 
Case #NetWork_Authorized 
   ;If you get here your authenticated 
   ;Enable the send button and return the nickname to the server
   DisableGadget(2,0)
   DisableGadget(3,0)
   net\send(lcon,#Nick,@nickname,Len(nickname))
   
Case #NetWork_BroadCast 
    ;ok we got a message display it 
    name = PeekS(*mdata,64) 
    If len < 256
      text = name + " said " + PeekS(*mdata+64,len-64)
    Else 
      text = "got data"
    EndIf 
    AddGadgetItem(0,ct,Text)
    ct+1
        
EndSelect 

If ct > 2000 
  ClearGadgetItems(0)
  ct=0
EndIf   

;Delay(80)


EndProcedure 

Procedure send(msg.s)
  ;7) send a message to the server
  If msg <> ""
    AddGadgetItem(0,ct,msg)
    ct+1
    net\send(lcon,#Messege,@msg,Len(msg)) 
    SetGadgetText(1,"")   
  EndIf 
EndProcedure
Procedure senddata()
 Protected *tdata,size,a 
 
 DisableGadget(3,1)
 size = (1024*1024)
 *tdata = AllocateMemory(size)
 For a = 0 To size-1 
   PokeB(*tdata+a,a%255)
 Next   
 net\send(lcon,#data,*tdata,size) 
 FreeMemory(*tdata) 
 DisableGadget(3,0)

EndProcedure

Procedure Status(*Status)
  Static lstatus.s 
  Protected status.s 
  Status = PeekS(*status)
  If IsStatusBar(0)
    If Status <> lStatus 
       StatusBarText(0,0,Status)
      lStatus = Status
    EndIf
  EndIf   
EndProcedure

Define ConnectToIP.s,port.s,pass.s
ConnectToIP = InputRequester("Client","Enter IP","127.0.0.1")
port = InputRequester("Client","Enter port","7500")
pass = InputRequester("Client","Enter password","qwerty")
nickname = InputRequester("NickName","Enter a nick name","")

;
Define EV,evt,txt.s

OpenWindow(0,0,0,250,293,"Client",#PB_Window_SystemMenu)
ListViewGadget(0,3,3,246,220)
StringGadget(1,3,225,195,20,"")
ButtonGadget(2,200,225,50,20,"send")
ButtonGadget(3,200,247,50,20,"data")
CreateStatusBar(0,WindowID(0))
AddStatusBarField(200)


DisableGadget(2,1)

net.NetWork_obj = New_NetWork(@net,0,1,#False) 
;4) Set the Clinet CallBack Address  
net\Set_ClientCallBack(@listen())
net\Set_StatusCallBack(@Status())

;5) When Ready to Connect Init Client with IP, port, Password 
net\Init_NetClient(ConnectToIP,Val(port),pass)


Repeat   
  
  EV = WaitWindowEvent() 
  If ev = #PB_Event_Gadget 
    evt = EventGadget()
    If evt = 2 
     txt = GetGadgetText(1)
     send(txt)
    ElseIf evt = 3 
      Senddata() 
    EndIf
  EndIf        
Until EV = #WM_CLOSE 

;8) Destroy the object 
If net 
  net\Destroy(@net)
EndIf 

Last edited by idle on Sun May 02, 2010 9:51 pm, edited 1 time in total.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: oonetwork lib 0.91a

Post by DoubleDutch »

Not looked at it properly yet, but could it be made to pass through proxy servers? If so, if the server was set to ports 80, 21 or 23 (etc) could the clients 'grab' the proxy information from Windows settings automatically?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Re: oonetwork lib 0.91a

Post by Thalius »

Proxy relies ona Standard Protocol.

This looks custom but seems nice and easy ^^
Thanks idle. ! :D
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oonetwork lib 0.91a

Post by idle »

I don't know whats involved with passing it though a proxy server, though I'm sure it'd be easy enough to do once I know whats involved.

Thanks, hopefully it should be easy and flexible.

I'm currently working on a threaded mode option which isn't as easy as it would appear since I don't want to suffer the cost of creating threads per request. A Threaded receive procedure per connection seems straight forward enough using semaphores to halt the loop but the send isn't quite so simple.
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oonetwork lib 1.00a

Post by idle »

updated to a threaded model
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: oonetwork lib 1.00a

Post by PureLeo »

I get an error:

" CopyStructure(*this\m_client[tclient]\m_Connection[a],*this\m_client[tclient]\m_Connection[a-1],mConnection)
"

It says CopyStructure is not a function/structure/map etc...
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oonetwork lib 1.00a

Post by idle »

That would be due to the PB version not sure if CopyStructure was added in 4.40 or 4.50 but I'm currently using 4.50 b4
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: oonetwork lib 1.00a

Post by PureLeo »

oh, i see... i'm using pb 4.41 yet
will have to wait a little bit :)
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oonetwork lib 1.00a

Post by idle »

you can replace the CopyStructure using CopyMemory and then do a manual copy of any strings within the structure.
Post Reply