TCP + UDP, CLIENT + SERVER model

Miscellaneous Forums/General Discussion/TCP + UDP, CLIENT + SERVER model

Ugh, this one has had some thinking into it. It is still in concept. May i introduce to you, my Simple Server Packet Protocol. It is designed for MMOG.
When it is done, i will be useing this for my MMORPG Fantasaar. Well, lets say i then have a decent base to build on.

Any comment and suggestions are welcome. If many like this idea i would suggest we make it an OpenSource.

For now i have only the client model:
SuperStrict

''Simple Server Packet Protocol (SSPP)  - Revision 1
'Author: Patrick Weijtenburg
'Last update: 31-01-2008 23:07

Type TClient
	Field name:String
	Field id:Int 'read only, set by server
	Field host:String
	Field port:Int
	Field channels:TList = CreateList() 
	Field channel:Int
	Field packetout:TBank
	Field packetin:TBank
	Field pointer:Int 'used for reading packets
	
	Method SetPlayerName(name:String) 
		Self.name = name
		'if is connected, send new name
	End Method
	
	Method GetPlayerName() 
		Return Self.name
	End Method
	
	Method GetPlayerID() 
		Return Self.id
	End Method
	
	Method Connect(host:String, port:Int, timeout:Int = 30) 
		Self.host = host
		Self.port = port
		'connection code, if timeout OnConnectTimeout()
	End Method
	
	Method OnConnectTimeout() 
	End Method
	
	Method OnAuthorize(key:String) 
		'parse key from server and send back to authorize
		Authorize(key) 
	End Method
	
	Method Authorize(key:String) 
		'return key to server
	End Method
	
	Method IsConnected() 
		'check if is connected or not
	End Method
	
	Method Disconnect() 
		'close connection
	End Method
	
	Method ChannelSignOn(channel:String) 
		Self.channels.AddLast(channel) 
		Self.channel = Self.channels.Count() - 1
		'signon at "channel"
	End Method
	
	Method ChannelSignOff() 
		'signoff at "GetChannelName()"
	End Method
	
	Method GetChannelID() 
		Return Self.channel
	End Method
	
	Method GetChannelName() 
		Return Self.channels.ValueAtIndex(Self.channel) 
	End Method
		
	Method SelectChannelByID(id:Int) 
		If Self.channels.ValueAtIndex(id) 
			Self.channel = id
			Return True
		Else
			Return False
		End If
	End Method
	
	Method SelectChannelByName(channel:String) 
		Local id:Int = 0
		For c = EachIn Self.channels
			If c = channel
				Self.channel = id
				Return True
			End If
			id:+1
		Next
		Return False
	End Method
	
	Method PacketNew(OnSubchannel:Int = 0) 
		Self.packetout = CreateBank(1) 
		Self.packetout.PokeByte(0, Byte(OnSubchannel)) 'set packet identifier for serverside parsing. a.k.a. subchannel
	End Method
	
	Method PacketAddByte(b:Byte) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(b)) 
		Self.packetout.PokeByte(size - 1, b) 
	End Method
	
	Method PacketAddShort(s:Short) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(s)) 
		Self.packetout.PokeShort(size - 1, s) 
	End Method
	
	Method PacketAddInt(i:Int) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(i)) 
		Self.packetout.PokeInt(size - 1, i) 
	End Method
	
	Method PacketAddLong(l:Long) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(l)) 
		Self.packetout.PokeLong(size - 1, l) 
	End Method
	
	Method PacketAddFloat(f:Float) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(f)) 
		Self.packetout.PokeFloat(size - 1, f) 
	End Method
	
	Method PacketAddDouble(d:Double) 
		Local size:Int = Self.packetout.Size() 
		Self.packetout.Resize(size + SizeOf(d)) 
		Self.packetout.PokeDouble(size - 1, d) 
	End Method
	
	Method PacketAddString(text:String) 
		Local size:Int = Self.packetout.Size() 
		Local s:Short = SizeOf(text) 
		Local x:Int
		text = Left(text, 65535) 
		Self.packetout.Resize(size + SizeOf(s) + SizeOf(text)) 
		Self.packetout.PokeShort(size - 1, s) 
		For x = 0 To Len(text) 
			Self.packetout.PokeByte(size + 1 + x, Byte(Mid(text, x))) 
		Next
	End Method
	
	rem
	Method PacketAddBinary(data:?) 
		Local size:Int = packetout.Size() 
		'add filesize
		'add file to packet
	End Method
	endrem
	
	Method PacketSendTCP() 
		'send Self.packetout over TCP on active channel to server
	End Method
	
	Method PacketSendUDP() 
		'send Self.packetout over UDP on active channel to server
	End Method
		
	Method OnPacketReceived(packet:TBank) 
		Self.packetin = packet
		Self.pointer = 0
	End Method
	
	Method PacketReadByte() 
		Local b:Byte = Self.packetin.PeekByte(Self.pointer) 
		Self.pointer:+SizeOf(b) 
		return b 
	End Method
	
	Method PacketReadShort() 
		Local s:Short = Self.packetin.PeekShort(Self.pointer) 
		Self.pointer:+SizeOf(s) 
		Return s
	End Method
	
	Method PacketReadInt() 
		Local i:Int = Self.packetin.PeekInt(Self.pointer) 
		Self.pointer:+SizeOf(i) 
		Return i
	End Method
	
	Method PacketReadLong() 
		Local l:Long = Self.packetin.PeekLong(Self.pointer) 
		Self.pointer:+SizeOf(l) 
		Return l
	End Method
	
	Method PacketReadFloat() 
		Local f:Float = Self.packetin.PeekFloat(Self.pointer) 
		Self.pointer:+SizeOf(f) 
		Return f
	End Method
	
	Method PacketReadDouble() 
		Local d:Double = Self.packetin.PeekDouble(Self.pointer) 
		Self.pointer:+SizeOf(d) 
		Return d
	End Method
	
	Method PacketReadString() 
		Local text:String
		Local s:Short = Self.packetin.PeekShort(Self.pointer) 
		Local size:Int = Int(s) 
		Self.pointer:+SizeOf(s) 
		Local x:Int
		For x = 0 To size
			text = text + Chr(Self.packetin.PeekByte(Self.pointer + x)) 
		Next
		Self.pointer:+size
		Return text
	End Method
	
	rem
	Method PacketReadBinary() 
		Local data:?
		'read filesize
		'read file from packet
		'return file
	End Method
	endrem

EndType


!! attention: in concept !! ~contains psuedo setup