Skip to content

Commit

Permalink
Implement issue TehGimp#126, Slight fix to handling the time skew mes…
Browse files Browse the repository at this point in the history
…sage.
  • Loading branch information
godarklight committed Feb 12, 2014
1 parent 7d95942 commit a470738
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
9 changes: 5 additions & 4 deletions KMPClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,11 +796,12 @@ static void handleMessage(KMPCommon.ServerMessageID id, byte[] data)
{
String server_version = encoder.GetString(data, 8, server_version_length);
clientID = KMPCommon.intFromBytes(data, 8 + server_version_length);
gameManager.gameMode = KMPCommon.intFromBytes(data, 12 + server_version_length);
int kmpModControl_length = KMPCommon.intFromBytes(data, 16 + server_version_length);
gameManager.gameMode = KMPCommon.intFromBytes(data, 12 + server_version_length);
gameManager.numberOfShips = KMPCommon.intFromBytes(data, 16 + server_version_length);
int kmpModControl_length = KMPCommon.intFromBytes(data, 20 + server_version_length);
kmpModControl_bytes = new byte[kmpModControl_length];
Array.Copy(data, 20 + server_version_length, kmpModControl_bytes, 0, kmpModControl_length);
gameManager.checkAllModFiles = Convert.ToBoolean(data[20+server_version_length+kmpModControl_length]);
Array.Copy(data, 24 + server_version_length, kmpModControl_bytes, 0, kmpModControl_length);
gameManager.checkAllModFiles = Convert.ToBoolean(data[24 + server_version_length+kmpModControl_length]);
SetMessage("Handshake received. Server version: " + server_version);
}
}
Expand Down
2 changes: 1 addition & 1 deletion KMPCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static String PROGRAM_VERSION
}

public const Int32 FILE_FORMAT_VERSION = 10000;
public const Int32 NET_PROTOCOL_VERSION = 10014;
public const Int32 NET_PROTOCOL_VERSION = 10015;
public const int MSG_HEADER_LENGTH = 8;
public const int MAX_MESSAGE_SIZE = 1024 * 1024; //Enough room for a max-size craft file
public const int MESSAGE_COMPRESSION_THRESHOLD = 4096;
Expand Down
25 changes: 19 additions & 6 deletions KMPManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void HandleHash(System.Object state)

public static object interopInQueueLock = new object();

public int numberOfShips = 0;
public int gameMode = 0; //0=Sandbox, 1=Career
public bool gameCheatsEnabled = false; //Allow built-in KSP cheats
public bool gameArrr = false; //Allow private vessels to be taken if other user can successfully dock manually
Expand Down Expand Up @@ -192,6 +193,7 @@ public void HandleHash(System.Object state)
private const int SYNC_TIME_VALID_COUNT = 4; //Number of SYNC_TIME's to receive until time is valid.
private const int MAX_TIME_SYNC_HISTORY = 10; //The last 10 SYNC_TIME's are used for the offset filter.
private ScreenMessage skewMessage;
private ScreenMessage vesselLoadedMessage;

private Queue<KMPVesselUpdate> vesselUpdateQueue = new Queue<KMPVesselUpdate>();
private Queue<KMPVesselUpdate> newVesselUpdateQueue = new Queue<KMPVesselUpdate>();
Expand Down Expand Up @@ -266,6 +268,7 @@ public void HandleHash(System.Object state)
private Vessel lastEVAVessel = null;
private bool showServerSync = false;
private bool inGameSyncing = false;
private int vesselUpdatesLoaded = 0;

private bool configRead = false;

Expand Down Expand Up @@ -367,7 +370,16 @@ public void updateStep()
if (syncing)
{
ScreenMessages.PostScreenMessage("Synchronizing universe, please wait...",1f,ScreenMessageStyle.UPPER_CENTER);
ScreenMessages.PostScreenMessage("Loaded vessels: " + FlightGlobals.Vessels.Count,0.04f,ScreenMessageStyle.UPPER_RIGHT);
if (vesselLoadedMessage != null) {
vesselLoadedMessage.duration = 0f;
}
if (!inGameSyncing) {
if (numberOfShips != 0) {
vesselLoadedMessage = ScreenMessages.PostScreenMessage("Loaded vessels: " + vesselUpdatesLoaded + "/" + numberOfShips + " (" + (vesselUpdatesLoaded * 100 / numberOfShips) + "%)",1f,ScreenMessageStyle.UPPER_RIGHT);
}
} else {
vesselLoadedMessage = ScreenMessages.PostScreenMessage("Loaded vessels: " + FlightGlobals.Vessels.Count,1f,ScreenMessageStyle.UPPER_RIGHT);
}
}

if (!isInFlight && HighLogic.LoadedScene == GameScenes.TRACKSTATION)
Expand Down Expand Up @@ -1653,6 +1665,7 @@ private void handleUpdate(object obj)

if (obj is KMPVesselUpdate)
{
vesselUpdatesLoaded++;
handleVesselUpdate((KMPVesselUpdate)obj);
}
else if (obj is String[])
Expand Down Expand Up @@ -3551,6 +3564,9 @@ private void SkewTime ()
double currentErrorS = Math.Round (currentError, 2);

if (Math.Abs (currentError) > 5) {
if (skewMessage != null) {
skewMessage.duration = 0f;
}
skewMessage = ScreenMessages.PostScreenMessage ("Time skew fast, error: " + currentErrorS + " seconds.", 4f, ScreenMessageStyle.UPPER_RIGHT);
if (isInFlight) {
krakensBaneWarp(skewTargetTick + timeFromLastSyncSecondsAdjusted);
Expand Down Expand Up @@ -3590,11 +3606,8 @@ private void SkewTime ()


if (isSkewingTime) {
try {
skewMessage.duration = 0;
}
catch (Exception e) {
Log.Debug (e.ToString ());
if (skewMessage != null) {
skewMessage.duration = 0f;
}
skewMessage = ScreenMessages.PostScreenMessage("Time skew error: " + currentErrorMs + "ms.", 1f, ScreenMessageStyle.UPPER_RIGHT);
}
Expand Down
46 changes: 39 additions & 7 deletions KMPServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,35 @@ private void countShipsServerCommand(bool bList = false)

}

private int countShipsInDatabase()
{
var universeDB = KMPServer.Server.universeDB;
if (settings.useMySQL) {
universeDB = new MySqlConnection(settings.mySQLConnString);
universeDB.Open();
}
DbCommand cmd = universeDB.CreateCommand();
String sql = "SELECT vu.UpdateMessage, v.ProtoVessel, v.Guid" +
" FROM kmpVesselUpdate vu" +
" INNER JOIN kmpVessel v ON v.Guid = vu.Guid AND v.Destroyed != 1" +
" INNER JOIN kmpSubspace s ON s.ID = vu.Subspace" +
" INNER JOIN" +
" (SELECT vu.Guid, MAX(s.LastTick) AS LastTick" +
" FROM kmpVesselUpdate vu" +
" INNER JOIN kmpSubspace s ON s.ID = vu.Subspace" +
" GROUP BY vu.Guid) t ON t.Guid = vu.Guid AND t.LastTick = s.LastTick;";
cmd.CommandText = sql;
DbDataReader reader = cmd.ExecuteReader();
int count = 0;
while (reader.Read())
{
count++;
}
reader.Dispose();
if (settings.useMySQL) universeDB.Close();
return count;
}

private void listShipsServerCommand()
{
countShipsServerCommand(true);
Expand Down Expand Up @@ -2930,7 +2959,7 @@ private void sendHandshakeMessage(Client cl)

byte[] version_bytes = encoder.GetBytes(KMPCommon.PROGRAM_VERSION);

byte[] data_bytes = new byte[version_bytes.Length + 20 + kmpModControl.Length + 1];
byte[] data_bytes = new byte[version_bytes.Length + 24 + kmpModControl.Length + 1];

//Write net protocol version
KMPCommon.intToBytes(KMPCommon.NET_PROTOCOL_VERSION).CopyTo(data_bytes, 0);
Expand All @@ -2943,15 +2972,18 @@ private void sendHandshakeMessage(Client cl)

//Write client ID
KMPCommon.intToBytes(cl.clientIndex).CopyTo(data_bytes, 8 + version_bytes.Length);
//Write gameMode

//Write gameMode
KMPCommon.intToBytes(settings.gameMode).CopyTo(data_bytes, 12 + version_bytes.Length);

KMPCommon.intToBytes(kmpModControl.Length).CopyTo(data_bytes, 16 + version_bytes.Length);
kmpModControl.CopyTo(data_bytes, 20 + version_bytes.Length);

//Write number of ships in initial sync
KMPCommon.intToBytes(countShipsInDatabase()).CopyTo(data_bytes, 16 + version_bytes.Length);

KMPCommon.intToBytes(kmpModControl.Length).CopyTo(data_bytes, 20 + version_bytes.Length);
kmpModControl.CopyTo(data_bytes, 24 + version_bytes.Length);

//Add checkAllModFiles setting to the handshake message
data_bytes[20 + version_bytes.Length + kmpModControl.Length] = Convert.ToByte(settings.checkAllModFiles);
data_bytes[24 + version_bytes.Length + kmpModControl.Length] = Convert.ToByte(settings.checkAllModFiles);

cl.queueOutgoingMessage(KMPCommon.ServerMessageID.HANDSHAKE, data_bytes);
}
Expand Down

0 comments on commit a470738

Please sign in to comment.