this is my server i wrote....can I have some suggestions on optmizing this tcp server?
I see that some have used banks....can you explain the need and benefits of banks verses strings? Probably takes more bytes to send for string
anyway here is code:
the client sends 1,x,y,z,rx,ry,rz,animseq,animframe,name$ everytime a move is made
to synch up animation and position/rotation
here is the server
I see that some have used banks....can you explain the need and benefits of banks verses strings? Probably takes more bytes to send for string
anyway here is code:
the client sends 1,x,y,z,rx,ry,rz,animseq,animframe,name$ everytime a move is made
to synch up animation and position/rotation
here is the server
Type netplayer Field netid# Field name$ Field port$ Field ip$ Field stream Field x#,y#,z#,rx#,ry#,rz# Field animframe# Field AniSeq# Field updated# End Type Global currentid# Dim splitdata$(10) Graphics3D 800,600,0,2 AppTitle "Server 1.0 beta" server=CreateTCPServer(8080) Print "Server Again 1.0 beta" If server<>0 Print "Server Accepting Connections..." Print "press escape to close" EndIf Global currentcount=0 While Not KeyDown(1) stream = AcceptTCPStream(server) If stream currentcount=currentcount+1 WriteLine(stream,currentcount) Print "New Net Player Joined : Player ID:"+currentcount n.netplayer=New netplayer n\netid=currentcount n\stream=stream n\ip$=DottedIP$(TCPStreamIP(stream)) n\port$=TCPStreamPort(stream) n\x=0:n\y=0:n\z=0:n\rx=0:n\ry=0:n\rz=0 For existing.netplayer=Each netplayer ; send all player locations - animation positions for existing players If existing\netid<>n\netid WriteLine(n\stream,1+","+existing\netid+","+existing\x+","+existing\y+","+existing\z+","+existing\rx+","+existing\ry+","+existing\rz+","+existing\aniseq+","+existing\animframe+","+existing\name$) ; notify new player of all existing players WriteLine(existing\stream,5+","+n\netid+","+n\x+","+n\y+","+n\z+","+n\rx+","+n\ry+","+n\rz+","+n\aniseq+","+n\animframe+","+n\name$) ; notify existing players of new player EndIf Next EndIf For n.netplayer=Each netplayer If Eof(n\stream)=True currentcount=currentcount-1 Print "player " + n\name$+ " has disconnected from ip address:" + n\ip$ + " and port " + n\port$ + " leaving " + currentcount + " clients connected." For dd.netplayer=Each netplayer WriteLine(dd\stream,6+","+n\netid+","+n\x+","+n\y+","+n\z+","+n\rx+","+n\ry+","+n\rz+","+n\aniseq+","+n\animframe+","+n\name$);notify all players that this player has Left Next Delete n Else If ReadAvail(n\stream)>2 Then message$ = ReadLine$(n\stream) If Instr(message$,",") d=split(message$) Select splitdata$(1) Case 1 ; update coordinates/rotation n\updated=1 n\x=Float(splitdata$(3)):n\y=Float(splitdata$(4)):n\z=Float(splitdata$(5)):n\rx=Float(splitdata$(6)):n\ry=Float(splitdata$(7)):n\rz=Float(splitdata$(8)) n\Aniseq=Int(splitdata$(9)):n\animframe=Int(splitdata$(10)):n\name$=splitdata$(11) ; Print 1+","+n\netid+","+n\x+","+n\y+","+n\z+","+n\rx+","+n\ry+","+n\rz+","+n\aniseq+","+n\animframe+","+n\name$ Case 2 ; send private message Print "sending a private" ; 2,chris,msg For p.netplayer=Each netplayer If p\name$=splitdata$(2) WriteLine(p\stream,"7,"+splitdata$(3)) EndIf Next End Select EndIf EndIf EndIf Next For n.netplayer=Each netplayer If n\updated=1 For q.netplayer=Each netplayer WriteLine(q\stream,1+","+n\netid+","+n\x+","+n\y+","+n\z+","+n\rx+","+n\ry+","+n\rz+","+n\aniseq+","+n\animframe+","+n\name$) Next n\updated=0 EndIf Next Wend CloseTCPServer(server) Function Split(DataToSplit$) ;Declare our variables Local splitcounter = 0, pntr = -1,chris=1, pntr2 = 0, counter = 0 ; Continue to check for splits until we reach the end of the string Local a=0 While pntr <> 0 pntr = Instr(datatosplit$, ",", chris) ; If we found a split then add 1 to the counter If pntr > 0 Then splitcounter = splitcounter + 1 chris=pntr+1 a=a+1 Wend ; Now we have the amount of items (-1) in the string so we can dimension our array Dim SplitData$(splitcounter + 1) ; the first item in the string is handled differently than the rest since there is no comma preceding it pntr = 0:pntr2 = Instr(DataToSplit$, ",", 1) SplitData$(1) = Left$(DataToSplit$, pntr2 - 1) ; After splitting off the first item we move on to the rest of the string. For counter = 2 To splitcounter ; These 2 pointers keep track of the beginning and ending of the item we are looking for in the string. pntr = pntr2 + 1 pntr2 = Instr(DataToSplit$, ",", pntr) ; Now that we have the items location we can pull it from the main string and insert it in to our return array SplitData$(counter) = Mid$(DataToSplit$, pntr, (pntr2 - pntr)) Next ; Once we have done every instance of the comma delimiters we are left with the very end of the string ; This also is handled differently than the rest of the string since there is no comma after it. SplitData$(counter) = Right$(DataToSplit$, Len(DataToSplit$) - pntr2) ; Now we can return the # of items located in the string back to the calling program. Return (splitcounter + 1) End Function