<?php

function SendMPacket( $pkt, $ModeName )
  {
    int	total  = 0;
    int length = 0;
    int sent   = 0;

    echo "SendMPacket: Sending MDSQ ".$ModeName."\n";
    memset( pkt, 0, BUFFER_SIZE );

    total = sizeof( pkt->header ) + strlen( ModeName ) + 1;
    pkt->header.token = SLT_MODESETQUERY;
    pkt->header.length = htonl( total );

    if( *ModeName )
	strcpy( pkt->body.modesetquery.modesetquery, ModeName );

    while( total != sent )
      {
	length = send( tcpSocket, (const char *)(pkt + sent), total-sent, 0 );
	if( length < 0 )
	  {
	    vbPrintf( 0, "SendMPacket: Error sending...\n" );
	    return -1;
	  }
	sent += length;
      }
    return 0;
  }


int SendPacket( char *pkt, size_t size )
  {
    int length = 0;
    size_t sent   = 0;
    int tosend;

    while( size  !=  sent )
      {
	tosend	= size - sent;
	//	if ( tosend  >  4 )
	//	    tosend = 4;
	length	= send( tcpSocket, (const char *)(pkt + sent), tosend, 0 );
	if( length  <  0 )
	  {
	    vbPrintf( 0, "SendPacket: Error sending packet: %d\n", errno );
            if( errno != EAGAIN )
	        return errno;
            errno = 0;
            continue;
	  }
	sent += length;
      }
    return 0;
  }


void RecvPacket( SLPACKET *pkt )
  {
    int	    length  = 0;
    int	    total   = 0;
    char   *p;

    length  = recv( tcpSocket, (char *)pkt, 4, 0 );
    total   = ntohl( pkt->header.length ) - sizeof pkt->header.length;
    p	    = (char *)&pkt->header.token;

    while ( total  >  0 )
      {
	length	= recv( tcpSocket, p, total, 0 );
	if ( length  <=  0 )
	    continue;
	total	-= length;
	p	+= length;
      }
  }

?>
