Exemplo n.º 1
0
/*
  nbytes = MG_NET_RECV(socket, variable [, MAXIMUM_BYTES=b])

  Reads the raw data available on the socket and returns a BYTE array in
  variable. The maximum number of bytes to read can be specified by the
  MAXIMUM_BYTES keyword. The default is to read all the data available on the
  socket. Note: no byteswapping is performed.
*/
IDL_VPTR IDL_CDECL mg_net_recv(int argc, IDL_VPTR argv[], char *argk) {
	IDL_LONG i, iRet, err;
	int len;
	IDL_VPTR vpPlainArgs[2], vpTmp;
	char *pbuffer;

	static IDL_LONG	iMax;
	static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
		{ "MAXIMUM_BYTES", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iMax) },
    { NULL }
  };

	IDL_KWCleanup(IDL_KW_MARK);
	IDL_KWGetParams(argc, argv, argk, kw_pars, vpPlainArgs, 1);

	i = IDL_LongScalar(vpPlainArgs[0]);
	if ((i < 0) || (i >= MAX_SOCKETS)) return (IDL_GettmpLong(-1));
	if (net_list[i].iState != NET_IO) return (IDL_GettmpLong(-1));
	IDL_EXCLUDE_EXPR(vpPlainArgs[1]);

	err = IOCTL(net_list[i].socket, FIONREAD, &len);
	if (err != 0) {
		iRet = -1;
		goto err;
	}
	if (iMax) len = IDL_MIN(iMax, len);

	pbuffer = (char *) IDL_MakeTempVector(IDL_TYP_BYTE, len, IDL_ARR_INI_NOP, &vpTmp);
	IDL_VarCopy(vpTmp, vpPlainArgs[1]);

	iRet = recv(net_list[i].socket, pbuffer, len, 0);

err:
	IDL_KWCleanup(IDL_KW_CLEAN);

	return(IDL_GettmpLong(iRet));
}
Exemplo n.º 2
0
/*
  out = MG_NET_SELECT(sockets[], timeout)

  Checks to see if there is data waiting to be read or a connection has been
  requested for a list of sockets. The return value is -1 on error, scalar 0
  if no sockets are ready or returns a list of the sockets which are ready.
  The routine waits the number of seconds specified by the timeout argument
  for sockets to become ready. A timeout value of 0 results in a poll of the
  sockets.
*/
static IDL_VPTR IDL_CDECL mg_net_select(int argc, IDL_VPTR argv[], char *argk) {
  struct timeval timeval;
  fd_set rfds;

  IDL_LONG i, j;
  IDL_LONG n, num;

  float	fWait;
  IDL_LONG *piSocks,iNum;
  IDL_VPTR vpSocks;

  vpSocks = IDL_CvtLng(1, &(argv[0]));
  IDL_VarGetData(vpSocks, &iNum, (char **) &piSocks, 1);
  fWait = (float) IDL_DoubleScalar(argv[1]);

  num = -1;
  FD_ZERO(&rfds);

  for (j = 0; j < iNum; j++) {
    i = piSocks[j];
    if ((i < 0) || (i >= MAX_SOCKETS)) {
      if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);
      return (IDL_GettmpLong(-1));
    }
    if (net_list[i].iState != NET_UNUSED) {
      FD_SET(net_list[i].socket, &rfds);
      if (net_list[i].socket > (SOCKET) num) num = net_list[i].socket;
    }
  }
  while (fWait >= 0.0) {
    if (fWait >= 2.0) {
      timeval.tv_sec = 2;
      timeval.tv_usec = 0;
    } else {
      timeval.tv_sec = (long) fWait;
      fWait = fWait - timeval.tv_sec;
      timeval.tv_usec = (long) (fWait * 1000000);
    }
    n = select(num + 1, &rfds, NULL, NULL, &timeval);
    if (n == -1) fWait = -1.0;
    if (n > 0) fWait = -1.0;
    fWait -= 2.0;
    if (IDL_BailOut(IDL_FALSE)) {
      n = -1;
      fWait = -1.0;
    }
  }

  if (n > 0) {
    IDL_LONG *pOut;
    IDL_VPTR vpTmp;

    pOut = (IDL_LONG *) IDL_MakeTempVector(IDL_TYP_LONG,
					   n, IDL_ARR_INI_NOP, &vpTmp);
    for (j = 0; j < iNum; j++) {
      i = piSocks[j];
      if (net_list[i].iState != NET_UNUSED) {
	if (FD_ISSET(net_list[i].socket, &rfds)){
	  *pOut++ = i;
	}
      }
    }
    if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);
    return (vpTmp);
  }

  if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);

  return (IDL_GettmpLong(n));
}