Hello
I'm trying to write a gameserver, the gameserver is using good old TCP sockets for communication.
I have the following structure:
The problem that arises here: When a networkclient is "attached" to the player class, it should perform a CallSub to the Player class, which handles player messages, not to the GameServerSocket, which only handles network data from unauthenticated sockets (thus not players). The class to callback to is in other words dynamic.
Basically, when a new socket is connected, I create a NetworkClient in the GameserverSocket class, and pass an Eventname and a Class to callback to (Me)
Where the NetworkClient.Initialize code looks like this:
Notice that ParentToCallback is an Object in this context, as it could be a GameServerSocket object or a Player object that is needs to be used in the CallSub (if authenticated yes or no)
The Callback is used this way in the NetworkClient Class:
But, I get an error when I try to pass "Me" (from the GameServerSocket object) to the Initialize ParentToCallback parameter, stating:
GameServerSocket cannot be converted to Object (while my thinking process was: It shouldn't be converted, it should just hold a "lowest common denominator' reference to it, and I was thinking Object was somekind of baseclass for everything.)
What kind of type should I use for unknown class reference to callback to, if not object?
I don't use the object for anything else in this context.
I searched in the Booklets, but there Object is used all the time for Callback purposes, so I don't know why my custom class GameServerSocket cannot be converted to an object?
thanks for any help or insights!
I'm trying to write a gameserver, the gameserver is using good old TCP sockets for communication.
I have the following structure:
- A GameServerSocket class, which handles new incomming connections (and players etc)
- A NetworkClient class, which is a .. Client, A client that is connected, but not authenticated and thus has no relation to any player at all.
- A Player class, holds player data, and a NetworkClient reference AFTER authentication.
The problem that arises here: When a networkclient is "attached" to the player class, it should perform a CallSub to the Player class, which handles player messages, not to the GameServerSocket, which only handles network data from unauthenticated sockets (thus not players). The class to callback to is in other words dynamic.
Basically, when a new socket is connected, I create a NetworkClient in the GameserverSocket class, and pass an Eventname and a Class to callback to (Me)
B4X:
Public Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim newNetClient As NetworkClient
newNetClient.Initialize(NewSocket, Me, "UnauthenticatedNetClient")
mUnauthenticatedNetclientList.Add(newNetClient)
End If
End Sub
Where the NetworkClient.Initialize code looks like this:
B4X:
Public Sub Initialize( Sock As Socket, ParentToCallback As Object, EventName As String)
mSocket = Sock
mParentToCallback = ParentToCallback
mEventName = EventName
mStream.InitializePrefix(Sock.InputStream, False, Sock.OutputStream, "stream")
End Sub
Notice that ParentToCallback is an Object in this context, as it could be a GameServerSocket object or a Player object that is needs to be used in the CallSub (if authenticated yes or no)
The Callback is used this way in the NetworkClient Class:
B4X:
Private Sub stream_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
CallSub3(mParentToCallback, mEventName & "_NewData", Me, msg)
End Sub
But, I get an error when I try to pass "Me" (from the GameServerSocket object) to the Initialize ParentToCallback parameter, stating:
Compiling generated Java code. Error
B4J line: 21
newNetClient.Initialize(NewSocket, Me, \
src\b4j\example\gameserversocket.java:116: error: incompatible types: gameserversocket cannot be converted to object
_newnetclient._initialize /*String*/ (null,ba,_newsocket,(b4j.example.object)(this),"UnauthenticatedNetClient");
^
1 error
GameServerSocket cannot be converted to Object (while my thinking process was: It shouldn't be converted, it should just hold a "lowest common denominator' reference to it, and I was thinking Object was somekind of baseclass for everything.)
What kind of type should I use for unknown class reference to callback to, if not object?
I don't use the object for anything else in this context.
I searched in the Booklets, but there Object is used all the time for Callback purposes, so I don't know why my custom class GameServerSocket cannot be converted to an object?
thanks for any help or insights!
Last edited: