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)