K-netlib issue

Advanced game related topics
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

K-netlib issue

Post by PureLeo »

I wanna make a little online game, for study purposes and i got 2 k-netlib versions..
I know k-netlib got a bit old.. but i still couldnt find another lib like this :S

http://www.4shared.com/file/zq38IfMu/K- ... Setup.html (2005 i think)

and

http://www.4shared.com/file/hu13d10m/KNL_403.html (2009)

but i cant menage to get it to work...

on the 306 example i get an error with the iclude file:
"Bad parameter type, number expected instead of string" at line 428, k-netlib.pb

Code: Select all

Procedure.l KNL_Initialize(GameID.s, Service.l, Session.l)
   ProcedureReturn CallFunction(KNL, "KNL_Initialize", GameID.s, Service.l, Session.l)
EndProcedure
And in the latest version i get another weird error about an argument not being a linked list (although im almost sure it is)

It was made for an older PB version, ofc, and ive tried to fix this but cant make it work..
I also got from the forums a possible translated include file for pb4 but still... doesnt work

Any ideas?

Anyone knows a better and newer free lib?

If anyone downloaded from my links and have been able to make any of those to work, please help me see whats wrong ;)
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: K-netlib issue

Post by Perkin »

There were some changes in PB, check the history.

change to

Code: Select all

Procedure.l KNL_Initialize(GameID.s, Service.l, Session.l)
   ProcedureReturn CallFunction(KNL, "KNL_Initialize", @GameID.s, Service.l, Session.l)
EndProcedure
notice the added '@'
%101010 = $2A = 42
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: K-netlib issue

Post by netmaestro »

Knetlib was written for PB 3.94 originally and that version was pretty stable. Your best bet would be to use that version, compiled with 3.94 to a shared .dll and see how it goes. It should be fine. The alternative would be to convert the whole code to work with the current version and that could be a pretty large task given the changes to AllocateMemory and CallFunctionFast/CallFunction just to name a couple.
BERESHEIT
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: K-netlib issue

Post by PureLeo »

wow, thank you very much! i could never figure it out (the reference thing '@')
:)

i then could do all the fixes myself...

changed every string argument to references...
for instance:

Code: Select all

...
ProcedureReturn CallFunction(KNL, "KNL_Connect", @PlayerName.s, @PlayerData.s, @HostAddress.s, PortNR.l, @Password.s, Method.l)
EndProcedure
and some procedures like

Code: Select all

Procedure.l KNL_GroupGetName(GroupID.l)
...
   ProcedureReturn KNL_Tmp.s
...
to

Code: Select all

ProcedureDLL.s KNL_GroupGetName(GroupID.l)
...
   ProcedureReturn KNL_Tmp.s
...
and finally managed to compile the chat exemple :)
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: K-netlib issue

Post by PureLeo »

"[ERROR]The specified 'MemoryID' is not valid.

at this line:

Code: Select all

Procedure.s KNL_FieldName(FldIdx.l)
   
   Define.l *KNL_Str
   Define.s KNL_Tmp
   
   *KNL_Str = CallFunction(KNL, "KNL_FieldName", FldIdx.l)
   KNL_Tmp.s = PeekS(*KNL_Str)

  *** FreeMemory(*KNL_Str) ***

   ProcedureReturn KNL_Tmp.s
EndProcedure
so i just cut off this line, since i don't think we need to "FreeMemory" since it has no "AllocateMemory" (am i wrong?)

and now i dont get any debug errors :D

BUT, the chat still crashes if i try to close its window, then i think its a bug in the chat code, not anything about k-netlib... thats good
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: K-netlib issue

Post by PureLeo »

just to let you know the chat.pb code:

Code: Select all

IncludeFile "K-NetLib.pb"

#Window = 0

#Radio_Client = 0
#Radio_Server = 1
#Frame_Option = 2
#Frame_Info   = 3
#String_Name  = 4
#Text_Name    = 5
#String_Port  = 6
#Text_Port    = 7
#String_IP    = 8
#Text_IP      = 9
#Editor_History  = 10
#String_Msg      = 11
#Button_Start    = 12
#Button_Shutdown = 13
#Button_Send     = 14

#Server = 0
#Client = 1

#GUID = "{C4765326-F34E-4240-A862-F3175F182F45}"

Global ConnectionType.l
Global Name.s, PortNR.l, IP.s
Global CourierNew.l

Procedure History(msg.s)
   Define.s Tmp
   Tmp.s  = GetGadgetText(#Editor_History)
   Tmp.s = "------------------------------------------------------" + Chr(13) + Tmp.s
   Tmp.s = msg.s + Chr(13) + Tmp.s
   SetGadgetText(#Editor_History, Tmp.s)
EndProcedure

Procedure.s GetDescription(MsgID.l)
   Select MsgID.l
     Case #KNL_MSG_IndicateConnect:  Description.s = "Indicate Connect"
     Case #KNL_MSG_ConnectAborted: Description.s = "Connect Aborted"
     Case #KNL_MSG_ConnectComplete: Description.s = "Connect Complete"
     Case #KNL_MSG_CreatePlayer: Description.s = "Create Player"
     Case #KNL_MSG_DestroyPlayer: Description.s = "Destroy Player"
     Case #KNL_MSG_IndicateSend: Description.s = "Indicate Send"
     Case #KNL_MSG_Receive: Description.s = "Packet Received"
     Case #KNL_MSG_SendComplete: Description.s = "Send Complete"
     Case #KNL_MSG_ServerInfo: Description.s = "Server Info Changed"
     Case #KNL_MSG_PlayerInfo: Description.s = "Player Infor Changed"
     Case #KNL_MSG_CreateGroup: Description.s = "Group Created"
     Case #KNL_MSG_EnterGroup: Description.s = "Player Added to Group"
     Case #KNL_MSG_LeaveGroup: Description.s = "Player Removed from Group"
     Case #KNL_MSG_DestroyGroup: Description.s = "Group Destroyed"
     Case #KNL_MSG_GroupInfo: Description.s = "Group Info Changed"
     Case #KNL_MSG_HostMigrate: Description.s = "Host is Migrating"
     Case #KNL_MSG_TerminateSession: Description.s = "Session is Terminating"
     Case #KNL_MSG_Handshake: Description.s = "Handshake in Progress"
     Case #KNL_MSG_ConnectionInfo: Description.s = "Connection Info Request"
     Case #KNL_MSG_GameInfo: Description.s = "Game Info Request"
     Case #KNL_MSG_AcceptFile: Description.s = "Accept File?"
     Case #KNL_MSG_FileAccepted: Description.s = "File Accepted"
     Case #KNL_MSG_FileRefused: Description.s = "File Refused"
     Case #KNL_MSG_FileTransferComplete: Description.s = "File Transfer Complete"
     Case #KNL_MSG_TimerIgnited: Description.s = "Game Timer Ignited!"
     Case #KNL_MSG_TimerStopped: Description.s = "Game Timer Stopped!"
     Case #KNL_MSG_HostID: Description.s = "Host ID"
     Case #KNL_MSG_CAPS: Description.s = "Caps Structure Requested"
     Case #KNL_MSG_SPCAPS: Description.s = "SP Caps Structure Requested"
     Case #KNL_MSG_HostFound: Description.s = "A Host has been found"
     Case #KNL_MSG_Download: Description.s = "Download Completed!"
   EndSelect
   ProcedureReturn Description.s
EndProcedure

CourierNew = LoadFont(#PB_Any, "Courier New", 8)

If OpenWindow(#Window, 216, 0, 428, 330, "KNL PB Chat", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar)
   
   If CreateGadgetList(WindowID(#Window))
      Frame3DGadget(#Frame_Option, 9, 24, 75, 60, "")
      OptionGadget(#Radio_Client, 18, 57, 57, 21, "Client")
      OptionGadget(#Radio_Server, 18, 36, 57, 21, "Server")
      SetGadgetState(#Radio_Server,#True)
      Frame3DGadget(#Frame_Info, 90, 3, 243, 99, "")
      TextGadget(#Text_Name, 102, 24, 66, 15, "Host Name:", #PB_Text_Right)
      StringGadget(#String_Name, 174, 18, 93, 21, "Host")
      TextGadget(#Text_Port, 102, 51, 66, 15, "Port Number:", #PB_Text_Right)
      StringGadget(#String_Port, 174, 45, 57, 21, "1001")
      TextGadget(#Text_IP, 102, 78, 66, 15, "IP Address:", #PB_Text_Right)
      StringGadget(#String_IP, 174, 72, 147, 21, "Not Needed!")
      DisableGadget(#String_IP, 1)
      ButtonGadget(#Button_Start, 342, 21, 72, 33, "Start")
      ButtonGadget(#Button_Shutdown, 342, 60, 72, 33, "Shutdown")
      DisableGadget(#Button_Shutdown, 1)
      EditorGadget(#Editor_History, 12, 117, 405, 171)
      SetGadgetText(#Editor_History, "History. New items will be added at the top...")
      SetGadgetFont(#Editor_History, FontID(CourierNew))
      StringGadget(#String_Msg, 12, 297, 342, 24, "")
      ButtonGadget(#Button_Send, 360, 294, 57, 30, "Send")
   EndIf
EndIf

KNL_ErrorOption(1)
Repeat
   E = WindowEvent()
   Delay(5)
   If E = #PB_Event_Gadget
      Select EventGadget()
        Case #Radio_Server
          SetGadgetText(#Text_Name, "Server Name:")
          SetGadgetText(#String_Name, "Host")
          SetGadgetText(#String_IP, "Not Needed!")
          DisableGadget(#String_IP, 1)
          ConnectionType.l = #Server
        Case #Radio_Client
          SetGadgetText(#Text_Name, "Client Name:")
          SetGadgetText(#String_Name, "Client")
          DisableGadget(#String_IP, 0)
          SetGadgetText(#String_IP, "localhost")
          ConnectionType.l = #Client
        Case #Button_Start
          DisableGadget(#Button_Start, 1)
          DisableGadget(#Button_Shutdown, 0)
          DisableGadget(#Radio_Client,1)
          DisableGadget(#Radio_Server,1)
          PortNR.l = Val(GetGadgetText(#String_Port))
          Name.s = GetGadgetText(#String_Name)
          IP.s = GetGadgetText(#String_IP)

          Select ConnectionType.l
            Case #Server
              History("Initializing. Please wait...")
              If KNL_Initialize(#GUID, #KNL_Service_TCPIP, #KNL_INI_Peer2Peer + #KNL_INI_Server)
                 History("KNL Initialized for hosting a session...")
                 If KNL_CreateHost(PortNR.l, Name.s,"","",0)
                    History("Host Created OK!")
                    History("Waiting for clients to connect.")
                 EndIf
              EndIf
            Case #Client
              History("Initializing. Please wait...")
              If KNL_Initialize(#GUID, #KNL_Service_TCPIP, #KNL_INI_Peer2Peer + #KNL_INI_Client)
                 History("KNL Initialized as client...")
                 If KNL_Connect(Name.s, "",IP.s,PortNR.l,"",1)
                 History("Connection request sent. Waiting host response...")
               EndIf
              EndIf
          EndSelect
        Case #Button_Shutdown
          DisableGadget(#Button_Start, 0)
          DisableGadget(#Button_Shutdown, 1)
          History("Shutting down...")
          KNL_Shutdown()
          DisableGadget(#Radio_Client,0)
          DisableGadget(#Radio_Server,0)
        Case #Button_Send
          ; Get the content of the message gadget and
          ; send it to all peers in the session...
          KNL_SendString(GetGadgetText(#String_Msg))
          If KNL_UpdateNetwork(0,0,100, #KNL_Send_NoIndicate + #KNL_Send_NoComplete) = 0
             History("Could not send the message!")
             Else
             SetGadgetText(#String_Msg,"") ; Clear the message gadget...
          EndIf
      EndSelect
      Else
      If E = #PB_Event_CloseWindow
         Break
      EndIf
   EndIf 
   While KNL_CheckErrors()
     History(KNL_GetErrorText())
     KNL_ClearError()
   Wend
   While KNL_CheckMessages()
     ID = KNL_GetMessageID() ; Get the message ID
     Fields = KNL_MessageFields() ; Get the number of fields the message contains.
     Msg.s = "Message Received - ID: " + Str(ID) + Chr(13)
     Msg.s = Msg.s + "Description: " + GetDescription(ID) + Chr(13)
     Msg.s = Msg.s + "Message contains " + Str(Fields) + " field(s): "
     For I = 0 To Fields - 1
         ; KNL_GetString will automatically convert
         ; any field content to a string and return
         ; it to us:
         Msg.s = Msg.s + Chr(13) + KNL_FieldName(I) + " = " + KNL_GetString(I) 
     Next
     History(Msg.s)
     KNL_ClearMessage()
   Wend
ForEver

KNL_Shutdown()

CloseLibrary(KNL)
Post Reply