1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-28 09:22:27 +01:00

Added an example of how to create a network client using WMConnection

This commit is contained in:
dan
1999-12-14 01:45:16 +00:00
parent c952eb84ae
commit e5a7a883eb
4 changed files with 229 additions and 5 deletions

View File

@@ -18,7 +18,7 @@ lib_LIBRARIES = libWINGs.a libWUtil.a
include_HEADERS = WINGs.h WUtil.h WINGsP.h include_HEADERS = WINGs.h WUtil.h WINGsP.h
noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel connect
testmywidget_SOURCES = testmywidget.c mywidget.c mywidget.h testmywidget_SOURCES = testmywidget.c mywidget.c mywidget.h
@@ -42,11 +42,14 @@ testcolorpanel_SOURCES = testcolorpanel.c
testcolorpanel_LDADD = libWINGs.a $(LIBLIST) testcolorpanel_LDADD = libWINGs.a $(LIBLIST)
wmquery_SOURCES = wmquery.c wmquery_SOURCES = wmquery.c
wmquery_LDADD = libWINGs.a $(LIBLIST) wmquery_LDADD = libWINGs.a $(LIBLIST)
connect_SOURCES = connect.c
connect_LDADD = libWUtil.a @X_EXTRA_LIBS@ @LIBPL@
EXTRA_DIST = logo.xpm BUGS EXTRA_DIST = logo.xpm BUGS

View File

@@ -107,7 +107,7 @@ lib_LIBRARIES = libWINGs.a libWUtil.a
include_HEADERS = WINGs.h WUtil.h WINGsP.h include_HEADERS = WINGs.h WUtil.h WINGsP.h
noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel connect
testmywidget_SOURCES = testmywidget.c mywidget.c mywidget.h testmywidget_SOURCES = testmywidget.c mywidget.c mywidget.h
@@ -135,6 +135,10 @@ wmquery_SOURCES = wmquery.c
wmquery_LDADD = libWINGs.a $(LIBLIST) wmquery_LDADD = libWINGs.a $(LIBLIST)
connect_SOURCES = connect.c
connect_LDADD = libWUtil.a @X_EXTRA_LIBS@ @LIBPL@
EXTRA_DIST = logo.xpm BUGS EXTRA_DIST = logo.xpm BUGS
# wbutton.c # wbutton.c
@@ -194,6 +198,9 @@ testcolorpanel_OBJECTS = testcolorpanel.o
testcolorpanel_DEPENDENCIES = libWINGs.a \ testcolorpanel_DEPENDENCIES = libWINGs.a \
$(top_builddir)/wrlib/libwraster.la $(top_builddir)/wrlib/libwraster.la
testcolorpanel_LDFLAGS = testcolorpanel_LDFLAGS =
connect_OBJECTS = connect.o
connect_DEPENDENCIES = libWUtil.a
connect_LDFLAGS =
CFLAGS = @CFLAGS@ CFLAGS = @CFLAGS@
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
@@ -208,8 +215,8 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = tar TAR = tar
GZIP_ENV = --best GZIP_ENV = --best
SOURCES = $(libWINGs_a_SOURCES) $(libWUtil_a_SOURCES) $(wtest_SOURCES) $(wmquery_SOURCES) $(wmfile_SOURCES) $(fontl_SOURCES) $(testmywidget_SOURCES) $(testcolorpanel_SOURCES) SOURCES = $(libWINGs_a_SOURCES) $(libWUtil_a_SOURCES) $(wtest_SOURCES) $(wmquery_SOURCES) $(wmfile_SOURCES) $(fontl_SOURCES) $(testmywidget_SOURCES) $(testcolorpanel_SOURCES) $(connect_SOURCES)
OBJECTS = $(libWINGs_a_OBJECTS) $(libWUtil_a_OBJECTS) $(wtest_OBJECTS) $(wmquery_OBJECTS) $(wmfile_OBJECTS) $(fontl_OBJECTS) $(testmywidget_OBJECTS) $(testcolorpanel_OBJECTS) OBJECTS = $(libWINGs_a_OBJECTS) $(libWUtil_a_OBJECTS) $(wtest_OBJECTS) $(wmquery_OBJECTS) $(wmfile_OBJECTS) $(fontl_OBJECTS) $(testmywidget_OBJECTS) $(testcolorpanel_OBJECTS) $(connect_OBJECTS)
all: all-redirect all: all-redirect
.SUFFIXES: .SUFFIXES:
@@ -335,6 +342,10 @@ testcolorpanel: $(testcolorpanel_OBJECTS) $(testcolorpanel_DEPENDENCIES)
@rm -f testcolorpanel @rm -f testcolorpanel
$(LINK) $(testcolorpanel_LDFLAGS) $(testcolorpanel_OBJECTS) $(testcolorpanel_LDADD) $(LIBS) $(LINK) $(testcolorpanel_LDFLAGS) $(testcolorpanel_OBJECTS) $(testcolorpanel_LDADD) $(LIBS)
connect: $(connect_OBJECTS) $(connect_DEPENDENCIES)
@rm -f connect
$(LINK) $(connect_LDFLAGS) $(connect_OBJECTS) $(connect_LDADD) $(LIBS)
install-includeHEADERS: $(include_HEADERS) install-includeHEADERS: $(include_HEADERS)
@$(NORMAL_INSTALL) @$(NORMAL_INSTALL)
$(mkinstalldirs) $(DESTDIR)$(includedir) $(mkinstalldirs) $(DESTDIR)$(includedir)

209
WINGs/connect.c Normal file
View File

@@ -0,0 +1,209 @@
/*
* WINGs connect.c: example how to create a netwrok client using WMConnection
*
* Copyright (c) 1999 Dan Pascu
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "WINGs.h"
static int initialized = 0;
static void didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr);
static void connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr);
static void didInitialize(ConnectionDelegate *self, WMConnection *cPtr);
static ConnectionDelegate socketDelegate = {
NULL, /* data */
NULL, /* didCatchException */
connectionDidDie, /* didDie */
didInitialize, /* didInitialize */
didReceiveInput, /* didReceiveInput */
NULL /* didTimeout */
};
void
wAbort(Bool foo) /*FOLD00*/
{
exit(1);
}
static void
printHelp(char *progname) /*FOLD00*/
{
printf("usage: %s [host [port]]\n\n", progname);
printf(" --help print this message\n");
}
static char*
getMessage(WMConnection *cPtr)
{
char *buffer;
WMData *aData;
int length;
aData = WMGetConnectionAvailableData(cPtr);
if (!aData)
return NULL;
if ((length=WMGetDataLength(aData))==0) {
WMReleaseData(aData);
return NULL;
}
buffer = (char*)wmalloc(length+1);
WMGetDataBytes(aData, buffer);
buffer[length]= '\0';
WMReleaseData(aData);
return buffer;
}
static void
inputHandler(int fd, int mask, void *clientData)
{
WMConnection *cPtr = (WMConnection*)clientData;
WMData *aData;
char buf[4096];
int n;
if (!initialized)
return;
n = read(fd, buf, 4096);
if (n>0) {
aData = WMCreateDataWithBytes(buf, n);
WMSendConnectionData(cPtr, aData);
WMReleaseData(aData);
}
}
static void
didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
{
char *buffer;
buffer = getMessage(cPtr);
if (!buffer) {
fprintf(stderr, "Connection closed by peer.\n");
exit(0);
}
printf("%s", buffer);
wfree(buffer);
}
static void
connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
{
WMCloseConnection(cPtr);
fprintf(stderr, "Connection closed by peer.\n");
exit(0);
}
static void
didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
{
int state = WMGetConnectionState(cPtr);
if (state == WCConnected) {
fprintf(stderr, "connected to '%s:%s'\n", WMGetConnectionAddress(cPtr),
WMGetConnectionService(cPtr));
initialized = 1;
return;
} else {
wsyserrorwithcode(WCErrorCode, "Unable to connect");
exit(1);
}
}
int
main(int argc, char **argv) /*FOLD00*/
{
char *ProgName, *host, *port;
int i;
WMConnection *sPtr;
wsetabort(wAbort);
WMInitializeApplication("connect", &argc, argv);
ProgName = strrchr(argv[0],'/');
if (!ProgName)
ProgName = argv[0];
else
ProgName++;
host = NULL;
port = "7000";
if (argc>1) {
for (i=1; i<argc; i++) {
if (strcmp(argv[i], "--help")==0) {
printHelp(argv[0]);
exit(0);
} else {
if (!host)
host = argv[i];
else
port = argv[i];
}
}
}
sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
if (!sPtr) {
wfatal("could not create connection. exiting");
exit(1);
}
WMSetConnectionDelegate(sPtr, &socketDelegate);
/* watch what user types and send it over the connection */
WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
while (1) {
WHandleEvents();
}
return 0;
}

View File

@@ -169,6 +169,7 @@ inputHandler(int fd, int mask, void *clientData) /*FOLD00*/
(void*)&result, &len) == 0 && result != 0) { (void*)&result, &len) == 0 && result != 0) {
cPtr->state = WCFailed; cPtr->state = WCFailed;
WCErrorCode = result; WCErrorCode = result;
/* should call wsyserrorwithcode(result, ...) here? */
} else { } else {
cPtr->state = WCConnected; cPtr->state = WCConnected;
} }