[BACK]Return to sm1Socket.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/sm1Socket.c, Revision 1.6

1.6     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.5 2002/10/17 13:40:29 takayama Exp $ */
1.1       maekawa     2: /* msg0s.c */
                      3: #include <stdio.h>
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9:
                     10:
                     11: #include "datatype.h"
                     12: #include "stackm.h"
                     13: #include "extern.h"
                     14: #include "sm1Socket.h"
                     15:
                     16: extern int Quiet;
                     17: static void errorMsg1s(char *s);
1.6     ! takayama   18: #define MAX_LISTEN_QUEUE 3
1.1       maekawa    19:
                     20: /* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */
                     21: struct object KsocketOpen(struct object obj) {
                     22:   char serverName[1024];
                     23:   int portNumber;
                     24:   struct object rob = NullObject;
                     25:   struct hostent *myhost;
                     26:   struct sockaddr_in me;
                     27:   int s_waiting;
                     28:   int on;
                     29:   int tt;
                     30:   extern int errno;
                     31:
                     32:   if (obj.tag != Sarray) {
                     33:     errorMsg1s("KsocketOpen([optional integer,optional string name])");
                     34:   }
                     35:   strcpy(serverName,"localhost");
                     36:   portNumber = 0;
                     37:   if (getoaSize(obj) >= 1) {
                     38:     if ((getoa(obj,0)).tag != Sinteger) {
                     39:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the first argument must be an integer.");
                     40:     }
                     41:     portNumber = KopInteger(getoa(obj,0));
                     42:   }
                     43:   if (getoaSize(obj) >= 2) {
                     44:     if ((getoa(obj,1)).tag != Sdollar) {
                     45:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the second argument must be a string.");
                     46:     }
                     47:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                     48:       errorMsg1s("Too long server name");
                     49:     }
                     50:     strcpy(serverName,KopString(getoa(obj,1)));
                     51:   }
                     52:
                     53:
                     54:   /* fprintf(stderr,"Hello from open.\n"); */
                     55:   if ((myhost = gethostbyname(serverName)) == NULL) {
                     56:     errorMsg1s("Bad server name.");
                     57:   }
                     58:   bzero((char *)&me,sizeof(me));
                     59:   me.sin_family = AF_INET;
                     60:   me.sin_port = htons(portNumber);
                     61:   bcopy(myhost->h_addr,
1.3       takayama   62:         &me.sin_addr,myhost->h_length);
1.1       maekawa    63:
                     64:   if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
                     65:     errorMsg1s("Socket allocation is failed.");
                     66:   }
                     67:
                     68:   on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
                     69:   /* important */
                     70:   if (bind(s_waiting,(struct sockaddr *) &me,sizeof(me)) == -1) {
                     71:     fprintf(stderr,"Bind error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                     72:     errorMsg1s("cannot bind");
                     73:   }
                     74:
                     75:   tt = sizeof(me);
                     76:   if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
                     77:     fprintf(stderr,"getsockname error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                     78:     errorMsg1s("cannot getsockname");
                     79:   }
                     80:
                     81:
1.6     ! takayama   82:   if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.1       maekawa    83:     errorMsg1s("Listen failed");
                     84:   }
                     85:   /*
1.3       takayama   86:       fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port));
                     87:       fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */
1.1       maekawa    88:   rob = newObjectArray(2);
                     89:   putoa(rob,0,KpoInteger(s_waiting));
                     90:   putoa(rob,1,KpoInteger(ntohs(me.sin_port)));
                     91:   return(rob);
                     92:
                     93: }
                     94:
                     95: /* [ integer port, optional string host default localhost]
                     96:    [ integer socketid, integer port ] */
                     97: struct object KsocketConnect(struct object obj) {
                     98:   char serverName[1024];
                     99:   int portNumber;
                    100:   struct hostent *servhost;
                    101:   struct sockaddr_in server;
                    102:   int socketid;
                    103:   struct object robj = NullObject;
                    104:
                    105:   if (obj.tag != Sarray) {
                    106:     errorMsg1s("KsocketConnect( [ integer port, optional string host default localhost])");
                    107:   }
                    108:   strcpy(serverName,"localhost");
                    109:   if (getoaSize(obj) < 1) {
                    110:     errorMsg1s("You need to specify port number.\n");
                    111:   }
                    112:   if (getoaSize(obj) >= 1) {
                    113:     if ((getoa(obj,0)).tag != Sinteger) {
                    114:       errorMsg1s("KsocketConnect([integer port,optional string host]), the first argument must be an integer.");
                    115:     }
                    116:     portNumber = KopInteger(getoa(obj,0));
                    117:   }
                    118:   if (getoaSize(obj) >= 2) {
                    119:     if ((getoa(obj,1)).tag != Sdollar) {
                    120:       errorMsg1s("KsocketConnect([integer port,optional string host]), the second argument must be a string.");
                    121:     }
                    122:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                    123:       errorMsg1s("Too long server name");
                    124:     }
                    125:     strcpy(serverName,KopString(getoa(obj,1)));
                    126:   }
                    127:
                    128:
                    129:   if ((servhost = gethostbyname(serverName)) == NULL) {
                    130:     errorMsg1s("bad server name.\n");
                    131:   }
                    132:   bzero((char *)&server,sizeof(server));
                    133:   server.sin_family = AF_INET;
                    134:   server.sin_port = htons(portNumber);
                    135:   bcopy(servhost->h_addr,
1.3       takayama  136:         (char *)&server.sin_addr,servhost->h_length);
1.1       maekawa   137:
                    138:   if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
                    139:     errorMsg1s("socket allocation is failed.\n");
                    140:   }
                    141:   if (!Quiet) {
                    142:     fprintf(stderr,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
                    143:   }
                    144:   if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
                    145:     errorMsg1s("cannot connect");
                    146:   }
                    147:   /* fprintf(stderr,"connected.\n"); */
                    148:   robj = newObjectArray(2);
                    149:   putoa(robj,0,KpoInteger(socketid));
                    150:   putoa(robj,1,KpoInteger(portNumber));
                    151:   return(robj);
                    152: }
                    153:
                    154: /* [ integer socketid ]
                    155:    [ integer newsocketid ] */
                    156: struct object KsocketAccept(struct object obj) {
                    157:   struct object obj1;
                    158:   struct object obj2;
                    159:   struct object robj;
                    160:   int s, news;
                    161:
                    162:   if (obj.tag != Sarray) {
                    163:     errorMsg1s("KsocketAccept([integer socketid])");
                    164:   }
                    165:   if (getoaSize(obj) < 1) {
                    166:     errorMsg1s("KsocketAccept([integer socketid])");
                    167:   }
                    168:   obj1 = getoa(obj,0);
                    169:   if (obj1.tag != Sinteger ) {
                    170:     errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
                    171:   }
                    172:   s = KopInteger(obj1);
                    173:   if ((news = accept(s,NULL,NULL)) < 0) {
                    174:     errorMsg1s("Error in accept.");
                    175:   }
                    176:   if (close(s) < 0) {
                    177:     errorMsg1s("Error in closing the old socket.");
                    178:   }
                    179:   robj = newObjectArray(1);
                    180:   putoa(robj,0,KpoInteger(news));
                    181:   return(robj);
                    182: }
                    183:
                    184: int KsocketSelect0(int fd,int t) {
                    185:   fd_set readfds;
                    186:   struct timeval timeout;
                    187:   extern int errno;
                    188:   FD_ZERO(&readfds);
                    189:   FD_SET(fd,&readfds);
                    190:   timeout.tv_sec = 0;
                    191:   timeout.tv_usec = (long) t;
                    192:   if (t >= 0) {
                    193:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    194:       /* It must be fd+1 !,  Not fd. */
                    195:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    196:       errorMsg1s("KsocketSelect0() : select failed.");
                    197:       return(0);
                    198:     }
                    199:   }else{ /* block */
                    200:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    201:       errorMsg1s("KsocketSelect0() : select failed.");
                    202:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    203:       return(0);
                    204:     }
                    205:   }
                    206:   if (FD_ISSET(fd,&readfds)) return(1);
                    207:   else return(0);
                    208: }
                    209:
                    210: /* [ integer socketid optional integer timeout default 0]
                    211:    integer true or false
                    212: */
                    213: struct object KsocketSelect(struct object obj) {
                    214:   struct object robj;
                    215:   struct object ob1;
                    216:   struct object ob2;
                    217:   if (obj.tag != Sarray) {
                    218:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    219:   }
                    220:   if (getoaSize(obj) < 1) {
                    221:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    222:   }
                    223:   if (getoaSize(obj) >= 1) {
                    224:     ob1 = getoa(obj,0);
                    225:     ob2 = KpoInteger(0); /* default value */
                    226:   }
                    227:   if (getoaSize(obj) >= 2) {
                    228:     ob2 = getoa(obj,1);
                    229:   }
                    230:   if (ob1.tag != Sinteger) {
                    231:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the first argument must be an integer.");
                    232:   }
                    233:   if (ob2.tag != Sinteger) {
                    234:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the second argument must be an integer.");
                    235:   }
                    236:   robj = KpoInteger( KsocketSelect0(KopInteger(ob1),KopInteger(ob2)) );
                    237:   return(robj);
                    238: }
                    239:
                    240: struct object KsocketSelectMulti(struct object obj)
1.3       takayama  241:      /* [ [integer socketid1, integer socketid2, ...]
1.1       maekawa   242:      optional integer timeout default 0]
                    243:    [ result1, result2, ....]
                    244: */
                    245: {
                    246:   struct object robj;
                    247:   struct object ob1;
                    248:   struct object ob2;
                    249:   struct object ob3;
                    250:   int size,i,fd,p,t;
                    251:   fd_set readfds;
                    252:   struct timeval timeout;
                    253:   extern errno;
                    254:   if (obj.tag != Sarray) {
                    255:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    256:   }
                    257:   if (getoaSize(obj) < 1) {
                    258:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    259:   }
                    260:   if (getoaSize(obj) >= 1) {
                    261:     ob1 = getoa(obj,0);
                    262:     ob2 = KpoInteger(0); /* default value */
                    263:   }
                    264:   if (getoaSize(obj) >= 2) {
                    265:     ob2 = getoa(obj,1);
                    266:   }
                    267:   if (ob1.tag != Sarray) {
                    268:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the first argument must be an array.");
                    269:   }
                    270:   if (ob2.tag != Sinteger) {
                    271:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the second argument must be an integer.");
                    272:   }
                    273:   FD_ZERO(&readfds);
                    274:   timeout.tv_sec = 0; t =  KopInteger(ob2);
                    275:   timeout.tv_usec = (long)t;
                    276:
                    277:   size = getoaSize(ob1);
                    278:   if (size < 1) {
                    279:     errorMsg1s("KsocketSelectMulti: the first argument must be a non-empty array of integers.");
                    280:   }
                    281:   fd = 0;
                    282:
                    283:   for (i=0; i<size; i++) {
                    284:     ob3 = getoa(ob1,i);
                    285:     if (ob3.tag != Sinteger) {
                    286:       errorMsg1s("KsocketSelectMulti: the first argument must be an array of integers.");
                    287:     }
                    288:     p = KopInteger(ob3);
                    289:     if (p > fd) fd = p;
                    290:     FD_SET(p,&readfds);
                    291:     /* printf("p = %d, fd=%d",p,fd); */
                    292:   }
                    293:
                    294:   if (t >= 0) {
                    295:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    296:       /* It must be fd+1 !,  Not fd. */
                    297:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    298:       errorMsg1s("KsocketSelectMulti() : select failed.");
                    299:     }
                    300:   }else{ /* block */
                    301:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    302:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    303:       errorMsg1s("KsocketSelectMulti() : (block) select failed.");
                    304:     }
                    305:   }
                    306:   robj = newObjectArray(size);
                    307:   for (i=0; i<size; i++) {
                    308:     if (FD_ISSET(KopInteger(getoa(ob1,i)),&readfds)) {
                    309:       putoa(robj,i,KpoInteger(1));
                    310:     }else{
                    311:       putoa(robj,i,KpoInteger(0));
                    312:     }
                    313:   }
                    314:
                    315:   return(robj);
                    316: }
                    317:
                    318:
                    319:
                    320:
                    321: static char Data00[1024];
                    322: /* [ integer socketid ]
                    323:    string data
                    324: */
                    325: struct object KsocketRead(struct object obj) {
                    326:   struct object ob1;
                    327:   struct object robj = NullObject;
                    328:   static int datasize = 1024;
                    329:   static char *data = Data00;
                    330:   char *tmp;
                    331:   char *r;
                    332:   int n;
                    333:   int socketid;
                    334:   int totalsize;
                    335:
                    336:   if (obj.tag != Sarray) {
                    337:     errorMsg1s("KsocketRead([integer socketid])");
                    338:   }
                    339:   if (getoaSize(obj) < 1) {
                    340:     errorMsg1s("KsocketRead([integer socketid])");
                    341:   }
                    342:   ob1 = getoa(obj,0);
                    343:   if (ob1.tag != Sinteger) {
                    344:     errorMsg1s("KsocketRead([integer socketid]) : the argument must be integer.");
                    345:   }
                    346:   socketid = KopInteger(ob1);
                    347:
                    348:   totalsize = 0;
                    349:
                    350:   while (KsocketSelect0(socketid,0)) {
                    351:     if (datasize - totalsize > 0) {
                    352:       n = read(socketid,data+totalsize,datasize-totalsize);
                    353:       if (n < 0) {
1.3       takayama  354:         errorMsg1s("Read error.");
1.1       maekawa   355:       }
                    356:       if (n < datasize-totalsize) {
1.3       takayama  357:         totalsize += n;
                    358:         break;
1.1       maekawa   359:       }else{ totalsize += n; }
                    360:       if (totalsize == 0) {
1.3       takayama  361:         errorMsg1s("Select returns 1, but there is no data to read.");
1.1       maekawa   362:       }
                    363:     }else { /* increase the datasize */
                    364:       tmp = (char *)GC_malloc(sizeof(char)*2*datasize);
                    365:       /*I should use GC_malloc_atomic and free after finishing this function?*/
                    366:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    367:       bcopy(data,tmp,totalsize);
                    368:       data = tmp;
                    369:       datasize = 2*datasize;
                    370:     }
                    371:   }
                    372:
1.5       takayama  373:   r = (char *)GC_malloc(sizeof(char)*(totalsize+1));
1.1       maekawa   374:   if (r == (char *)NULL) errorMsg1s("Out of Memory.");
                    375:   bcopy(data,r,totalsize);
1.5       takayama  376:   r[totalsize] = 0;
                    377:   robj = KpoString(r);  /* BUG: it works only for reading string from TCP/IP
                    378:                                                    stream. */
1.1       maekawa   379:
                    380:   return(robj);
                    381:
                    382: }
                    383:
                    384: /* [ integer socketid, string data ]
                    385:    integer  */
                    386: struct object KsocketWrite(struct object obj) {
                    387:   struct object ob1;
                    388:   struct object ob2;
                    389:   int socketid;
                    390:   int r;
                    391:   if (obj.tag != Sarray) {
                    392:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    393:   }
                    394:   if (getoaSize(obj) < 2) {
                    395:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    396:   }
                    397:   ob1 = getoa(obj,0);
                    398:   ob2 = getoa(obj,1);
                    399:   if (ob1.tag != Sinteger) {
                    400:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the first argument must be an integer.");
                    401:   }
                    402:   socketid = KopInteger(ob1);
                    403:   if (ob2.tag != Sdollar) {
                    404:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the second argument must be a string.");
                    405:   }
                    406:   r = write(socketid,KopString(ob2), strlen(KopString(ob2)));
                    407:   return(KpoInteger(r));
                    408:
                    409: }
                    410: struct object KsocketClose(struct object obj) {
                    411:   int socketid;
                    412:   struct object ob1;
                    413:   if (obj.tag != Sarray) {
                    414:     errorMsg1s("KsocketClose([ integer socketid ])");
                    415:   }
                    416:   if (getoaSize(obj) != 1) {
                    417:     errorMsg1s("KsocketClose([ integer socketid ])");
                    418:   }
                    419:   ob1 = getoa(obj,0);
                    420:   if (ob1.tag != Sinteger) {
                    421:     errorMsg1s("KsocketClose([ INTEGER socketid ])");
                    422:   }
                    423:   socketid = KopInteger(ob1);
                    424:   return(KpoInteger(close(socketid)));
                    425: }
                    426:
                    427:
                    428: static void errorMsg1s(char *s) {
                    429:   fprintf(stderr,"%s\n",s);
                    430:   errorKan1("msg1s.c: %s\n",s);
                    431: }
                    432:
                    433:
                    434: /**************** new 1997, 11/23 *******************/
                    435: struct object KsocketReadByte(struct object obj);
                    436: struct object KsocketWriteByte(struct object obj);
                    437:
                    438: struct object KsocketReadByte(struct object obj) {
                    439:   struct object ob1;
                    440:   struct object robj = NullObject;
                    441:   char data[2];
                    442:   char *tmp;
                    443:   char *r;
                    444:   int n;
                    445:   int socketid;
                    446:
                    447:
                    448:   if (obj.tag != Sarray) {
                    449:     errorMsg1s("KsocketReadByte([integer socketid])");
                    450:   }
                    451:   if (getoaSize(obj) < 1) {
                    452:     errorMsg1s("KsocketReadByte([integer socketid])");
                    453:   }
                    454:   ob1 = getoa(obj,0);
                    455:   if (ob1.tag != Sinteger) {
                    456:     errorMsg1s("KsocketReadByte([integer socketid]) : the argument must be integer.");
                    457:   }
                    458:   socketid = KopInteger(ob1);
                    459:
                    460:
                    461:   n = read(socketid,data,1);
                    462:   if (n < 0) {
                    463:     errorMsg1s("Read error.");
                    464:     robj = KpoInteger(-1);
                    465:     return(robj);
                    466:   }
                    467:   if (n == 0) {
                    468:     errorMsg1s("Read returned without data.");
                    469:   }
                    470:
                    471:   robj = KpoInteger((int)((unsigned char)data[0]));
                    472:   return(robj);
                    473:
                    474: }
                    475:
                    476: /* [ integer socketid, int ]
                    477:    integer  */
                    478: struct object KsocketWriteByte(struct object obj) {
                    479:   struct object ob1;
                    480:   struct object ob2;
                    481:   int socketid;
1.4       takayama  482:   int r,i,n,kk,r0;
                    483: #define DATA_SIZE 1024
                    484:   char data[DATA_SIZE];
1.1       maekawa   485:   if (obj.tag != Sarray) {
1.4       takayama  486:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   487:   }
                    488:   if (getoaSize(obj) < 2) {
1.4       takayama  489:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   490:   }
                    491:   ob1 = getoa(obj,0);
                    492:   ob2 = getoa(obj,1);
                    493:   if (ob1.tag != Sinteger) {
1.4       takayama  494:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer.");
1.1       maekawa   495:   }
                    496:   socketid = KopInteger(ob1);
1.4       takayama  497:   if (ob2.tag != Sinteger && ob2.tag != Sarray) {
                    498:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers.");
                    499:   }
                    500:   if (ob2.tag == Sinteger) {
                    501:     data[0] = KopInteger(ob2);
                    502:     r = write(socketid,data, 1);
                    503:   }else{
                    504:     n = getoaSize(ob2); kk = 0; r = 0;
                    505:     for (i=0; i<n; i++) {
                    506:       if (getoa(ob2,i).tag != Sinteger)
                    507:            errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers.");
                    508:       data[kk] = KopInteger(getoa(ob2,i));
                    509:       kk++;
                    510:       if (kk >= DATA_SIZE) {
                    511:        r0 = write(socketid,data,kk);
                    512:        if (r0 != kk) {
                    513:          fprintf(stderr,"Warning: Could not write to the socket.\n");
                    514:          return(KpoInteger(r+r0));
                    515:        }
                    516:         r += r0;
                    517:        kk = 0;
                    518:       }
                    519:     }
                    520:     if (kk > 0) {
                    521:       r0 = write(socketid,data,kk);
                    522:       if (r0 != kk) {
                    523:        fprintf(stderr,"Warning: Could not write to the socket.\n");
                    524:        return(KpoInteger(r+r0));
                    525:       }
                    526:       r += r0;
                    527:     }
1.1       maekawa   528:   }
                    529:   return(KpoInteger(r));
                    530: }
                    531:
1.5       takayama  532: struct object KsocketReadBlock(struct object socketObj) {
                    533:   /* Read until the empty line appears. */
                    534:   struct object ob;
                    535:   char *s;
                    536:   char *sss;
                    537:   char *tmp;
                    538:   int i;
                    539:   int flag;
                    540:   int datasize;
                    541:   ob = KsocketRead(socketObj);
                    542:   s = KopString(ob);
                    543:   flag = 0;
                    544:   for (i=strlen(s)-1; i>=0; i--) {
                    545:        if ((s[i] == '\n') && (i==0)) {
                    546:          flag = 1;
                    547:        }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
                    548:          flag = 1;
                    549:        }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) {
                    550:          flag = 1;
                    551:        }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
                    552:          flag = 1;
                    553:        }
                    554:   }
                    555:   if (flag == 1) return ob;
                    556:   datasize = strlen(s);
                    557:   sss = s;
                    558:
                    559:   while (flag == 0) {
                    560:        fprintf(stderr,"Waiting in socketReadBlock (spin lock to wait an empty line).\n");
                    561:        if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n");
                    562:        }else{
                    563:          /* for debugging. */
                    564:          for (i=0; i<strlen(sss); i++) {
                    565:                fprintf(stderr,"%3x",sss[i]);
                    566:          }
                    567:          fprintf(stderr,"\n");
                    568:        }
                    569:        sleep(2);
                    570:        ob = KsocketRead(socketObj);
                    571:        s = KopString(ob);
                    572:        flag = 0;
                    573:        for (i=strlen(s)-1; i>=0; i--) {
                    574:          if ((s[i] == '\n') && (i==0)) {
                    575:                flag = 1;
                    576:          }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
                    577:                flag = 1;
                    578:          }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) {
                    579:                flag = 1;
                    580:          }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
                    581:                flag = 1;
                    582:          }
                    583:        }
                    584:        if (datasize-1 <= strlen(sss)+strlen(s)) {
                    585:       tmp = (char *)GC_malloc(sizeof(char)*2*(datasize+strlen(s))+1);
                    586:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    587:          strcpy(tmp,sss);
                    588:          strcat(tmp,s);
                    589:       datasize = 2*(datasize+strlen(s));
                    590:          sss = tmp;
                    591:        }else{
                    592:          strcat(sss,s);
                    593:        }
                    594:   }
                    595:
                    596:   return KpoString(sss);
                    597:
                    598: }
                    599:
1.1       maekawa   600: struct object Kplugin_sm1Socket(char *key,struct object obj) {
                    601:   struct object robj = NullObject;
                    602:   if (strcmp(key,"open") == 0) {
                    603:     robj = KsocketOpen(obj);
                    604:   }else if (strcmp(key,"connect") == 0) {
                    605:     robj = KsocketConnect(obj);
                    606:   }else if (strcmp(key,"accept") == 0) {
                    607:     robj = KsocketAccept(obj);
                    608:   }else if (strcmp(key,"select") == 0) {
                    609:     robj = KsocketSelect(obj);
                    610:   }else if (strcmp(key,"mselect") == 0) {
                    611:     robj = KsocketSelectMulti(obj);
                    612:   }else if (strcmp(key,"read") == 0) {
                    613:     robj = KsocketRead(obj);
1.5       takayama  614:   }else if (strcmp(key,"readBlock") == 0) {
                    615:     robj = KsocketReadBlock(obj);
1.1       maekawa   616:   }else if (strcmp(key,"write") == 0) {
                    617:     robj = KsocketWrite(obj);
                    618:   }else if (strcmp(key,"read") == 0) {
                    619:     robj = KsocketRead(obj);
                    620:   }else if (strcmp(key,"readByte") == 0) {
                    621:     robj = KsocketReadByte(obj);
                    622:   }else if (strcmp(key,"writeByte") == 0) {
                    623:     robj = KsocketWriteByte(obj);
                    624:   }else if (strcmp(key,"close") == 0) {
                    625:     robj = KsocketClose(obj);
                    626:   }else {
                    627:     errorMsg1s("Unknown tag for sm1.socket");
                    628:   }
                    629:   return(robj);
                    630: }
                    631:
                    632:
                    633:
                    634:
                    635:
                    636:
                    637:
                    638:
                    639:
                    640:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>