beetlex-io / mqtt

high performance .NET library for MQTT based communication support v3.x and v5.0 protocols
Apache License 2.0
15 stars 2 forks source link

MQTTClientPacket导致空异常 #1

Open yintao666 opened 1 year ago

yintao666 commented 1 year ago

//MQTTParse中 Write函对ISession参数做了一些处理,但是MQTTClientPacke中调用直接传空,导致发送时异常,请问MQTTClientPacke中如何才能拿到ISession解决此报错呢 public void Write(MQTTMessage msg, Stream stream, ISession session) { IServer server = session.Server; if (server.EnableLog(LogType.Debug)) { server.Log(LogType.Debug, session, $"write mqtt message {msg}"); }

        MemoryStream protocolStream = GetProtocolStream();
        int num = 0;
        num |= (int)msg.Type << 4;
        if (msg.DUP)
        {
            num |= 8;
        }

        num |= (int)msg.QoS << 1;
        if (msg.Retain)
        {
            num |= 1;
        }

        stream.WriteByte((byte)num);
        msg.Write(protocolStream, session);
        if (server.EnableLog(LogType.Debug))
        {
            server.Log(LogType.Debug, session, $"write mqtt message body size {protocolStream.Length}");
        }

        mInt7BitHandler.Write(stream, (int)protocolStream.Length);
        protocolStream.Position = 0L;
        protocolStream.CopyTo(stream);
        if (server.EnableLog(LogType.Debug))
        {
            server.Log(LogType.Debug, session, "write mqtt message success");
        }
    }
}

MQTTClientPacket中加密部分中: public void Encode(object data, IClient client, Stream stream) { MQTTMessage mQTTMessage = data as MQTTMessage; if (mQTTMessage != null) { mMQTTParse.Write(mQTTMessage, stream.ToPipeStream(), null); return; }

        throw new BXException("Invalid message type!");
    }
beetlex-io commented 1 year ago

client是不存在session,你可以在MQTTParse添加一个iclient的参数,服务端传null就好。