Пример #1
0
            public void Send(byte[] bytes)
            {
                if (Configuration.Network.UdpProtocol == UdpProtocol.COBS)
                {
                    byte[] rawBytes = COBSEncoding.Encode(bytes);
                    byte[] msg      = new byte[rawBytes.Length + 1];

                    Array.Copy(rawBytes, 0, msg, 0, rawBytes.Length);
                    msg[rawBytes.Length] = 0;

                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, Configuration.Network.UdpPort);
                    m_client.Send(msg, msg.Length, endPoint);
                }
                else if (Configuration.Network.UdpProtocol == UdpProtocol.Plain)
                {
                    byte[] controlSequence = new byte[] { 255, 255, 255, 255 };
                    byte[] payloadLength   = BitConverter.GetBytes((ushort)bytes.Length);

                    byte[] msg = new byte[4 + 2 + bytes.Length];

                    Array.Copy(controlSequence, 0, msg, 0, controlSequence.Length);
                    Array.Copy(payloadLength, 0, msg, 4, payloadLength.Length);
                    Array.Copy(bytes, 0, msg, 4 + 2, bytes.Length);

                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, Configuration.Network.UdpPort);
                    m_client.Send(msg, msg.Length, endPoint);
                }
            }
Пример #2
0
            public void Send(List <byte[]> bytes)
            {
                List <byte> final = new List <byte>();

                foreach (byte[] packet in bytes)
                {
                    byte[] rawBytes    = COBSEncoding.Encode(packet);
                    byte[] finalPacket = new byte[rawBytes.Length + 1];

                    Array.Copy(rawBytes, 0, finalPacket, 0, rawBytes.Length);
                    finalPacket[rawBytes.Length] = 0;

                    final.AddRange(finalPacket);
                }

                byte[] msg = final.ToArray();

                IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, Configuration.Network.UdpPort);

                m_client.Send(msg, msg.Length, endPoint);
            }
Пример #3
0
            private void ProcessBuffer()
            {
                if (Configuration.Network.UdpProtocol == UdpProtocol.COBS)
                {
                    if (!m_bufferSynchronized)
                    {
                        // we have not found a zero yet
                        // is there one in the buffer?
                        int index = m_buffer.IndexOf(0);
                        if (index >= 0)
                        {
                            // we have a zero, trim the buffer
                            if (index > 0)
                            {
                                // only if there's stuff up front
                                m_buffer.RemoveRange(0, index + 1);
                            }

                            // now, the buffer is synchronized
                            m_bufferSynchronized = true;
                        }
                    }

                    if (m_bufferSynchronized)
                    {
                        // find the next zero
                        int index = -1;
                        while ((index = m_buffer.IndexOf(0)) > 0)
                        {
                            // extract the packet
                            byte[] rawBytes = m_buffer.GetRange(0, index).ToArray();
                            m_buffer.RemoveRange(0, index + 1);

                            byte[] bytes = COBSEncoding.Decode(rawBytes);

                            PacketReceived?.Invoke(this, bytes);
                        }
                    }
                }
                else
                {
                    // let's find the second-last four FFs first
                    int        firstIndex = -1;
                    List <int> indices    = FindControlSequenceIndices(m_buffer);
                    if (indices.Count == 1)
                    {
                        firstIndex = indices[0];
                    }
                    else if (indices.Count > 1)
                    {
                        firstIndex = indices[indices.Count - 2];
                    }

                    if (firstIndex >= 0)
                    {
                        // strip everything before (not needed anymore)
                        m_buffer.RemoveRange(0, firstIndex);

                        // do we have the length?
                        int payloadLength = -1;
                        if (m_buffer.Count >= 6)
                        {
                            payloadLength = BitConverter.ToUInt16(new byte[] { m_buffer[4], m_buffer[5] }, 0);
                        }

                        // do we have enough bytes
                        if (payloadLength > 0 &&
                            m_buffer.Count >= 2 + 4 + payloadLength)
                        {
                            // get the payload
                            List <byte> payload = m_buffer.GetRange(4 + 2, payloadLength);

                            // check that there's no control sequence in there
                            List <int> subIndices = FindControlSequenceIndices(payload);
                            if (subIndices.Count > 0)
                            {
                                m_buffer.RemoveRange(0, 2 + 4 + subIndices.Last());
                            }
                            else
                            {
                                // remove those bytes (not needed anymore)
                                m_buffer.RemoveRange(0, 2 + 4 + payloadLength);

                                // fire the event
                                PacketReceived?.Invoke(this, payload.ToArray());
                            }
                        }
                    }
                }
            }