예제 #1
0
        private void BeginSending()
        {
            //  If write buffer is empty,  try to read new data from the encoder.
            if (m_writeSize == 0)
            {
                //  First two bytes (sizeof uint16_t) are used to store message
                //  offset in following steps. Note that by passing our buffer to
                //  the get data function we prevent it from returning its own buffer.
                var bf     = new ByteArraySegment(m_outBuffer, sizeof(ushort));
                int bfsz   = m_outBufferSize - sizeof(ushort);
                int offset = -1;
                m_encoder.GetData(ref bf, ref bfsz, ref offset);

                //  If there are no data to write stop polling for output.
                if (bfsz == 0)
                {
                    m_state = State.ActiveSendingIdle;
                    return;
                }

                //  Put offset information in the buffer.
                m_writeSize = bfsz + sizeof(ushort);

                m_outBuffer.PutUnsignedShort(m_options.Endian, offset == -1 ? (ushort)0xffff : (ushort)offset, 0);
            }

            try
            {
                m_socket.Send((byte[])m_outBuffer, m_outBuffer.Offset, m_writeSize, SocketFlags.None);
            }
            catch (SocketException ex)
            {
                NetMQException.Create(ex.SocketErrorCode);
            }
        }