private void OnServerTickPacket(ServerTick packet) { //var now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; // If this is our first packet, just set the frame id if (FrameID == NetworkConstants.BeforeStartOfGameFrameId) { FrameID = packet.TickCount; } // We've had at least two ticks, so estimate a new server frame time //else //{ // // the number of ticks we need to get through, is the number elapsed on the server // // plus the difference between our frameId and the server frame id, in order to speed // // or slow down if we're not quite in sync // int ticksUntilNextPacket = packet.NumTicksSinceLastSend + (packet.TickCount - FrameID); // // frame time = elapsed time / number of frames // estimatedServerFrameTime = ((float)(now - lastServerTickPacketArrivalTime)) / ticksUntilNextPacket; // Console.WriteLine("Got server tick: server frameId:{0}, our frameId:{1}, ticksUntilNext:{2}, serverFrameTime:{3}", packet.TickCount, FrameID, ticksUntilNextPacket, estimatedServerFrameTime); //} lastServerTickFrameId = packet.TickCount; //nextClientTick = now + (int)estimatedServerFrameTime; //lastServerTickPacketArrivalTime = now; //Console.WriteLine("Server tick: {0}", packet.TickCount); }
// Shut down this server private void Server_Shutdown() { ServerTick.Stop(); boxServerClose.Enabled = false; if (HostServer != null) { HostServer.Shutdown(); } }
public void StartTimer(TimerTick timerTickCallback, ServerTick serverUpdateCallback) { TimerTickCallback = timerTickCallback; ServerUpdateCallback = serverUpdateCallback; // Start interval for sending worklogs to Jira serverUpdateTimer.Tick += OnServerUpdateTick; serverUpdateTimer.Start(); // Get the current time, and start the timer timeSpanLog = DateTime.Now; timer.Tick += OnTimerTick; timer.Start(); }
/// <summary> /// This starts the server when we click File->New Server->Host New Server. /// </summary> private void hostNewGameToolStripMenuItem_Click(object sender, EventArgs e) { Server_Shutdown(); HostServer = new Server(PlayerSettings.Settings.Host_ServerName, PlayerSettings.Settings.Host_ServerPassword); HostServer.Init(); Debug.Log(string.Format("Opened new server {0}", HostServer.ServerName)); MessageBox.Show(NetworkTools.GetLocalIPAddress()); Connect(string.Empty, PlayerSettings.Settings.Host_ServerPassword); //ClientSendMessage(BoggleInstructions.SET_NAME, PlayerSettings.Settings.Host_ServerPassword); StartServer(); //ConnectPlayer(); ServerTick.Start(); Text = string.Format("NET Boggle on {0}", HostServer.ServerName); }
void SendFrameTickToAllConnectedClients() { long elapsed = frameTickTimer.ElapsedMilliseconds; if (elapsed < frameClampTime) { return; } int diffTicks = (int)((elapsed - snapShotFrameTime) / frameClampTime); snapShotFrameTime = elapsed; frameId += diffTicks - 1; ServerTick packet = (ServerTick)IntrepidSerialize.TakeFromPool(PacketType.ServerTick); packet.TickCount = frameId; packet.NumTicksSinceLastSend = (short)diffTicks; Send(packet); }
private static void RaiseServerTick(object sender, UInt64 tick) { ServerTick?.Invoke(sender, tick); }
public void MainLoop(float loopTime = 1f / 10) { IsRunning = true; var msLoopTime = (int)(loopTime * 1000f); EndPoint remoteEp = new IPEndPoint(0, 0); var watch = Stopwatch.StartNew(); while (IsRunning) { Time = watch.ElapsedMilliseconds; // first receive packets var packetIndex = 0; while (packetIndex < packetsPerLoop && socket.Available > 0) { var size = socket.ReceiveFrom(recvBuffer, ref remoteEp); Logs.Log($"[{Time}] Received message from '{remoteEp}', size: '{size}'."); Packet packet = null; try { packet = new Packet(this, size, recvBuffer, (IPEndPoint)remoteEp, recvStream, recvReader, recvWriter); packet.ParseHeaderFromData(); } catch (Exception e) { Logs.LogError($"Error while parsing packet from '{remoteEp}', with size of '{size}':\n{e}"); continue; } finally { ServerPacketReceive?.Invoke(this, packet); } packetIndex++; } // then send packets (do we need to send only a fixed number?) foreach (var packet in packetsToSend) { if (packet.Broadcast) { foreach (var pair in clients) { if (!pair.Key.Equals(packet.RemoteEp)) { socket.SendTo(packet.Data, 0, packet.PacketSize, SocketFlags.None, pair.Key); } } } else { socket.SendTo(packet.Data, 0, packet.PacketSize, SocketFlags.None, packet.RemoteEp); } } packetsToSend.Clear(); ServerTick?.Invoke(this); DeltaTime = watch.ElapsedMilliseconds - Time; Thread.Sleep(Math.Max((int)(msLoopTime - DeltaTime), 0)); } socket.Close(); }