private void RunThread() { UdpClient listener = new UdpClient(mConfigFile.mRCPort); IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, mConfigFile.mRCPort); listener.Client.ReceiveTimeout = 60; /* * Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); * IPAddress send_to_address = IPAddress.Parse(mConfig.mIPAddress); * IPEndPoint sending_end_point = new IPEndPoint(send_to_address, mConfig.mRCPort); */ bool SenderOpen = false; Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPAddress send_to_address = IPAddress.Parse("127.0.0.1"); IPEndPoint sending_end_point = new IPEndPoint(send_to_address, mConfigFile.mDSPort); PiMessage.RC_Status_Message RCMessage = new RC_Status_Message(); while (true == mRunThread) { mCount++; try { mData = listener.Receive(ref groupEP); // You can repeat this as many times, but you can’t send stuff using this port. PiMessage.Message theMessage = MessageFactory.Build(mData); if (null != theMessage) { switch (theMessage.GetMessageID()) { case (PiMessage.Message.MessageIDs.DS_STATUS): mDSMessage = (DS_Status_Message)theMessage; break; } } if (mLastSequence + 1 != mDSMessage.GetSequence()) { mMessage = "Skipped"; } else { mMessage = "OK"; } mLastSequence = mDSMessage.GetSequence(); if (false == SenderOpen) { send_to_address = groupEP.Address; sending_end_point = new IPEndPoint(send_to_address, mConfigFile.mDSPort); SenderOpen = true; } mRCMessage.SetStatus(PiMessage.RCStatus.STATUS_GOOD); mRCMessage.SetSequence(mDSMessage.GetSequence()); mRCMessage.SetTime((UInt32)DateTime.Now.Subtract(mStartTime).TotalMilliseconds); mData = mRCMessage.GetData(); sending_socket.SendTo(mData, sending_end_point); // Repeat this as many times you want } catch (SocketException e) { //if(0x0000274c != e.ErrorCode) { mMessage = e.Message.ToString(); } } catch (Exception e) { mMessage = e.Message.ToString(); } Thread.Sleep(5); } listener.Close(); }
private void RunThread() { Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPAddress send_to_address = IPAddress.Parse(mConfig.mIPAddress); IPEndPoint sending_end_point = new IPEndPoint(send_to_address, mConfig.mRCPort); UdpClient listener = new UdpClient(mConfig.mDSPort); IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, mConfig.mDSPort); listener.Client.ReceiveTimeout = 30; byte theDouble = 0; while (true == mRunThread) { mCurStatusMessage.SetStatus(mStatus); mCurStatusMessage.SetTime((UInt32)DateTime.Now.Subtract(mStartTime).TotalMilliseconds); mCurStatusMessage.RollSequenceNumber(); mController.SetNumber(1); for (int i = 0; i < MAX_BUTTONS; i++) { mController.SetButton(i, ((1 << i) == (mButtons & (1 << i)))?true:false); } mController.SetAnalog(0, (short)(mLeftX / 256)); mController.SetAnalog(1, (short)(mLeftY / 256)); mController.SetAnalog(2, (short)(mRightX / 256)); mController.SetAnalog(3, (short)(mRightY / 256)); theDouble += 1; byte[] theData = mCurStatusMessage.GetData(); sending_socket.SendTo(theData, sending_end_point); // Repeat this as many times you want theData = mController.GetData(); sending_socket.SendTo(theData, sending_end_point); // Repeat this as many times you want Thread.Sleep(2); try { theData = listener.Receive(ref groupEP); // You can repeat this as many times, but you can’t send stuff using this port. PiMessage.Message theMessage = MessageFactory.Build(theData); if (null != theMessage) { switch (theMessage.GetMessageID()) { case (PiMessage.Message.MessageIDs.DS_STATUS): mDSMessage = (DS_Status_Message)theMessage; break; case (PiMessage.Message.MessageIDs.RC_STATUS): mRCMessage = (RC_Status_Message)theMessage; break; } } if (mCurStatusMessage.GetSequence() != mRCMessage.GetSequence()) { mMessage = "Skipped"; } else { mMessage = "OK"; } } catch (Exception e) { mMessage = e.Message; } mCount++; Thread.Sleep(50); } sending_socket.Close(); }