コード例 #1
0
ファイル: PgmSender.cs プロジェクト: zad15c/netmq
        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.
                ushort offset     = 0xffff;
                var    buffer     = new ByteArraySegment(m_outBuffer, sizeof(ushort));
                int    bufferSize = m_outBufferSize - sizeof(ushort);

                int bytes = m_encoder.Encode(ref buffer, bufferSize);
                while (bytes < bufferSize)
                {
                    if (!m_moreFlag && offset == 0xffff)
                    {
                        offset = (ushort)bytes;
                    }
                    Msg msg = new Msg();
                    if (m_session.PullMsg(ref msg) != PullMsgResult.Ok)
                    {
                        break;
                    }
                    m_moreFlag = msg.HasMore;
                    m_encoder.LoadMsg(ref msg);
                    buffer = buffer + bytes;
                    var n = m_encoder.Encode(ref buffer, bufferSize - bytes);
                    bytes += n;
                }

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

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

                m_outBuffer.PutUnsignedShort(m_options.Endian, offset, 0);
            }

            try
            {
                m_socket.Send((byte[])m_outBuffer, m_outBuffer.Offset, m_writeSize, SocketFlags.None);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.ConnectionReset)
                {
                    Error();
                }
                else
                {
                    throw NetMQException.Create(ex.SocketErrorCode, ex);
                }
            }
        }