2011-06-26 32 views
0

我有這個類,我通過RMI發送這個類,並通過kryonet和kryo,我得到的rmi兩個10個對象的數組作爲遠程方法的返回打電話,如果kryonet從服務器到客戶端使用kryonet和kryo回顯,我總共有一個對象55 * 10其中555但RMI爲1196bytes,自定義網絡庫和序列化VS默認序列化和RMI

這些結果是否合理?

*爲什麼這些結果是這樣的?

爲什麼會有這麼多的差異。

這過頂球或其他因素參與幕後,

這些都使得在RMI那麼多總和太多的區別,並指出了我。

而且單個對象的總共55個字節是可以嗎?*。

我只是需要一些確認和專家的眼睛,因爲我有呈現結果,

我會真的很感謝。

這是類我米既使用:

public class TBall { 

private float x, y; // Ball's center (x, y) 
private float speedX, speedY; // Ball's speed per step in x and y 
private float radius; // Ball's radius 
private Color color; // Ball's color 

public boolean collisionDetected = false; 
public static boolean run = false; 

private String name; 

private float nextX, nextY; 
private float nextSpeedX, nextSpeedY; 

public TBall() { 
    super(); 
} 

public TBall(String name1, float x, float y, float radius, float speed, 
     float angleInDegree, Color color) { 
    this.x = x; 
    this.y = y; 
    // Convert velocity from polar to rectangular x and y. 
    this.speedX = speed * (float) Math.cos(Math.toRadians(angleInDegree)); 
    this.speedY = speed * (float) Math.sin(Math.toRadians(angleInDegree)); 
    this.radius = radius; 
    this.color = color; 
    this.name = name1; 
} 

public String getName() { 
    return this.name; 
} 

public float getSpeed() { 
    return (float) Math.sqrt(speedX * speedX + speedY * speedY); 
} 

public float getMoveAngle() { 
    return (float) Math.toDegrees(Math.atan2(speedY, speedX)); 
} 

public float getRadius() { 
    return radius; 
} 

public Color getColor() { 
    return this.color; 
} 

public void setColor(Color col) { 
    this.color = col; 
} 

public float getX() { 
    return x; 
} 

public float getY() { 
    return y; 
} 

public void setX(float f) { 
    x = (int) f; 
} 

public void setY(float f) { 
    y = (int) f; 
} 

public void move() { 
    if (collisionDetected) { 
     // Collision detected, use the values computed. 
     x = nextX; 
     y = nextY; 
     speedX = nextSpeedX; 
     speedY = nextSpeedY; 
    } else { 
     // No collision, move one step and no change in speed. 
     x += speedX; 
     y += speedY; 
    } 
    collisionDetected = false; // Clear the flag for the next step 

    System.out.println("In serializedBall in move."); 
} 

public void collideWith() { 

    float minX = 0 + radius; 
    float minY = 0 + radius; 
    float maxX = 0 + 640 - 1 - radius; 
    float maxY = 0 + 480 - 1 - radius; 

    double gravAmount = 0.9811111f; 
    double gravDir = (90/57.2960285258); 

    // Try moving one full step 
    nextX = x + speedX; 
    nextY = y + speedY; 
    System.out.println("In serializedBall in collision."); 

    // If collision detected. Reflect on the x or/and y axis 
    // and place the ball at the point of impact. 
    if (speedX != 0) { 
     if (nextX > maxX) { // Check maximum-X bound 
      collisionDetected = true; 
      nextSpeedX = -speedX; // Reflect 
      nextSpeedY = speedY; // Same 
      nextX = maxX; 
      nextY = (maxX - x) * speedY/speedX + y; // speedX non-zero 
     } else if (nextX < minX) { // Check minimum-X bound 
      collisionDetected = true; 
      nextSpeedX = -speedX; // Reflect 
      nextSpeedY = speedY; // Same 
      nextX = minX; 
      nextY = (minX - x) * speedY/speedX + y; // speedX non-zero 
     } 
    } 
    // In case the ball runs over both the borders. 
    if (speedY != 0) { 
     if (nextY > maxY) { // Check maximum-Y bound 
      collisionDetected = true; 
      nextSpeedX = speedX; // Same 
      nextSpeedY = -speedY; // Reflect 
      nextY = maxY; 
      nextX = (maxY - y) * speedX/speedY + x; // speedY non-zero 
     } else if (nextY < minY) { // Check minimum-Y bound 
      collisionDetected = true; 
      nextSpeedX = speedX; // Same 
      nextSpeedY = -speedY; // Reflect 
      nextY = minY; 
      nextX = (minY - y) * speedX/speedY + x; // speedY non-zero 
     } 
    } 

    System.out.println("In serializedBall collision."); 
    // speedX += Math.cos(gravDir) * gravAmount; 
    // speedY += Math.sin(gravDir) * gravAmount; 

    System.out.println("In serializedBall in collision."); 
} 

}

感謝。

回答

0

你從哪裏得到'55'?您有:

9浮筒,= 9X4個字節,總共36個字節 1布爾值,序列化爲一個字節,總共1個字節 1字符串,可以是任何長度 1顏色,又包含: 1 INT,序列化爲4個字節 1個浮子,序列化爲4個字節 2浮[]長度3各自,序列化爲24個字節 1彩色空間,而這又包含: 2整數,序列化爲8個字節

總的這至少需要77個字節加上傳輸字符串所需的任何東西。

序列化還會在每個項目前發送類信息,版本控制信息和標籤; RMI也發送方法信息。所有這些都可以輕鬆解釋這種差異。我不知道那些其他軟件包是做什麼的。

+0

感謝您的回覆,這是在數組中發送一個球對象的結果,結果(我使用wireshark查看)是55字節的旅行。當我增加沒有對象時,55乘以數組中沒有對象,即使用kryo序列化庫55 * 10 = 555。使用RMI和默認序列化,數組中的一個對象取= 575字節,取1096取1196,這與第一個數據相比是非常不同的,並表示與數組中的對象無關的某種開銷。那是什麼開銷? –

+0

@static void main它看起來像kryo庫必須做某種壓縮或默認值消除。我已經寫了關於RMI和序列化的開銷。 – EJP