version 1.3, 2001/05/04 01:06:30 |
version 1.5, 2002/10/17 13:40:29 |
|
|
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.2 2000/01/16 07:55:48 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.4 2001/08/12 03:13:36 takayama Exp $ */ |
/* msg0s.c */ |
/* msg0s.c */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
Line 369 struct object KsocketRead(struct object obj) { |
|
Line 369 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 476 struct object KsocketWriteByte(struct object obj) { |
|
Line 478 struct object KsocketWriteByte(struct object obj) { |
|
struct object ob1; |
struct object ob1; |
struct object ob2; |
struct object ob2; |
int socketid; |
int socketid; |
int r; |
int r,i,n,kk,r0; |
char data[2]; |
#define DATA_SIZE 1024 |
|
char data[DATA_SIZE]; |
if (obj.tag != Sarray) { |
if (obj.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
if (getoaSize(obj) < 2) { |
if (getoaSize(obj) < 2) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
ob1 = getoa(obj,0); |
ob1 = getoa(obj,0); |
ob2 = getoa(obj,1); |
ob2 = getoa(obj,1); |
if (ob1.tag != Sinteger) { |
if (ob1.tag != Sinteger) { |
errorMsg1s("KsocketWriteByte([integer socketid, int]) : the first argument must be an integer."); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer."); |
} |
} |
socketid = KopInteger(ob1); |
socketid = KopInteger(ob1); |
if (ob2.tag != Sinteger) { |
if (ob2.tag != Sinteger && ob2.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int]) : the second argument must be a string."); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers."); |
} |
} |
data[0] = KopInteger(ob2); |
if (ob2.tag == Sinteger) { |
r = write(socketid,data, 1); |
data[0] = KopInteger(ob2); |
|
r = write(socketid,data, 1); |
|
}else{ |
|
n = getoaSize(ob2); kk = 0; r = 0; |
|
for (i=0; i<n; i++) { |
|
if (getoa(ob2,i).tag != Sinteger) |
|
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers."); |
|
data[kk] = KopInteger(getoa(ob2,i)); |
|
kk++; |
|
if (kk >= DATA_SIZE) { |
|
r0 = write(socketid,data,kk); |
|
if (r0 != kk) { |
|
fprintf(stderr,"Warning: Could not write to the socket.\n"); |
|
return(KpoInteger(r+r0)); |
|
} |
|
r += r0; |
|
kk = 0; |
|
} |
|
} |
|
if (kk > 0) { |
|
r0 = write(socketid,data,kk); |
|
if (r0 != kk) { |
|
fprintf(stderr,"Warning: Could not write to the socket.\n"); |
|
return(KpoInteger(r+r0)); |
|
} |
|
r += r0; |
|
} |
|
} |
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 512 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 610 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) { |