locked
Custom IBuffer implementations RRS feed

  • Question

  • Hi,

    I want to implement a custom IBuffer to use with ReadAsync/WriteAsync operations, the IBuffer interface just expose Capacity and Length properties, with just these two properties is clear that the IBuffer cannot access to the underlying memory needed for read/write.

    Seems to me that you move a compile time problem to run time, if an operation accepts a type IBuffer^ that type is the contract and the operation must not depends on other internals. But  ReadAsync/WriteAsync clearly breaks this assumption.

    Is there a way to implement an IBuffer that can be used with ReadAsync/WriteAsync operations?

    Regards,

    Jose
    Wednesday, May 9, 2012 4:19 PM

Answers

  • Hi,

    I finally decide to directly use the DataWriter as seems the simplest way to send data, i doing something like:

    unsigned char* buffer[] = .... // That will be free by my code after StoreAsync has completed
    
    class Transceiver
    {
        Transceiver(StreamSocket^ socket)
            _socket(socket),
            _writer(ref new DataWriter(_socket->OutputStream))
        {
        }
    
        bool
        write(unsigned char* buffer, unsigned int size)
        {
            _writer = new DataWriter(socket->OutputStream);
            _writer->WriteBytes(ref new Array<unsigned char>(buffer, size));
            DataWriterStoreOperation writerResult = writer->StoreAsync();
            //handle result.
        }
    
    private:
    
        StreamSocket^ _socket;
        DataWriter^ _writer;
    }
    
    do you think this code is right?


    Thursday, May 10, 2012 3:38 PM

All replies

  •  

    Hello,

     

    You can try to convert other stream to the Ibuffer by the DataWriter class.

     

    You can use other stream inherit the IInputStream interface so that it can have the readasync function, also IOutputStream has the writeasync function.

     

    At last convert them through DataWriter class.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 10, 2012 7:50 AM
  • Hi,

    I finally decide to directly use the DataWriter as seems the simplest way to send data, i doing something like:

    unsigned char* buffer[] = .... // That will be free by my code after StoreAsync has completed
    
    class Transceiver
    {
        Transceiver(StreamSocket^ socket)
            _socket(socket),
            _writer(ref new DataWriter(_socket->OutputStream))
        {
        }
    
        bool
        write(unsigned char* buffer, unsigned int size)
        {
            _writer = new DataWriter(socket->OutputStream);
            _writer->WriteBytes(ref new Array<unsigned char>(buffer, size));
            DataWriterStoreOperation writerResult = writer->StoreAsync();
            //handle result.
        }
    
    private:
    
        StreamSocket^ _socket;
        DataWriter^ _writer;
    }
    
    do you think this code is right?


    Thursday, May 10, 2012 3:38 PM
  • Hello Pepone,

    That looks like an acceptable approach.

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, May 11, 2012 1:02 AM
    Moderator