私はマルチプレイヤーゲームを作成しています。マルチプレイヤーゲームは、各クライアント用に新しいスレッドを作成し、入力ストリームと出力ストリームを介して文字列のソケットを待ち受けます。クライアント側では、メインのゲームスレッドとは別のスレッドもありますが、これはサーバーに2回の文字列を1秒間に60回送信して、何を起こそうとしているかのキーになります。クライアント側で「一般的な更新」(コードRP)を要求すると、サーバーはエンティティ配列のサイズをOKにして、ストリームが再び準備完了になるまでに非常に長い遅延があるようです。なぜ私のin.ready()は長時間かかるのですか?
これは、デバッグのための多くのテストの後で、これが問題の領域のコードであることを確かめています。サーバー側では
: サーバーが未満を取ります。クライアント側で
if(inputLine.equals("RP")){ //this is the main update
timetoupdate = System.currentTimeMillis(); //start timer
ArrayList<Player> currentplayers =world.getPlayers(); //get the list of players
out.println(currentplayers.size()); //send the size
for(int pl = 0; pl<currentplayers.size();pl++){ //loop through
Player player = currentplayers.get(pl); //get the player object
out.println(""+pl);
out.println(player.getUsername());
out.println(player.getType());
out.println(player.getXloc());
out.println(player.getYloc());
out.println(player.getXvel());
out.println(player.getYvel());
out.println(player.getPressing());
out.println(player.getLookingxloc());
out.println(player.getLookingyloc());
out.println(player.getTeam());
out.flush();
/*
id
username
class(type)
xloc
yloc
lxloc
lyloc
*/
// Just gaveout all info on player.getUsername()
}
System.out.println("Time this ("+inputLine+") part takes:"+(System.currentTimeMillis()-timetoupdate)); //stop timer
out.println("RP"); //tell the server we are generally updating
String playercountstring = in.readLine(); //get the array size
int playercount = Integer.valueOf(playercountstring); //convert to int
for (int i = 0; i < playercount; i++) {//loop through all players
long timetoupdate = System.currentTimeMillis(); //start timer
while(!in.ready()){}//wait for input to become ready
System.out.println("time this (to ready) part takes:"+(System.currentTimeMillis()-timetoupdate));//stop timer
String toprocess = in.readLine(); //get id of player
... and continues to get all info on this player
私は、問題のある領域が完了するまでどのくらいのタイミング午前に気づくでしょうとして1ミリ秒ですが、クライアントの1つの領域が200ミリ秒かかる、特に前の行が配列のサイズを集めるのは1ミリ秒もかかりません。私は本当に問題が何であるかについてここで迷っています。私は必ずしも解決策が必要なわけではありません。ありがとう!
出力をフラッシングしていて、ソケットにTCP_NODELAYを設定していますか? – zapl
サーバーコードのように、出力をフラッシュします。ソケットは確かにTCP_NODELAYに設定されています – thetrueprime
両側にはアウトバウンドTCPバッファがあるので、どこにでも設定する必要があります。あなたが見ている200msの間、 "RP"をバッファリングしているクライアントもいるかもしれません。 – zapl