public void StartSimulation() { m_isSimulationRunning = true; m_clientUdp = new UdpClient(); for (int port = CommonParams.CLIENT_UDP_PORT_START; port < CommonParams.CLIENT_UDP_PORT_START + CommonParams.MAX_CLIENTS; ++port) { m_remoteEndPoint = new IPEndPoint(IPAddress.Parse(SERVER_IP), port); m_clientUdp.Client.ReceiveTimeout = 1000; { MsgCheckVersion msg = new MsgCheckVersion(); msg.m_clientVersion = CommonParams.PROTOCOL_VERSION; msg.m_observerType = OBSERVER_TYPE; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } { // receive bytes IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0); byte[] buffer = m_clientUdp.Receive(ref anyIP); PPh.MsgCheckVersionResponse msgReceive = ServerProtocol.QueryMessage<MsgCheckVersionResponse>(buffer); if ( msgReceive != null) { if (msgReceive.m_serverVersion == CommonParams.PROTOCOL_VERSION) { break; } else { // wrong protocol m_remoteEndPoint = null; break; } } } m_remoteEndPoint = null; } if (m_remoteEndPoint != null) { m_clientUdp.Client.Blocking = false; // to enable non-blocking socket // create thread for UDP messages m_simulationThread = new Thread(new ThreadStart(ThreadCycle)); m_simulationThread.IsBackground = true; m_simulationThread.Start(); } else { // server not found } }
void PPhTick() { { MsgGetState msg = new MsgGetState(); byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (GetTimeMs() - m_lastUpdateStateExtTime > 20) // get position/orientation data every n milliseconds { m_lastUpdateStateExtTime = GetTimeMs(); MsgGetStateExt msg = new MsgGetStateExt(); byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (GetTimeMs() - m_lastStatisticRequestTime > STATISTIC_REQUEST_PERIOD) { m_lastStatisticRequestTime = GetTimeMs(); MsgGetStatistics msg = new MsgGetStatistics(); byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isLeft) { MsgRotateLeft msg = new MsgRotateLeft(); msg.m_value = 4; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isRight) { MsgRotateRight msg = new MsgRotateRight(); msg.m_value = 4; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isUp) { MsgRotateDown msg = new MsgRotateDown(); msg.m_value = 4; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isDown) { MsgRotateUp msg = new MsgRotateUp(); msg.m_value = 4; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isForward) { MsgMoveForward msg = new MsgMoveForward(); msg.m_value = 16; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } if (m_isBackward) { MsgMoveBackward msg = new MsgMoveBackward(); msg.m_value = 16; byte[] buffer = msg.GetBuffer(); m_clientUdp.Send(buffer, buffer.Length, m_remoteEndPoint); } // receive bytes IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0); while (m_clientUdp.Available > 0) { byte[] buffer = m_clientUdp.Receive(ref anyIP); if (buffer.Length > 0) { switch ((MsgType)buffer[0]) { case MsgType.GetStateResponse: { MsgGetStateResponse msg = ServerProtocol.QueryMessage<MsgGetStateResponse>(buffer); m_timeOfTheUniverse = msg.m_time; } break; case MsgType.GetStateExtResponse: { MsgGetStateExtResponse msg = ServerProtocol.QueryMessage<MsgGetStateExtResponse>(buffer); lock (m_observerStateParamsMutex) { m_latitude = msg.m_latitude; m_longitude = msg.m_longitude; m_position = msg.m_pos; m_movingProgress = msg.m_movingProgress; if (m_eatenCrumbNum < msg.m_eatenCrumbNum) { m_eatenCrumbNum = msg.m_eatenCrumbNum; m_eatenCrumbPos = msg.m_eatenCrumbPos; m_isEatenCrumb = true; } } } break; case MsgType.SendPhoton: { MsgSendPhoton msg = ServerProtocol.QueryMessage<MsgSendPhoton>(buffer); // receive photons back // revert Y-coordinate because of texture format // photon (x,y) placed to [CommonParams.OBSERVER_EYE_SIZE - y -1][x] for simple copy to texture lock (m_eyeTextureMutex) { m_eyeColorArray[CommonParams.OBSERVER_EYE_SIZE - msg.m_posY - 1, msg.m_posX] = msg.m_color; m_eyeUpdateTimeArray[CommonParams.OBSERVER_EYE_SIZE - msg.m_posY - 1, msg.m_posX] = m_timeOfTheUniverse; } } break; case MsgType.GetStatisticsResponse: { MsgGetStatisticsResponse msg = ServerProtocol.QueryMessage<MsgGetStatisticsResponse>(buffer); lock (m_serverStatisticsMutex) { m_quantumOfTimePerSecond = msg.m_fps; m_tickTimeMusAverageObserverThread = msg.m_observerThreadTickTime; m_tickTimeMusAverageUniverseThreadsMin = msg.m_universeThreadMinTickTime; m_tickTimeMusAverageUniverseThreadsMax = msg.m_universeThreadMaxTickTime; m_universeThreadsNum = msg.m_universeThreadsCount; m_clientServerPerformanceRatio = msg.m_clientServerPerformanceRatio; m_serverClientPerformanceRatio = msg.m_serverClientPerformanceRatio; } } break; } } } }