view netinstall/simpsock.cc @ 1346:01c57eb70ae9

[xemacs-hg @ 2003-03-09 02:27:27 by ben] To: xemacs-patches@xemacs.org i.c: Sleep between calls to check for I/O, since these calls are non-blocking. behavior.el: Allow other keywords for forward compatibility. cl-macs.el: Rewrite to eliminate byte-compiler warning when `return' is used without `finally'. cmdloop.el: Avoid truncated error messages for `end-of-file' and the like. cmdloop.el: Avoid char-int error after syncing. files.el: Eliminate byte-compile warnings. printer.el: Fix line-width calculations. #### This used to work. Someone's changes (perhaps by Michael Sperber?) seem to have messed something up. simple.el: Use new clear-left-side functions to avoid messages ending up on the same line as other output. xemacs.mak: Add override for info/ as well when separate source/build dirs. xemacs.mak: Order sections in main build process and add comments. Add additional dependencies to try and prevent later steps from happening when failures in earlier steps have occurred. Makefile.in.in: Order sections in main build process and add comments. Add additional dependencies to try and prevent later steps from happening when failures in earlier steps have occurred. alloc.c: Don't arbitrarily clear Vconfigure_info_directory since it messes up separate build/source dirs. console.c, console.h, device-msw.c, device.c: Add accidentally omitted msprinter console and data descriptions. print.c, console-msw.c: Add clear-left-side functionality to help keep stdio/stderr output from separate sources on separate lines. Generalize the different kinds of debugging output. Add dpa(). profile.c: Add better docs on Unix/Windows differences. regex.c: Fix problems with rel-alloc compilation caused by previous patch. emacs.c: Seg fault rather than abort on Cygwin, since gdb doesn't trap aborts properly. console-gtk-impl.h, console-gtk.h, console-msw.h, console-x-impl.h, console-x.h, dialog-gtk.c, dialog-x.c, event-msw.c, frame-gtk.c, frame-x.c, frameslots.h, glyphs-gtk.c, glyphs-x.c, gui-gtk.c, gui-x.c, inline.c, menubar-gtk.c, menubar-msw.c, menubar-x.c, scrollbar-gtk.c, scrollbar-x.c, ui-gtk.c: Delete popup-data object. Delete menubar_data field from frames, since its usage is frame-specific. Delete menubar-msw.h, gui-x.h, gui-gtk.h. Clean up handling of lwlib callback data GCPRO'ing and add missing GCPRO recomputation in widget code.
author ben
date Sun, 09 Mar 2003 02:27:46 +0000
parents 3078fd1074e8
children
line wrap: on
line source

/*
 * Copyright (c) 2000, Red Hat, Inc.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by DJ Delorie <dj@cygnus.com>
 *
 */

/* Simplified socket access functions */

#include "win32.h"
#include <winsock.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "simpsock.h"
#include "msg.h"

#define SSBUFSZ 1024

SimpleSocket::SimpleSocket (char *hostname, int port)
{
  static int initted = 0;
  if (!initted)
    {
      initted = 1;
      WSADATA d;
      WSAStartup (MAKEWORD (1,1), &d);
    }

  s = INVALID_SOCKET;
  buf = (char *) malloc (SSBUFSZ + 3);
  putp = getp = 0;

  int i1, i2, i3, i4;
  unsigned char ip[4];

  if (sscanf (hostname, "%d.%d.%d.%d", &i1, &i2, &i3, &i4) == 4)
    {
      ip[0] = i1;
      ip[1] = i2;
      ip[2] = i3;
      ip[3] = i4;
    }
  else
    {
      struct hostent *he;
      he = gethostbyname (hostname);
      if (!he)
	{
	  msg ("Can't resolve `%s'\n", hostname);
	  return;
	}
      memcpy (ip, he->h_addr_list[0], 4);
    }

  s = socket (AF_INET, SOCK_STREAM, 0);
  if (s == INVALID_SOCKET)
    {
      msg ("Can't create socket, %d", WSAGetLastError ());
      return;
    }

  struct sockaddr_in name;

  memset (&name, 0, sizeof (name));
  name.sin_family = AF_INET;
  name.sin_port = htons (port);
  memcpy (&name.sin_addr, ip, 4);

  if (connect (s, (sockaddr *)&name, sizeof(name)))
    {
      msg ("Can't connect to %s:%d", hostname, port);
      closesocket (s);
      s = INVALID_SOCKET;
      return;
    }

  return;
}

SimpleSocket::~SimpleSocket ()
{
  if (s != INVALID_SOCKET)
    closesocket (s);
  s = INVALID_SOCKET;
  if (buf)
    free (buf);
  buf = 0;
}

int
SimpleSocket::ok ()
{
  if (s == INVALID_SOCKET)
    return 0;
  return 1;
}

int
SimpleSocket::printf (char *fmt, ...)
{
  char localbuf[SSBUFSZ];
  va_list args;
  va_start (args, fmt);
  vsprintf (localbuf, fmt, args);
  return send (s, localbuf, strlen (localbuf), 0);
}

int
SimpleSocket::write (char *localbuf, int len)
{
  return send (s, localbuf, len, 0);
}

int
SimpleSocket::fill ()
{
  if (putp == getp)
    putp = getp = 0;

  int n = SSBUFSZ - putp;
  if (n == 0)
    return 0;
  int r = recv (s, buf + putp, n, 0);
  if (r > 0)
    {
      putp += r;
      return r;
    }
  return 0;
}

char *
SimpleSocket::gets ()
{
  if (getp > 0 && putp > getp)
    {
      memmove (buf, buf+getp, putp-getp);
      putp -= getp;
      getp = 0;
    }
  if (putp == getp)
    fill();

  // getp is zero, always, here, and putp is the count
  char *nl;
  while ((nl = (char *)memchr (buf, '\n', putp)) == NULL && putp < SSBUFSZ)
    if (fill () <= 0)
      break;

  if (nl)
    {
      getp = nl - buf + 1;
      while ((*nl == '\n' || *nl == '\r') && nl >= buf)
	*nl-- = 0;
    }
  else
    {
      getp = putp;
      nl = buf + putp;
      nl[1] = 0;
    }

  return buf;
}

#define MIN(a,b) ((a) < (b) ? (a) : (b))

int
SimpleSocket::read (char *ubuf, int ulen)
{
  int n, rv=0;
  if (putp > getp)
    {
      n = MIN (ulen, putp-getp);
      memmove (ubuf, buf+getp, n);
      getp += n;
      ubuf += n;
      ulen -= n;
      rv += n;
    }
  while (ulen > 0)
    {
      n = recv (s, ubuf, ulen, 0);
      if (n <= 0)
	return rv;
      ubuf += n;
      ulen -= n;
      rv += n;
    }
  return rv;
}