K-NetLib a great network library

BlitzMax Forums/BlitzMax Programming/K-NetLib a great network library

Filax(Posted 2006) [#1]
Hi

I have fond by hazard a fantastic network library !
compatible blitz3d, purebasic, C++, delphi etc

http://www.krylarskreations.com/devtools.shtml

Features ? :

* Client/Server and Peer-to-Peer support

* Byte/Word/Long/Float/String/Array sends and receives
* Group controls

* Passive file transfers that allow your users to continue
chatting/playing while the file transfer in the background

* Built-in Bezier-Spline functionality to allow easy
calculation of smooth lines for multiplayer games

* Advanced error-handling routines to keep you informed
during your development cycle

* INI file commands to allow loading and saving of
initialization strings for your K-NetLib application

* Ban controls that allow one-time kicks, bans, and permanent bans stored in a file for your game

* ...and more!

It's very easy to code ! i have made thanslation for
blitzmax, here is the code, and i have made online
test with more than 8 player, it's really speed !

It's a great great job !

BLTIZMAX INCLUDE : Include "K-NetLib.bmx"

format_codebox('
' +--------------------------------------+
' | K-NetLib Constants For BlitzBasic |
' | (expects KNL v 3.0.2) |
' +--------------------------------------+
' | (C)2005 Krylar's Kreations |
' | A Division of 1338 Enterprises, Inc. |
' | All Rights Reserved |
' +--------------------------------------+
' | Producer: John "Krylar" Logsdon |
' | Coder...: Derlidio "PJoe" Siqueira |
' | Blitzmax version ...: Filax |
' +--------------------------------------+
' | Last Modified: 2005-06-08 (Y-M-D) |
' +--------------------------------------+

' ----------------
' Session options:
' ----------------
Const KNL_INI_ClientServer:Int = 1 ' User is coding a Client/Server interface.
Const KNL_INI_Peer2Peer:Int = 2 ' User is coding a Peer To Peer interface.
Const KNL_INI_Server:Int = 4 ' User wants To host a game.
Const KNL_INI_Client:Int = 8 ' User will connect To a remote host.
Const KNL_INI_MigrateHost:Int = 16 ' (Peer only) Migrate host If host falls down.
Const KNL_INI_Guaranteed:Int = 32 ' Use guaranteed packets.
Const KNL_INI_Sequential:Int = 64 ' Use sequential delivery.
Const KNL_INI_LinkTunning:Int = 128 ' Allows DX To tune the network flow.
Const KNL_INI_CanQueryUser:Int = 256 ' Allows DX To query user For needed info.
Const KNL_INI_SignPackets:Int = 512 ' Sign all outgoing packets with an 8 Byte key.
Const KNL_INI_UseSHA1:Int = 1024 ' Sign all outgoing packest using a SHA1 8 Byte key.
Const KNL_INI_LanSession:Int = 2048 ' Open a lagrer window For game sessions running on the same lan.

' ------------------
' Service providers:
' ------------------
Const KNL_Service_IPX:Int = 1
Const KNL_Service_MODEM:Int = 2
Const KNL_Service_SERIAL:Int = 4
Const KNL_Service_TCPIP:Int = 8
Const KNL_Service_BLUETOOTH:Int = 16

' ----------------------------
' Session Settings Components:
' ----------------------------
Const KNL_KEY_NAT_RESOLVER:String = "natresolver"
Const KNL_KEY_BAUD:String = "baud"
Const KNL_KEY_DEVICE:String = "device"
Const KNL_KEY_FLOWCONTROL:String = "flowcontrol"
Const KNL_KEY_HOSTNAME:String = "hostname"
Const KNL_KEY_PARITY:String = "parity"
Const KNL_KEY_PHONENUMBER:String = "phonenumber"
Const KNL_KEY_PORT:String = "port"
Const KNL_KEY_PROGRAM:String = "program"
Const KNL_KEY_PROVIDER:String = "provider"
Const KNL_KEY_STOPBITS:String = "stopbits"
Const KNL_KEY_NAMEINFO:String = "nameinfo"
Const KNL_KEY_PROCESSOR:String = "processor"
Const KNL_KEY_TRAVERSALMODE:String = "traversalmode"
Const KNL_KEY_APPLICATION_INSTANCE:String = "applicationinstance"
Const KNL_KEY_NAT_RESOLVER_USER_STRING:String = "natresolveruserstring"

' ----------------
' Connect options:
' ----------------
Const KNL_Process_Async:Int = 0
Const KNL_Process_Sync:Int = -1

' ------------
' Message IDs:
' ------------
Const KNL_MSG_IndicateConnect:Int = 1
Const KNL_MSG_ConnectAborted:Int = 2
Const KNL_MSG_ConnectComplete:Int = 3
Const KNL_MSG_CreatePlayer:Int = 4
Const KNL_MSG_DestroyPlayer:Int = 5
Const KNL_MSG_IndicateSend:Int = 6
Const KNL_MSG_Receive:Int = 7
Const KNL_MSG_AsyncOperationComplete:Int = 8
Const KNL_MSG_SendComplete:Int = 9
Const KNL_MSG_ServerInfo:Int = 10
Const KNL_MSG_PlayerInfo:Int = 11
Const KNL_MSG_CreateGroup:Int = 12
Const KNL_MSG_EnterGroup:Int = 13
Const KNL_MSG_LeaveGroup:Int = 14
Const KNL_MSG_DestroyGroup:Int = 15
Const KNL_MSG_GroupInfo:Int = 16
Const KNL_MSG_HostMigrate:Int = 17
Const KNL_MSG_TerminateSession:Int = 18
Const KNL_MSG_Handshake:Int = 19
Const KNL_MSG_ConnectionInfo:Int = 20
Const KNL_MSG_GameInfo:Int = 21
Const KNL_MSG_AcceptFile:Int = 22
Const KNL_MSG_FileAccepted:Int = 23
Const KNL_MSG_FileRefused:Int = 24
Const KNL_MSG_FileTransferComplete:Int = 25
Const KNL_MSG_TimerIgnited:Int = 26
Const KNL_MSG_TimerStop:Int = 27
Const KNL_MSG_HostID:Int = 28
Const KNL_MSG_CAPS:Int = 29
Const KNL_MSg_SPCAPS:Int = 30

' -------------
' Send Options:
' -------------
Const KNL_Priority_Normal:Int = 0
Const KNL_Priority_Low:Int = 1
Const KNL_Priority_High:Int = 2

Const KNL_Send_Sync:Int = 1
Const KNL_Send_Guaranteed:Int = 2
Const KNL_Send_CompleteOnProcess:Int = 4
Const KNL_Send_Sequential:Int = 8
Const KNL_Send_NoComplete:Int = 16
Const KNL_Send_NoIndicate:Int = 32

' --------------------
' Message Field Types:
' -------------------:
Const KNL_Fld_Byte:Int = 2
Const KNL_Fld_Word:Int = 4
Const KNL_Fld_Integer:Int = 8
Const KNL_Fld_Float:Int = 16
Const KNL_Fld_Double:Int = 32 ' Reserved * Not Implemented *
Const KNL_Fld_String:Int = 64
Const KNL_Fld_RawData:Int = 128

Const KNL_Fld_ByteArray:Int = 3
Const KNL_Fld_WordArray:Int = 5
Const KNL_Fld_IntegerArray:Int = 9
Const KNL_Fld_FloatArray:Int = 17
Const KNL_Fld_DoubleArray:Int = 33 ' Reserved * Not Implemented *
Const KNL_Fld_StringArray:Int = 65
Const KNL_Fld_RawArray:Int = 129

Const KNL_ArrayMask:Int = 1
Const KNL_TypeMask:Int = 254

' ---------------------------------------------------------------
' GUID stands fror Globally Unique IDentifier. Do Not use the key
' presented below For your Game Or App. Instead, generate a brand
' New one using Krylar's Kreations GUID Generator, which is part
' of K-NetLib(MultiPlayer Library) package.
' ---------------------------------------------------------------
Const GUID:String = "{E56FF876-B284-4469-9D36-7449F8028DDC}"


' --------------------
' DLL Loading and init
' --------------------
Const DllName:String = "K-NetLib.dll"
Local DllHandle = LoadLibraryA(DllName)

If DLLhandle = 0 Then
Notify "Unable to initialize dll : "+DllName
End
EndIf

' ---------------
' Function define
' ---------------
Global KNL_Initialize:Int(GameIDz$z, Service:Int, Session:Int)"Win32" = GetProcAddress(DllHandle,"KNL_Initialize")
Global KNL_Shutdown()"Win32" = GetProcAddress(DllHandle,"KNL_Shutdown")

Global KNL_GetVersion$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetVersion")
Global KNL_ParseString$z(SourceStr$z, Item:Int, Separator$z)"Win32" = GetProcAddress(DllHandle,"KNL_ParseString")

Global KNL_StringOption(Flag:Int)"Win32" = GetProcAddress(DllHandle,"KNL_StringOption")
Global KNL_SetReceiveQueueLimit(Limit:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SetReceiveQueueLimit")

Global KNL_SetSession:Int(Setting$z, Value$z)"Win32" = GetProcAddress(DllHandle,"KNL_SetSession")
Global KNL_GetSession$z(Setting$z)"Win32" = GetProcAddress(DllHandle,"KNL_GetSession")
Global KNL_GetServerName$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetServerName")
Global KNL_GetServerData$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetServerData")
Global KNL_GetGameInfo:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetGameInfo")
Global KNL_GetCaps:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetCaps")
Global KNL_SetCaps:Int(User$z, Value:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SetCaps")
Global KNL_GetSPCaps:Int(Provider:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetSPCaps")
Global KNL_SetSPCaps:Int(Provider:Int, BufferSize:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SetSPCaps")

Global KNL_CreateHost:Int(PortNR:Int, SessionName$z, Password$z, GameData$z, MaxPlayers:Int)"Win32" = GetProcAddress(DllHandle,"KNL_CreateHost")
Global KNL_Connect:Int(PlayerName$z, PlayerData$z, HostAddress$z, PortNR:Int, Password$z, Methode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_Connect")
Global KNL_CancelConnect:Int()"Win32" = GetProcAddress(DllHandle,"KNL_CancelConnect")
Global KNL_GetConnectionInfo:Int(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetConnectionInfo")
Global KNL_TerminateSession:Int()"Win32" = GetProcAddress(DllHandle,"KNL_TerminateSession")

Global KNL_StartTimer:Int()"Win32" = GetProcAddress(DllHandle,"KNL_StartTimer")
Global KNL_StopTimer:Int()"Win32" = GetProcAddress(DllHandle,"KNL_StopTimer")
Global KNL_GetTimer:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetTimer")

Global KNL_ErrorOption(Flag:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ErrorOption")
Global KNL_SetLogFile(FileName$z, Append:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SetLogFile")

Global KNL_CheckErrors:Int()"Win32" = GetProcAddress(DllHandle,"KNL_CheckErrors")
Global KNL_GetErrorCode:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetErrorCode")
Global KNL_GetErrorText$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetErrorText")
Global KNL_GetErrorMsg$z(ErrCode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetErrorMsg")
Global KNL_GetErrorLocalTime$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetErrorLocalTime")
Global KNL_GetErrorGameTime:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetErrorGameTime")
Global KNL_ErrorQueueCount:Int()"Win32" = GetProcAddress(DllHandle,"KNL_ErrorQueueCount")

Global KNL_CatchError(ErrCode:Int, ErrMsg$z)"Win32" = GetProcAddress(DllHandle,"KNL_CatchError")
Global KNL_ClearError()"Win32" = GetProcAddress(DllHandle,"KNL_ClearError")
Global KNL_ClearErrors()"Win32" = GetProcAddress(DllHandle,"KNL_ClearErrors")

Global KNL_UpdateNetwork:Int(DestID:Int, Priority:Int, TimeOut:Int, Flags:Int)"Win32" = GetProcAddress(DllHandle,"KNL_UpdateNetwork")
Global KNL_CancelMessage:Int(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_CancelMessage")
Global KNL_CancelMessages:Int(PlayerID:Int, Priority:Int)"Win32" = GetProcAddress(DllHandle,"KNL_CancelMessages")
Global KNL_CheckMessages:Int()"Win32" = GetProcAddress(DllHandle,"KNL_CheckMessages")
Global KNL_GetMessageID:Int()"Win32" = GetProcAddress(DllHandle,"KNL_GetMessageID")
Global KNL_MessageFields:Int()"Win32" = GetProcAddress(DllHandle,"KNL_MessageFields")
Global KNL_SendQueueCount:Int(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SendQueueCount")
Global KNL_ReceiveQueueCount:Int()"Win32" = GetProcAddress(DllHandle,"KNL_ReceiveQueueCount")
Global KNL_ForwardMessage:Int()"Win32" = GetProcAddress(DllHandle,"KNL_ForwardMessage")

Global KNL_ClearMessage()"Win32" = GetProcAddress(DllHandle,"KNL_ClearMessage")
Global KNL_ClearStream()"Win32" = GetProcAddress(DllHandle,"KNL_ClearStream")

Global KNL_SetFieldName:Int(Name$z)"Win32" = GetProcAddress(DllHandle,"KNL_SetFieldName")
Global KNL_SendInteger:Int(Value:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SendInteger")
Global KNL_SendFloat:Int(Value:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SendFloat")
Global KNL_SendString:Int(Value$z)"Win32" = GetProcAddress(DllHandle,"KNL_SendString")
Global KNL_SendByte:Int(Value:Byte)"Win32" = GetProcAddress(DllHandle,"KNL_SendByte")

Global KNL_ArraySendInteger:Int(StartAddress:Int, Elements:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ArraySendInteger")
Global KNL_ArraySendFloat:Int(StartAddress:Int, Elements:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ArraySendFloat")
Global KNL_ArraySendParsedString:Int(SourceStr$z, Separator$z)"Win32" = GetProcAddress(DllHandle,"KNL_ArraySendParsedString")

Global KNL_GetByte:Byte(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetByte")
Global KNL_GetInteger:Int(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetInteger")
Global KNL_GetFloat:Float(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetFloat")
Global KNL_GetString$z(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetString")

Global KNL_ArrayGetInteger:Float(FldIdx:Int, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ArrayGetInteger")
Global KNL_ArrayGetFloat:Float(FldIdx:Int, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ArrayGetFloat")
Global KNL_ArrayGetString$z(FldIdx:Int, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_ArrayGetString")

Global KNL_FieldName$z(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldName")
Global KNL_FieldType:Int(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldType")
Global KNL_FieldSize:Int(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldSize")
Global KNL_FieldIndex:Int(FldName$z)"Win32" = GetProcAddress(DllHandle,"KNL_FieldIndex")
Global KNL_FieldElements:Int(FldIdx:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldElements")

Global KNL_FieldGetInteger:Int(FldName$z)"Win32" = GetProcAddress(DllHandle,"KNL_FieldGetInteger")
Global KNL_FieldGetFloat:Float(FldName$z)"Win32" = GetProcAddress(DllHandle,"KNL_FieldGetFloat")
Global KNL_FieldGetString$z(FldName$z)"Win32" = GetProcAddress(DllHandle,"KNL_FieldGetString")

Global KNL_FieldArrayGetInteger:Int(FldName$z, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldArrayGetInteger")
Global KNL_FieldArrayGetFloat:Float(FldName$z, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldArrayGetFloat")
Global KNL_FieldArrayGetString$z(FldName$z, Element:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FieldArrayGetString")

Global KNL_FileSend:Int(PlayerID:Int, FileID:Int, FileName$z, CompressionLevel:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FileSend")
Global KNL_FileAccept:Int(Handle:Int, FileName$z)"Win32" = GetProcAddress(DllHandle,"KNL_FileAccept")
Global KNL_FileRefuse:Int(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FileRefuse")
Global KNL_FileProgress:Float(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FileProgress")
Global KNL_FileCancel:Int(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_FileCancel")

Global KNL_GroupCreate:Int(GroupName$z, GroupData$z, Methode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupCreate")
Global KNL_GroupAddPlayer:Int(PlayerID:Int, GroupID:Int, Methode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupAddPlayer")
Global KNL_GroupRemovePlayer:Int(PlayerID:Int, GroupID:Int, Methode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupRemovePlayer")
Global KNL_GroupDestroy:Int(GroupID:Int, Methode:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupDestroy")
Global KNL_GroupGetName$z(GroupID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupGetName")
Global KNL_GroupGetData$z(GroupID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupGetData")
Global KNL_GroupGetContext:Int(GroupID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupGetContext")
Global KNL_GroupGetList:Int(GroupID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupGetList")
Global KNL_GroupListCount:Int(ListHandle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupListCount")
Global KNL_GroupListGetID:Int(ListHandle:Int,Index:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupListGetID")
Global KNL_GroupListGetName$z(ListHandle:Int,Index:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupListGetName")
Global KNL_GroupListGetData$z(ListHandle:Int,Index:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupListGetData")
Global KNL_GroupFreeList(ListHandle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GroupFreeList")

Global KNL_SetPlayerInfo:Int(PlayerName$z, PlayerData$z)"Win32" = GetProcAddress(DllHandle,"KNL_SetPlayerInfo")
Global KNL_GetPlayerName$z(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetPlayerName")
Global KNL_GetPlayerData$z(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetPlayerData")
Global KNL_DestroyPlayer:Int(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DestroyPlayer")

Global KNL_EnumerateProviders:Int()"Win32" = GetProcAddress(DllHandle,"KNL_EnumerateProviders")
Global KNL_GetProviderName$z(Provider:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetProviderName")
Global KNL_GetProviderGUID$z(Provider:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetProviderGUID")

Global KNL_EnumerateDevices:Int(Provider:Int)"Win32" = GetProcAddress(DllHandle,"KNL_EnumerateDevices")
Global KNL_GetDeviceName$z(Device:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetDeviceName")
Global KNL_GetDeviceGUID$z(Device:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetDeviceGUID")

Global KNL_EnumerateHosts:Int(PortNR:Int)"Win32" = GetProcAddress(DllHandle,"KNL_EnumerateHosts")
Global KNL_StopHostsEnumeration:Int()"Win32" = GetProcAddress(DllHandle,"KNL_StopHostsEnumeration")
Global KNL_QueryHost:Int(IP$z,PortNR:Int)"Win32" = GetProcAddress(DllHandle,"KNL_QueryHost")

Global KNL_GetComputerName$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetComputerName")
Global KNL_CountLocalIPs:Int()"Win32" = GetProcAddress(DllHandle,"KNL_CountLocalIPs")
Global KNL_GetLocalIP$z(Index:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetLocalIP")
Global KNL_GetPlayerIP$z(PlayerID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_GetPlayerIP")
Global KNL_GetHostIP$z()"Win32" = GetProcAddress(DllHandle,"KNL_GetHostIP")

Global KNL_ReadINI$z(File$z, Section$z, Parameter$z, Defaut$z)"Win32" = GetProcAddress(DllHandle,"KNL_ReadINI")
Global KNL_WriteINI(File$z, Section$z, Parameter$z, Value$z)"Win32" = GetProcAddress(DllHandle,"KNL_WriteINI")

Global KNL_SetBanFile(FileName$z)"Win32" = GetProcAddress(DllHandle,"KNL_SetBanFile")
Global KNL_BanIP(IP$z, Duration:Int)"Win32" = GetProcAddress(DllHandle,"KNL_BanIP")
Global KNL_AllowIP(IP$z)"Win32" = GetProcAddress(DllHandle,"KNL_AllowIP")

Global KNL_SplineDestroy(Spline:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SplineDestroy")
Global KNL_SplineSetKnot(Spline:Int, Knot:Int, X:Float, Y:Float, Z:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SplineSetKnot")
Global KNL_SplineCalculate(Spline:Int)"Win32" = GetProcAddress(DllHandle,"KNL_SplineCalculate")

Global KNL_SplineCreate:Int(Knots:Int, Whole:Float, Piece:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SplineCreate")
Global KNL_SplineGetX:Float(Spline:Int, Frame:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SplineGetX")
Global KNL_SplineGetY:Float(Spline:Int, Frame:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SplineGetY")
Global KNL_SplineGetZ:Float(Spline:Int, Frame:Float)"Win32" = GetProcAddress(DllHandle,"KNL_SplineGetZ")

Global KNL_Download:Int(Client$z, URL$z, LocalFile$z, ID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_Download")
Global KNL_DownloadGetHandle:Int(Client$z, URL$z, LocalFile$z, ID:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadGetHandle")
Global KNL_DownloadSize:Int(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadSize")
Global KNL_DownloadBytes:Int(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadBytes")
Global KNL_DownloadProgress:Float(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadProgress")
Global KNL_DownloadCancel(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadCancel")
Global KNL_DownloadFreeHandle(Handle:Int)"Win32" = GetProcAddress(DllHandle,"KNL_DownloadFreeHandle")




' ---------------------------
' Function to create a server
' ---------------------------
Function KNL_CreateServer(Port:Int,PlayerName:String="[Server]",GameName:String="MyGame",Debug:Byte=True)
' -------------------
' Define log filename
' -------------------
Local LogFile:String = AppDir$ + "nsg_server.log"

' ---------------------------
' Start our logging To a file
' ---------------------------
If Debug=True Then
' --------------------------
' Catch error in a user file
' --------------------------
KNL_ErrorOption(2)

' -----------------------------------------------------
' Create the actual file, using 0 To overwrite previous
' -----------------------------------------------------
KNL_SetLogFile(LogFile,0)

' ----------------------
' put a quick message in
' ----------------------
KNL_CatchError(0,"Log Start")

' -------------------------------------
' Log that we're about to intialize KNL
' -------------------------------------
KNL_CatchError(0,"Initializing KNL...")
Print "Initializing KNL..."

' -----------------------------------------------------------------------------------------------
' And put in the current version of KNL being used (To make sure there's no mismatch upon errors)
' -----------------------------------------------------------------------------------------------
KNL_CatchError(0,KNL_GetVersion())
EndIf

' -------------------------------------------------------
' And Then init it as a peer-To-peer client, using TCPIP
' And link tuning...also make sure To use the proper GUID
' -------------------------------------------------------
If KNL_Initialize(GUID$,KNL_Service_TCPIP,KNL_INI_Peer2Peer + KNL_INI_Server + KNL_INI_MigrateHost) Then

' -------------------------------
' we were successful, so Log that
' -------------------------------
If Debug=True Then KNL_CatchError(0,"KNL initialization OK.")
Print "KNL initialization OK."

' ------------------------------
' And now Try To create the host
' ------------------------------
If KNL_CreateHost(Port,GameName,"","",8) Then

' ---------------------------------
' Tell KNL what my player's name is
' ---------------------------------
KNL_SetPlayerInfo(PlayerName,"")

' ---------------------------------
' Log that we created the host okay
' ---------------------------------
If Debug=True Then KNL_CatchError(0,"Host Created OK.")
Print "Host Created OK."
Else
' --------------------------------------
' could Not create the host, so Log that
' --------------------------------------
If Debug=True Then KNL_CatchError(0,"Host Creation Failed!")
Print "Host Creation Failed!"
Return False
EndIf
Else
' -------------------------------------------
' Log that And show a little message about it
' -------------------------------------------
If Debug=True Then KNL_CatchError(0,"KNL Initialization Failed!")

Print "KNL Initialization Failed!"
Return False
EndIf

Return True
End Function

' -------------------------------
' Function to connect to a server
' -------------------------------
Function KNL_ConnectServer(Ip:String,Port:Int,PlayerName:String="Player",Debug:Byte=True)
' -------------------
' Define log filename
' -------------------
Local LogFile:String = AppDir$ + "nsg_client.log"

' ---------------------------
' Start our logging To a file
' ---------------------------
If Debug=True Then
' --------------------------
' Catch error in a user file
' --------------------------
KNL_ErrorOption(2)

' -----------------------------------------------------
' Create the actual file, using 0 To overwrite previous
' -----------------------------------------------------
KNL_SetLogFile(LogFile,0)

' ----------------------
' put a quick message in
' ----------------------
KNL_CatchError(0,"Log Start")

' ----------------------------------------------
' first, though, Log that we're initializing KNL
' ----------------------------------------------
KNL_CatchError(0,"Initializing KNL...")
Print "Initializing KNL..."

' -----------------------------------------------------------------------------------------------
' And put in the current version of KNL being used (To make sure there's no mismatch upon errors)
' -----------------------------------------------------------------------------------------------
KNL_CatchError(0,KNL_GetVersion())
EndIf

' --------------------------------------------------------
' tell the user that we're trying to connect to the server
' --------------------------------------------------------
Print "Connecting to server..."

' -------------------------------------------------------
' And Then init it as a peer-To-peer client, using TCPIP
' And link tuning...also make sure To use the proper GUID
' -------------------------------------------------------
If KNL_Initialize(GUID$,KNL_Service_TCPIP, KNL_INI_Peer2Peer + KNL_INI_Client + KNL_INI_MigrateHost) ' KNL_INI_Guaranteed

' -------------------------------
' we were successful, so Log that
' -------------------------------
If Debug=True Then KNL_CatchError(0,"KNL Initialization OK.")
Print "KNL Initialization OK."

' ------------------------------------
' And now Try To connect To the server
' ------------------------------------
If KNL_Connect(PlayerName,"",Ip,Port,"",1)

' ---------------------
' Log that we succeeded
' ---------------------
If Debug=True Then KNL_CatchError(0,"Connected successfuly!")
Print "Connected successfuly!"
Else

' --------------------------------------------
' Log that we failed And show a little message
' --------------------------------------------
If Debug=True Then KNL_CatchError(0,"Connection failed!")
Print "Connection failed!"
Return False
EndIf
Else
' -------------------------------------------
' Log that And show a little message about it
' -------------------------------------------
If Debug=True Then KNL_CatchError(0,"KNL Initialization Failed!")
Print "KNL Initialization Failed!"
Return False
EndIf

Return True
End Function
')

And another used include : Include "K-Nework.bmx"

format_codebox('
' ----------------------------
' setup a Type For our players
' ----------------------------
Type Players
Field ID:Int ' the player's DirectPlay GameID
Field Name:String ' what's the player's name?
Field X:Int ' what's their X position?
Field Y:Int ' what's their Y position?
Field Z:Int ' what's their Y position?
Field Shoot:Int ' does it shoot?
Field Status:Byte ' 0 = waiting, 1 = playing
EndType

Global PlayersList:TList=CreateList()


Global KNL_PlayersNumber:Int ' Used to know how many player are connected
Global KNL_PlayersName:String ' Used to know your hown name
Global KNL_MyID:Int ' Used to know your hown ID



Function KNL_CreatePlayer:Players(ID:Int,Name:String)
Local P:Players=New Players
P.Id=Id
P.Name=Name

P.x=0
P.y=0

ListAddLast PlayersList,P
Return P
EndFunction

Function KNL_DrawPlayer()
KNL_PlayersNumber=0

For Local P:Players = EachIn PlayersList
If P.Shoot=True Then
SetColor 255,0,0
Else
SetColor 0,255,0
EndIf

DrawRect P.x,P.y,10,10

KNL_PlayersNumber:+1
Next
EndFunction

Function KNL_DrawList(Px:Int,Py:Int)
Local Pos_Y:Int=0
Local Temp_IP:String

For Local P:Players = EachIn PlayersList
SetColor 255,255,255

DrawText "["+KNL_GetPlayerIP(P.Id)+"] | "+P.Name,Px,Py+Pos_Y
Pos_Y:+13
Next
EndFunction

Function KNL_ControlPlayer()
If MouseDown(2) Then

For Local P:Players = EachIn PlayersList
If P.ID=KNL_MyID Then
P.X=MouseX()
P.Y=MouseY()
P.Shoot=MouseDown(1)

' --------------------------
' Send information to server
' --------------------------
KNL_SendString(P.X+"|"+P.Y+"|"+P.Shoot+"|")
EndIf
Next

EndIf
EndFunction

Function KNL_DeletePlayer(Id:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
ListRemove(PlayersList,p )
P=Null ; Exit
EndIf
Next
EndFunction




Function KNL_SetPlayerX(Id:Int,X:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
P.X=X
EndIf
Next
EndFunction

Function KNL_SetPlayerY(Id:Int,Y:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
P.Y=Y
EndIf
Next
EndFunction

Function KNL_SetPlayerZ(Id:Int,Z:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
P.Z=Z
EndIf
Next
EndFunction

Function KNL_SetPlayerShoot:Int(Id:Int,Shoot:Byte)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
P.Shoot=Shoot
EndIf
Next
EndFunction




Function KNL_GetPlayerX:Int(Id:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
Return P.X
EndIf
Next
EndFunction

Function KNL_GetPlayerY:Int(Id:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
Return P.Y
EndIf
Next
EndFunction

Function KNL_GetPlayerZ:Int(Id:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
Return P.Z
EndIf
Next
EndFunction

Function KNL_GetPlayerShoot:Int(Id:Int)
For Local P:Players = EachIn PlayersList
If P.Id=Id Then
Return P.Shoot
EndIf
Next
EndFunction


Function KNL_RefreshNetwork()
Local Temp_ID:Int

' ------------------------------------------
' If there are any messages in the KNL queue
' ------------------------------------------
While KNL_CheckMessages()
' ---------------------------------------
' see what the ID of the first message is
' ---------------------------------------
Select KNL_GetMessageID()

' ---------------------------------------------------
' If we are the server then i create your hown player
' ---------------------------------------------------
Case KNL_MSG_HostID
KNL_MyID = KNL_FieldGetInteger("LocalID")
KNL_CreatePlayer(KNL_MyID,KNL_PlayersName)

' --------------------------------------------------------------------------------
' If you are a client and correctly connected to server, i create your hown player
' --------------------------------------------------------------------------------
Case KNL_MSG_ConnectComplete
KNL_MyID = KNL_FieldGetInteger("LocalID")
KNL_CreatePlayer(KNL_MyID,KNL_PlayersName)

' -------------------
' Create a new player
' -------------------
Case KNL_MSG_CreatePlayer
Temp_ID = KNL_FieldGetInteger("PlayerID")
KNL_CreatePlayer(Temp_ID,KNL_GetPlayerName(Temp_ID))

' --------------------------------------------------------
' If a new player is connected you must send your position
' --------------------------------------------------------
KNL_SendString(KNL_GetPlayerX(KNL_MyID)+"|"+KNL_GetPlayerY(KNL_MyID)+"|"+KNL_GetPlayerShoot(KNL_MyID)+"|")

' ----------------
' A player as left
' ----------------
Case KNL_MSG_DestroyPlayer
Temp_ID = KNL_GetInteger(0)
KNL_DeletePlayer(Temp_ID)

' -----------------------
' If a message is receive
' -----------------------
Case KNL_MSG_Receive
Local Fields:Int = KNL_MessageFields()

For Local I:Int = 0 To Fields - 1
If KNL_FieldName(I)="Fld0" And KNL_FieldType(I)=64 Then
Temp_ID=KNL_FieldGetInteger("SenderID")

KNL_SetPlayerX(Temp_ID,Int(KNL_ParseString(KNL_GetString(I),1,"|")))
KNL_SetPlayerY(Temp_ID,Int(KNL_ParseString(KNL_GetString(I),2,"|")))
KNL_SetPlayerShoot(Temp_ID,Int(KNL_ParseString(KNL_GetString(I),3,"|")))
EndIf
Next
EndSelect

KNL_ClearMessage()
Wend

' ------------------
' Update KNL Network
' ------------------
KNL_UpdateNetwork(0,0,1500,2)
End Function
')


and one example client / server :

format_codebox('
Strict

Include "K-Network.bmx"
Include "K-NetLib.bmx"


Graphics 400,300,0


' -------
' Example
' -------
KNL_PlayersName="Server_"+Rand(MilliSecs())
Print "KNL Debug : "+KNL_CreateServer(4662,KNL_PlayersName,"MyGame",False)

' ---------
' Main Loop
' ---------
While Not KeyHit(KEY_ESCAPE)
Cls

KNL_DrawPlayer()
KNL_ControlPlayer()

KNL_RefreshNetwork()

KNL_DrawList(10,10)
Flip
Wend

KNL_Shutdown()
End
')

format_codebox('
Strict

Include "K-NetLib.bmx"
Include "K-Network.bmx"

Graphics 400,300,0

' ---------
' Main Loop
' ---------
KNL_PlayersName="Client_"+Rand(MilliSecs())


Print "KNL Debug : "+KNL_ConnectServer("82.241.165.222",4662,KNL_PlayersName,False)
'Print "KNL Debug : "+KNL_ConnectServer("localhost",4662,KNL_PlayersName,False)
Print "MSG Host IP : "+KNL_GetHostIP()

While Not KeyHit(KEY_ESCAPE)
Cls

KNL_DrawPlayer()
KNL_ControlPlayer()

KNL_RefreshNetwork()


KNL_DrawList(10,10)
Flip
Wend

KNL_Shutdown()
End
')


Boiled Sweets(Posted 2006) [#2]
awesome work Filax but how about a Blitz 3d version ;-)


Filax(Posted 2006) [#3]
In the original package ! (see site) there is an example for
blitz 3D !

Here is an example with spline :

format_codebox('
Strict

Include "..\K-NetLib.bmx"
Include "..\K-Network.bmx"

Graphics 400,300,0

' ---------
' Main Loop
' ---------



Local X:Int
Local Y:Int
Local FrameCur:Int
Local FrameMax:Int=3000

Global SplineID = KNL_SplineCreate(5, FrameMax, 30)
KNL_SplineSetKnot(SplineID,0,360,94,0)
KNL_SplineSetKnot(SplineID,1,265,25,0)
KNL_SplineSetKnot(SplineID,2,46,162,0)
KNL_SplineSetKnot(SplineID,3,120,25,0)
KNL_SplineSetKnot(SplineID,4,363,166,0)
KNL_SplineCalculate(SplineID)




While Not KeyHit(KEY_ESCAPE)
Cls

X = KNL_SplineGetX(SplineID, FrameCur)
Y = KNL_SplineGetY(SplineID, FrameCur)
DrawRect X,Y,10,10
FrameCur = FrameCur + 30 ; If FrameCur>FrameMax Then FrameCur=0

DrawText FrameCur,10,10


Flip
Wend

KNL_Shutdown()
End

')


Boiled Sweets(Posted 2006) [#4]
AHA! Cool!


Filax(Posted 2006) [#5]
See spline example !


Jake L.(Posted 2006) [#6]
Yes, it's great. I tested it on it's release but ran into compatibility-issues. It refused to initialize on my laptop allthough other Apps/Games using Directplay worked well on this machine.

Beside that I liked it very much.


kfprimm(Posted 2006) [#7]
Just found a bug, it's in the original k-netlib.bb
format_code('
Const KNL_INI_LinkTunning:Int = 128 ' Allows DX To tune the network flow.
')
needs to be,
format_code('Const KNL_INI_LinkTuning:Int = 128 ' Allows DX To tune the network flow.
')
It's amazing what Strict finds :)


Booticus(Posted 2006) [#8]
Thanks Filax! Awesome as always!


@rtur(Posted 2006) [#9]
It requres DirectX 9 to work, as I understood. It is not very good thing :(


Filax(Posted 2006) [#10]
Who have not directx 9 installed ?


@rtur(Posted 2006) [#11]
I'm developing casual games with B3D, so targeting on old systems, office comps etc with minimum DirectX 7 installed.
But this lib is realy good. Shame if it cannot be run on systems with only DirectX7 installed:(


Armitage 1982(Posted 2006) [#12]
@ @rtur : And why not offer the redist DX9 with your game ?

Exellent piece of software !
Thanks for this Filax ;)


ckob(Posted 2006) [#13]
hmm anyone get the file sending working with this?


Filax(Posted 2006) [#14]
? what ckob ?


FlameDuck(Posted 2006) [#15]
Who have not directx 9 installed ?
People with a Mac.


Filax(Posted 2006) [#16]
Lol it's sure Flame :) but mac people don't usee DLL too :)


Winni(Posted 2006) [#17]
>>Who have not directx 9 installed ?
>People with a Mac.
People with Linux.

BlitzMax was designed to be platform independent, but by using DirectX you give that up and tie yourself to Windows.


Dreamora(Posted 2006) [#18]
It was designed to support platform independence. This actually does not mean that you need to support it. Especially if you are a one men or small team. BM is not "copy and one click" multiplatform.


Winni(Posted 2006) [#19]
Yep, there is no copy-and-one-click-multiplatform tool - Java included. And of course you are right, you do not have to support multiple platforms.

However, there seems to be a tendency that many of the third party add-ons for BlitzMax are going to be available for the Windows platform only. And that is NOT a good development.


tonyg(Posted 2006) [#20]
I must be missing something here.
Filax has wrapped a Windows only (DirectPlay), non-Bmax product and made it freely available.
How is he supposed to make it magically cross-platform?


Dreamora(Posted 2006) [#21]
Winni: Nobody prevents you from writting a cross plattform version or a similar modul which is cross plattform.
Its the decision of the ones writting the moduls for which plattform it is and I'm glad that at least 3rd party modul writters make sure that the stuff works with acceptable hardware / driver support on current gen windows systems, unlike Max2D.


ckob(Posted 2006) [#22]
well you could always try to make directx crossplatform ;P

just kidding for those without a sense of humor


Filax(Posted 2006) [#23]
tonyg : Maybe by re-compiling the DLL for others
OS ? library is made with purebasic ? i don't know...


tonyg(Posted 2006) [#24]
The lib is written in Purebasic but uses the DirectPlay8 interface.


Stu_ovine(Posted 2006) [#25]
Typical just as I find a good library - development stops on it :S


Winni(Posted 2006) [#26]
TonyG:
I´m not saying anything against Filax´work! I only said that in general it is not a good thing that most of stuff coming out these days is Windows-only - just like there was no other platform supported by BlitzMax.

Dreamora:
You´re right. Like the old Fritz said: "In meinem Land soll jeder nach seiner eigenen Facon glücklich werden." For non-german speakers roughly translated: In my country everybody shall become happy following their own beliefs. There´s nothing wrong with that.

As for the idea with writing the module: Thank you, but when time allows I am already porting an NNTP server that I had written in C# to BlitzMax. By the way, the Max version uses TeaMonkey´s SQLite wrapper and SQLiter from Pyroplaysite - cross platform stuff.

-- If -- I finally have enough time to finish it, I will post it here in case somebody might have a use for it. At the moment, there is a good chance that I might stroll down the Java alley, at least for a while to see how life is down there.

Anyway - The C# version still is in two versions on my website (and under GPL), one for .NET with an installer and bundled with the MSDE 2000 (a remainder from the time when I was still using Windows: www.wmaus.net/nomad.zip), the other version is source code only for Mono and MySQL (www.wmaus.net/MonoNomad.zip). Once you get it compiled, the Mono version will work on OS X, Linux and Windows.

I wrote that thing to learn C#, and even though the source code is multithreaded, it´s dirty and really sucks. I already like my BlitzMax version better; it naturally is not multithreaded, but it is MUCH "cleaner" and solves some problems that the C# version had.

Whatsoever, none of that has anything to do with the original subject, which for me simply is: I find it sad that people use BlitzMax mostly for Win32-only stuff, even though it is capable of doing so much more. Period and good night. ;-)


xlsior(Posted 2006) [#27]
I´m not saying anything against Filax´work! I only said that in general it is not a good thing that most of stuff coming out these days is Windows-only - just like there was no other platform supported by BlitzMax.


what do you expect?

Windows has a 90% market share, while Apple and Linux are both hovering around 5%. And on top of that, Apple's 5% is divided between PPC and Intel.

Until those numbers change significantly, nothing is going to change. You're bound to see many external libraries tied to the individual platforms -- predominantly windows, thanks to its huge market share.


Filax(Posted 2006) [#28]
I agree ...


Winni(Posted 2006) [#29]
> what do you expect?

I certainly don't expect it, but I wish for this: A change in the mindset.


> Until those numbers change significantly, nothing is going to change. You're bound to see many external libraries tied to the individual platforms -- predominantly windows, thanks to its huge market share.


Which in itself is a self-fulfilling prophecy, fulfilling the circle to ensure that Windows stays the predominantly platform.

By the way, the market share of the target platform is not always relevant for the income it generates. You will find enough people who will tell you that OS X, although it has a much smaller share, generates at least as much income for them as the Windows version of their products. But that certainly cannot be generalized and what works for some companies does not necessarily work for others. However, I think it is safe to say that Mac users typically are willing to pay for good, Mac-like stuff, while Linux users usually mostly want "free" (as both in beer and speech) stuff. And on Windows... One of my old companies went bankrupt, but you can still download our products from the eDonkey network. You get the idea.

Anyway, guys, thanks for the discussion and your comments. Now let's move on to a new topic. ;-)


Tyler(Posted 2006) [#30]
Hmm, seems I'm getting a compile error involving CStrings (and although I know it has something to do with the $z on the end of some of these variables, I have no clue how to fix it) Any help would be great :D Thanks.


xlsior(Posted 2006) [#31]
which in itself is a self-fulfilling prophecy, fulfilling the circle to ensure that Windows stays the predominantly platform.


Yup.

Wether we like it or not, Microsoft has an amazing cash-cow going, and they are doing everything they can to keep it that way. They are going out of their way to keep 'proprietary' information from leveling the playing field, even after being tied in court for year upon year, and hundreds of millions of euro's in fines for failure to comply.


ckob(Posted 2006) [#32]
well I still havent got the file sending crap to work lol.


Steffenk(Posted 2006) [#33]
Maybe i'm a bit off-topic, but I think this http://enet.bespin.org/ (enet) looks like quite a good netlib, has anyone done a bmax wrapper for it?


deps(Posted 2006) [#34]
gnet is based on it. :)


Armitage 1982(Posted 2006) [#35]
A Great lib Filax :)
I know "K-NetLib.bmx" need to be modify for our need but :
format_code('
Local LogFile:String = AppDir$ + "nsg_server.log"
...
Local LogFile:String = AppDir$ + "nsg_client.log"
')
Should be :
format_code('
Local LogFile:String = AppDir$ +"/"+ "nsg_server.log"
...
Local LogFile:String = AppDir$ +"/"+ "nsg_client.log"
')
Thanks for your work :D


Filax(Posted 2007) [#36]
Do you think that KNetlib is equal to RakNet lib ?
Because i find that the Raknet library is a little bit
abusive ;/ 100$ only for one product :(


*(Posted 2007) [#37]
Its a nice lib, but I will continue work on my own BlitzMax networking library as I need it to be able to be compiled on Linux, Mac OSX as well as Win32 that was the main reason for buying BlitzMax :)


panton(Posted 2007) [#38]
Where to download the original version? i want a blitz3d version


andre72(Posted 2007) [#39]
You just have to fellow the Filax in the first post to find it ...
Here's the link to the download section:
http://www.chromatick.com.br/knl/index.php


panton(Posted 2007) [#40]
but where to download the blitz3d version?