title: UDP Packets Misfiring
body: <p>Trying to stage my question as generically as possible here. Perhaps I'm just thinking out loud.</p>
  <p>My program should be sending one large packet, around 24bytes but instead it would seem that it's firing 6 packets of length: 2,2,4,4,4,8 respectively.</p>
  <p>Transmitted from my Adafruit WICED Feather is:</p>
  <p><a href="https://i.stack.imgur.com/8IMT0.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8IMT0.gif" alt="Arduino/Adafruit Serial Monitor" /></a></p>
  <p>So the packet getting sent by my PC is 24bytes in this case. The handshake should return a packet of roughly the same size. However here's what is getting picked up on the other end:</p>
  <p><a href="https://i.stack.imgur.com/dsICR.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dsICR.gif" alt="rtpMidi Error Log" /></a></p>
  <p>As you can see, the incoming packets are of sizes 2, 2, 4, 4, 4, and then a buffer of 8+ bits.</p>
  <p>My first thought is that there is something going on in the main write function, which is a template:</p>
  <pre><code>template&lt;class UdpClass&gt;
  inline void AppleMidi_Class&lt;UdpClass&gt;::write(UdpClass&amp; udp, AppleMIDI_InvitationAccepted&amp; ia, IPAddress ip, uint16_t port)
  {
     Serial.println(&quot;+++++ Writing Packet&quot;);
     udp.beginPacket(ip, port);
  
        udp.write(ia.signature, sizeof(ia.signature));
        udp.write(ia.command, sizeof(ia.command));
  
        // To appropriate endian conversion
        uint32_t _version = AppleMIDI_Util::toEndian(ia.version);
        uint32_t _initiatorToken = AppleMIDI_Util::toEndian(ia.initiatorToken);
        uint32_t _ssrc = AppleMIDI_Util::toEndian(ia.ssrc);
  
        // write then out
        udp.write((uint8_t*) ((void*) (&amp;_version)), sizeof(_version));
        udp.write((uint8_t*) ((void*) (&amp;_initiatorToken)), sizeof(_initiatorToken));
        udp.write((uint8_t*) ((void*) (&amp;_ssrc)), sizeof(_ssrc));
  
        udp.write((uint8_t*) ia.name, strlen(ia.name) + 1);
  
     udp.endPacket();
     udp.flush();
  }
  </code></pre>
  <p>This template is actually calling the following write function (6 times in fact):</p>
  <pre><code>size_t AdafruitUDP::write(const uint8_t* buffer, size_t size)
  {
    if (_udp_handle == 0 || _sndIP == 0 || _sndPort == 0) {
      Serial.println(&quot;Handle, IP, port == 0 returning&quot;);
      return 0;
    }
    //Serial.printf(&quot;+++++ Handle: %s IP: %s Send Port: %s\n&quot;, _udp_handle, _sndIP, _sndPort);
  
    sdep_cmd_para_t para_arr[] =
    {
        { .len = 4   , .p_value = &amp;_udp_handle },
        { .len = 4   , .p_value = &amp;_sndIP      },
        { .len = 2   , .p_value = &amp;_sndPort    },
        { .len = size, .p_value = buffer       }
    };
    uint8_t para_count = sizeof(para_arr)/sizeof(sdep_cmd_para_t);
  
    VERIFY_RETURN(sdep_n(SDEP_CMD_UDP_WRITE, para_count, para_arr, NULL, NULL), 0);
    return size;
  }
  </code></pre>
  <p>This then calls:</p>
  <pre><code>bool AdafruitSDEP::sdep_n(uint16_t  cmd_id       ,
              uint8_t   para_count   , sdep_cmd_para_t const* para_arr,
              uint16_t* p_result_len , void* p_result)
  {
    _errno = FEATHERLIB-&gt;sdep_execute_n(cmd_id, para_count, para_arr, p_result_len, p_result);
    handle_error(cmd_id);
    return (ERROR_NONE == _errno);
  }
  </code></pre>
  <p>So, I think the next obvious question is: what happens during beginPacket and endPacket. Not much - I didn't see any callbacks either :</p>
  <pre><code>int AdafruitUDP::beginPacket(IPAddress ip, uint16_t port)
  {
      _sndIP   = (uint32_t) ip;
      _sndPort = port;
  
      return 1;
  }
  
  /******************************************************************************/
  /*!
      @brief  Called after writing UDP data to the remote connection.
  */
  /******************************************************************************/
  int AdafruitUDP::endPacket()
  {
    _sndIP   = 0;
    _sndPort = 0;
  
      return 1;
  }
  </code></pre>
  <p>So my real question, before I get to changing someones wonderful library around, is this: does it look as though the header is getting written with each <code>appleMidi.write()</code> function? Is there a way to concatenate those 6 write calls from the appleMidi class?</p>
  <p>I think I'm on the right track here but input is always much appreciated.</p>
  <p>Sincerely,</p>
  <p>-Daydreaming at work</p>