version 1.4, 2001/08/12 03:13:36 |
version 1.6, 2002/10/20 07:58:18 |
|
|
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.3 2001/05/04 01:06:30 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.5 2002/10/17 13:40:29 takayama Exp $ */ |
/* msg0s.c */ |
/* msg0s.c */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
|
|
|
|
extern int Quiet; |
extern int Quiet; |
static void errorMsg1s(char *s); |
static void errorMsg1s(char *s); |
|
#define MAX_LISTEN_QUEUE 3 |
|
|
/* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */ |
/* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */ |
struct object KsocketOpen(struct object obj) { |
struct object KsocketOpen(struct object obj) { |
Line 78 struct object KsocketOpen(struct object obj) { |
|
Line 79 struct object KsocketOpen(struct object obj) { |
|
} |
} |
|
|
|
|
if (listen(s_waiting,1) < 0) { |
if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) { |
errorMsg1s("Listen failed"); |
errorMsg1s("Listen failed"); |
} |
} |
/* |
/* |
Line 369 struct object KsocketRead(struct object obj) { |
|
Line 370 struct object KsocketRead(struct object obj) { |
|
} |
} |
} |
} |
|
|
r = (char *)GC_malloc(sizeof(char)*(n+1)); |
r = (char *)GC_malloc(sizeof(char)*(totalsize+1)); |
if (r == (char *)NULL) errorMsg1s("Out of Memory."); |
if (r == (char *)NULL) errorMsg1s("Out of Memory."); |
bcopy(data,r,totalsize); |
bcopy(data,r,totalsize); |
robj = KpoString(r); |
r[totalsize] = 0; |
|
robj = KpoString(r); /* BUG: it works only for reading string from TCP/IP |
|
stream. */ |
|
|
return(robj); |
return(robj); |
|
|
Line 526 struct object KsocketWriteByte(struct object obj) { |
|
Line 529 struct object KsocketWriteByte(struct object obj) { |
|
return(KpoInteger(r)); |
return(KpoInteger(r)); |
} |
} |
|
|
|
struct object KsocketReadBlock(struct object socketObj) { |
|
/* Read until the empty line appears. */ |
|
struct object ob; |
|
char *s; |
|
char *sss; |
|
char *tmp; |
|
int i; |
|
int flag; |
|
int datasize; |
|
ob = KsocketRead(socketObj); |
|
s = KopString(ob); |
|
flag = 0; |
|
for (i=strlen(s)-1; i>=0; i--) { |
|
if ((s[i] == '\n') && (i==0)) { |
|
flag = 1; |
|
}else if ((s[i] == '\n') && (s[i-1] == '\n')) { |
|
flag = 1; |
|
}else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) { |
|
flag = 1; |
|
}else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) { |
|
flag = 1; |
|
} |
|
} |
|
if (flag == 1) return ob; |
|
datasize = strlen(s); |
|
sss = s; |
|
|
|
while (flag == 0) { |
|
fprintf(stderr,"Waiting in socketReadBlock (spin lock to wait an empty line).\n"); |
|
if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n"); |
|
}else{ |
|
/* for debugging. */ |
|
for (i=0; i<strlen(sss); i++) { |
|
fprintf(stderr,"%3x",sss[i]); |
|
} |
|
fprintf(stderr,"\n"); |
|
} |
|
sleep(2); |
|
ob = KsocketRead(socketObj); |
|
s = KopString(ob); |
|
flag = 0; |
|
for (i=strlen(s)-1; i>=0; i--) { |
|
if ((s[i] == '\n') && (i==0)) { |
|
flag = 1; |
|
}else if ((s[i] == '\n') && (s[i-1] == '\n')) { |
|
flag = 1; |
|
}else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) { |
|
flag = 1; |
|
}else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) { |
|
flag = 1; |
|
} |
|
} |
|
if (datasize-1 <= strlen(sss)+strlen(s)) { |
|
tmp = (char *)GC_malloc(sizeof(char)*2*(datasize+strlen(s))+1); |
|
if (tmp == (char *)NULL) errorMsg1s("Out of Memory."); |
|
strcpy(tmp,sss); |
|
strcat(tmp,s); |
|
datasize = 2*(datasize+strlen(s)); |
|
sss = tmp; |
|
}else{ |
|
strcat(sss,s); |
|
} |
|
} |
|
|
|
return KpoString(sss); |
|
|
|
} |
|
|
struct object Kplugin_sm1Socket(char *key,struct object obj) { |
struct object Kplugin_sm1Socket(char *key,struct object obj) { |
struct object robj = NullObject; |
struct object robj = NullObject; |
if (strcmp(key,"open") == 0) { |
if (strcmp(key,"open") == 0) { |
Line 540 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 611 struct object Kplugin_sm1Socket(char *key,struct objec |
|
robj = KsocketSelectMulti(obj); |
robj = KsocketSelectMulti(obj); |
}else if (strcmp(key,"read") == 0) { |
}else if (strcmp(key,"read") == 0) { |
robj = KsocketRead(obj); |
robj = KsocketRead(obj); |
|
}else if (strcmp(key,"readBlock") == 0) { |
|
robj = KsocketReadBlock(obj); |
}else if (strcmp(key,"write") == 0) { |
}else if (strcmp(key,"write") == 0) { |
robj = KsocketWrite(obj); |
robj = KsocketWrite(obj); |
}else if (strcmp(key,"read") == 0) { |
}else if (strcmp(key,"read") == 0) { |