Another WTF.....

BlitzMax Forums/BlitzMax Programming/Another WTF.....

DH(Posted 2009) [#1]
Why the hell wont this compile?

MainApp.bmx
Framework BRL.StandardIO
Import "ConnectionMonitor.bmx"
Import "ActiveClient.bmx"
SuperStrict

'Global DEBUG_APP:Int = 0
Global EXIT_APP:Int = 0

ConnectionMonitor.CreateTcp( 9050 )

While Not EXIT_APP
	'If KeyDown(KEY_ESCAPE) Then EXIT_APP = 1
	ConnectionMonitor.Update()
	ActiveClient.Update()

Wend
ConnectionMonitor.DestroyAll()

End


ConnectionMonitor.bmx
Import BRL.LinkedList
Import BRL.Socket
Import "Client.bmx"
Strict
Type ConnectionMonitor
	Global ConnectionMonitors:TList = CreateList()
	Field port:Int, socketType:Int
	Field socket:TSocket
	Field monitorLink:TLink
	
	Function CreateTcp( port:Int )
		Local monitor:ConnectionMonitor = New ConnectionMonitor
		monitor.port = port
		monitor.socket = CreateTCPSocket()
		If Not BindSocket( monitor.socket, monitor.port) Or Not SocketListen( monitor.socket ) Then
			Print "Unable to bind TCPSocket to:" + port
			CloseSocket( monitor.socket )
			Return
		EndIf
		monitorLink = ConnectionMonitors.AddLast( client )
	End Function
	
	Function CreateUdp( port:Int )
	
	End Function
	
	Function Update()
		Local monitor:ConnectionMonitor
		For monitor = EachIn ConnectionMonitors
			monitor._Update()
		Next
	End Function
	
	Function DestroyAll()
		Local monitor:ConnectionMonitor
		For monitor = EachIn ConnectionMonitors
			monitor.Destroy()
		Next
	End Function
	
	Method Destroy()
		monitorLink.Remove()
		CloseSocket( socket )
	End Method
	
	Method _Update()
		Select socketType
			Case 0
				Local newSocket:TSocket = SocketAccept( socket )
				If newSocket Then CClient.Create( newSocket )
			Case 1
			
		End Select
	End Method
	
End Type


NetworkId.bmx
Import "Client.bmx"
Strict

Type NetworkID
	Global NetworkIDArray:CClient[] = New CClient[50]
	
	Function AquireId:Int( client:CClient )
		For idIndex:Int = 1 To ( NetworkIDArray.length - 1 )
			If NetworkIDArray[ idIndex ] = Null Then
				NetworkIDArray[ idIndex ] = client
				Return idIndex
			EndIf
		Next
		Return 0
	End Function
	
	Function ReleaseId( currentID:Int )
		NetworkIDArray[ currentID ] = Null
	End Function
	
	Function GetClient:CClient( atId:Int )
		Return NetworkIDArray[ atId ]
	End Function

End Type


ActiveClient.bmx
Import BRL.LinkedList
Import "Client.bmx"
Strict
Type ActiveClient
	Global ActiveClientList:TList = CreateList()
	
	Function Add:TLink( client:CClient )
		If client = Null Then Return Null
		Return ActiveClientList.AddLast( client )
	End Function
	
	Function Remove( clientLink:TLink )
		If clientLink = Null Then Return
		clientLink.Remove()
	End Function
	
	Function Update()
		Local client:CClient
		For client = EachIn ActiveClientList
			client.Update()
		Next
	End Function
	
End Type


Client.bmx
Import BRL.LinkedList
Import BRL.Socket
Import "NetworkId.bmx"
Import "ActiveClient.bmx"
Strict

Type CClient
	Field activeClientLink:TLink
	Field networkID:Int, ip:Int, port:Int, lastActivityMs:Int
	Field tcpSocket:TSocket, udpSocket:TSocket
	Field ipString:String
	
	Method Update()
		If Not Connected() Then Return
	End Method
	
	Method Connected:Int()
		If MilliSecs() - lastActivityMs > 5000 Then
			Destroy()
			Return False
		EndIf
		Return True
	End Method
	
	Function Create( socket:TSocket )
		Local client:CClient= New CClient
		client.networkID = NetworkID.AquireId( client )
		client.activeClientLink = ActiveClient.Add( client )
		client.tcpSocket = socket
	EndFunction
	
	Method Destroy()
		activeClientLink.Remove()
		activeClientLink = Null
		NetworkID.ReleaseId( networkID )
		networkID = 0
	End Method
	
	

End Type



It keeps erring: "Unable to open file '.bmx/Client.bmx.debug.win32.x86.i'


Amon(Posted 2009) [#2]
ConnectionMonitor imports Client.bmx but you're also importing it in the rest so you're importing in a loop.

Hope that makes sense???


DH(Posted 2009) [#3]
Nope, cause if I dont use "Import Client.bmx" in the sources where I use CClient, then it errors out saying it doesnt know what CClient is.....

How can one use references if the compile isn't smart enough to do different compiling passes....?


TaskMaster(Posted 2009) [#4]
Each one relies on the other. So, they cannot compile. You have a chicken and egg scenario...


TaskMaster(Posted 2009) [#5]
Files that are imported are compiled separately.

You would be better off combining both of those files. Or, Including one into the other.


Brucey(Posted 2009) [#6]
Or, changing your design.


DH(Posted 2009) [#7]
Ok, so without having to merge files, how would you arrange the imports so that it compiles?

Or, changing your design.

Ahhh yes. I suppose.


N(Posted 2009) [#8]
Ok, so without having to merge files, how would you arrange the imports so that it compiles?
Make the client not depend on classes that it doesn't need to depend on.


DH(Posted 2009) [#9]
Yeah, your all right. I am expecting a bit too much out of Blitzmax. I'll go back to the drawing board and simplify the design......


Evil Roy Ferguson(Posted 2009) [#10]
You've actually got a few problems preventing compilation.

1. Cyclic dependency is probably the one you're having trouble with. This is easy to fix with an interface in your case.

IClient.bmx
Type IClient
	Method Update() Abstract		
	Method Connected:Int() Abstract			
	Method Destroy() Abstract		
End Type


Make CClient import and extend IClient, and in NetworkID and ActiveClient, import IClient and use it instead of CClient.

2. Rename your NetworkID type to something else -- you have member variables with the same name and it's causing name resolution conflicts.

3. Your ConnectionMonitor's CreateTcp function is not fully implemented yet.