diff --git a/xwords4/common/comms.c b/xwords4/common/comms.c index 9d2f2aabf..853f45f1c 100644 --- a/xwords4/common/comms.c +++ b/xwords4/common/comms.c @@ -2020,7 +2020,6 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID, XP_U16 len = 0; CommsAddrRec addr; XWStreamCtxt* tmpStream; - XP_U8* buf; comms_getAddr( comms, &addr ); tmpStream = mem_stream_make( MPPARM(comms->mpool) @@ -2101,22 +2100,17 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID, } len = stream_getSize( tmpStream ); - buf = XP_MALLOC( comms->mpool, len ); - if ( buf != NULL ) { - stream_getBytes( tmpStream, buf, len ); - } - stream_destroy( tmpStream ); - if ( buf != NULL ) { + if ( 0 < len ) { XP_U16 result; XP_LOGF( "%s: passing %d bytes to sendproc", __func__, len ); - result = (*comms->procs.send)( buf, len, &addr, - comms->procs.closure ); + result = (*comms->procs.send)( stream_getPtr(tmpStream), len, + &addr, comms->procs.closure ); success = result == len; if ( success ) { setHeartbeatTimer( comms ); } } - XP_FREE( comms->mpool, buf ); + stream_destroy( tmpStream ); } return success; } /* send_via_relay */ diff --git a/xwords4/common/memstream.c b/xwords4/common/memstream.c index ec64b2439..569a04395 100644 --- a/xwords4/common/memstream.c +++ b/xwords4/common/memstream.c @@ -301,7 +301,7 @@ mem_stream_copyFromStream( XWStreamCtxt* p_sctx, XWStreamCtxt* src, if ( nBytes < len ) { len = nBytes; } - stream_getBytes( src, buf, len ); + stream_getBytes( src, buf, len );// fix to use stream_getPtr()? stream_putBytes( p_sctx, buf, len ); nBytes -= len; } @@ -338,6 +338,13 @@ mem_stream_getSize( const XWStreamCtxt* p_sctx ) return size; } /* mem_stream_getSize */ +static const XP_U8* +mem_stream_getPtr( const XWStreamCtxt* p_sctx ) +{ + MemStreamCtxt* stream = (MemStreamCtxt*)p_sctx; + return stream->buf; +} /* mem_stream_getPtr */ + static XP_PlayerAddr mem_stream_getAddress( XWStreamCtxt* p_sctx ) { @@ -451,6 +458,7 @@ make_vtable( MemStreamCtxt* stream ) SET_VTABLE_ENTRY( vtable, stream_close, mem ); SET_VTABLE_ENTRY( vtable, stream_getSize, mem ); + SET_VTABLE_ENTRY( vtable, stream_getPtr, mem ); SET_VTABLE_ENTRY( vtable, stream_getAddress, mem ); SET_VTABLE_ENTRY( vtable, stream_setAddress, mem ); diff --git a/xwords4/common/server.c b/xwords4/common/server.c index 25fbd864b..d259cfa6e 100644 --- a/xwords4/common/server.c +++ b/xwords4/common/server.c @@ -841,14 +841,11 @@ showPrevScore( ServerCtxt* server ) if ( !!server->nv.prevMoveStream ) { XWStreamCtxt* prevStream = server->nv.prevMoveStream; XP_U16 len = stream_getSize( prevStream ); - XP_UCHAR* buf = XP_MALLOC( server->mpool, len ); - stream_getBytes( prevStream, buf, len ); - stream_destroy( prevStream ); server->nv.prevMoveStream = NULL; - stream_putBytes( stream, buf, len ); - XP_FREE( server->mpool, buf ); + stream_putBytes( stream, stream_getPtr( prevStream ), len ); + stream_destroy( prevStream ); } (void)util_userQuery( util, QUERY_ROBOT_MOVE, stream ); diff --git a/xwords4/common/xwstream.h b/xwords4/common/xwstream.h index 56dbacf2b..32a72f8f5 100644 --- a/xwords4/common/xwstream.h +++ b/xwords4/common/xwstream.h @@ -69,6 +69,8 @@ typedef struct StreamCtxVTable { void (*m_stream_close)( XWStreamCtxt* dctx ); XP_U16 (*m_stream_getSize)( const XWStreamCtxt* dctx ); + + const XP_U8* (*m_stream_getPtr)( const XWStreamCtxt* dctx ); /* void (*m_stream_makeReturnAddr)( XWStreamCtxt* dctx, XP_PlayerAddr* addr, */ /* XP_U16* addrLen ); */ @@ -143,6 +145,9 @@ struct XWStreamCtxt { #define stream_getSize(sc) \ (sc)->vtable->m_stream_getSize((sc)) +#define stream_getPtr(sc) \ + (sc)->vtable->m_stream_getPtr((sc)) + #define stream_makeReturnAddr(sc,addr,len) \ (sc)->vtable->m_stream_makeReturnAddr((sc),(addr),(len))