/**********************************************************************
 * ti library's combined send-message/receive message routine
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/talk.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/talk.c,v 1.1 92/04/01 16:15:34 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid__ti_talk_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/talk.c,v 1.1 92/04/01 16:15:34 brlewis Exp $";
#endif /* lint */

#include <errno.h>
#include <ti/ti.h>
#include <stdarg.h>

/**********************************************************************
 * _ti_talk(TI_H hti, LPSTR outbuf, int outbufsize, ...)
 *	outgoing message in outbuf
 *	space in inbuf (should be different from outbuf)
 *
 * - writes message and reads response
 * - reconnects/reauthenticates if necessary
 * - returns error code
 **********************************************************************/

/* jms 7/9/92 -- windows-izing begun. */

long
_ti_talk(hti, outbuf, outbufsize, inbuf, inbufsize)
     TI_H hti;
     LPSTR outbuf;
     int outbufsize;
     LPSTR inbuf;
     int inbufsize;
{
  long code;
  int try;

  /* note: no need to dereference the TI_H handle. */

  /* try three times to get the server to listen */
  for (try=1; try < 4; try++)
    {
      /* write message */
      code = _ti_sendmsg(hti, outbuf, outbufsize);

      /* read response */
      if (!code)
	code = _ti_recvmsg(hti, inbuf, inbufsize);

      /* success? */
      if (!code)
        return(code);

      /* handle error */
      switch(code)
	{
	case (long) EPIPE:
#ifdef ECONNRESET		/* not a POSIX error code */
	case (long) ECONNRESET:
#endif
	  /* If server broke connection, retry */
	  if (code = ti_connect(hti)) return(code);
	  continue;
	default:
	  return(code);
	}
    }
  return(code);
}

long
_ti_talkf(hti, inbuf, inbufsize, format, ...)
TI_H hti;
LPSTR inbuf;
int inbufsize;
LPSTR format;
{
  static char outbuf[BUFSIZ];
  va_list pvar;

  va_start(pvar, format);
  vsprintf(outbuf, format, pvar);
  va_end(pvar);

  return(_ti_talk(hti, outbuf, strlen(outbuf),
		       inbuf, inbufsize));
}





