comparison net.rhope @ 145:357f4ce3ca6d

Add incredibly ugly implementation of Read Delim to TCP Connection
author Mike Pavone <pavone@retrodev.com>
date Sun, 21 Nov 2010 22:08:17 -0500
parents 65ebd1ce2611
children f3686f60985d
comparison
equal deleted inserted replaced
144:65ebd1ce2611 145:357f4ce3ca6d
321 321
322 _Merge One Buf[inprog,src:out] 322 _Merge One Buf[inprog,src:out]
323 { 323 {
324 oldlen <- [inprog]Length 324 oldlen <- [inprog]Length
325 srclen <- [src]Length 325 srclen <- [src]Length
326 out <- [_internal_array_copychunk[src, 0, inprog, oldlen, srclen]]Length <<[[oldlen]+[srclen]] 326 If[srclen]
327 {
328 out <- [_internal_array_copychunk[src, 0, inprog, oldlen, srclen]]Length <<[[oldlen]+[srclen]]
329 }{
330 out <- inprog
331 }
327 } 332 }
328 333
329 _Merge Buffers[buflist:out] 334 _Merge Buffers[buflist:out]
330 { 335 {
331 If[[[buflist]Length]=[1]] 336 If[[[buflist]Length]=[1]]
332 { 337 {
333 out <- [buflist]Index[0] 338 out <- [buflist]Index[0]{}
339 { Print["This shouldn't happen!!!!"] }
334 }{ 340 }{
335 len <- Fold[_Add Len[?], 0, buflist] 341 len <- Fold[_Add Len[?], 0, buflist]
336 out <- Fold[_Merge One Buf[?], _internal_array_allocnaked[len, [[buflist]Index[0]]Eltype >>], buflist] 342 out <- Fold[_Merge One Buf[?], _internal_array_allocnaked[len, UInt8()], buflist]
337 } 343 }
338 } 344 }
339 345
340 Read@TCP Connection[con,toread(Int32):data,out con,err] 346 Read@TCP Connection[con,toread(Int32):data,out con,err]
341 { 347 {
362 { data <- _Merge Buffers[~] } 368 { data <- _Merge Buffers[~] }
363 } 369 }
364 } 370 }
365 } 371 }
366 372
373 _Check Partial[buffer,delim,offset:partial,none]
374 {
375 none <- If[[offset]=[[buffer]Length]] {}
376 {
377 left <- [[buffer]Length]-[offset]
378 If[_internal_memcmp_offset[buffer, offset, delim, 0, left]]
379 {
380 partial,none <- _Check Partial[buffer,delim,[offset]+[1]]
381 }{
382 partial <- offset
383 }
384 }
385 }
386
387 _Check Buffer[buffer,delim,offset:out,partial,none]
388 {
389 dlen <- [delim]Length
390 blen <- [buffer]Length
391 If[[[offset]+[dlen]]<=[blen]]
392 {
393 If[_internal_memcmp_offset[buffer, offset, delim, 0, dlen]]
394 {
395 out,partial,none <- _Check Buffer[buffer,delim,[offset]+[1]]
396 }{
397 out <- offset
398 }
399 }{
400 partial,none <- _Check Partial[buffer,delim,offset]
401 }
402 }
403
404 _Check Split[old,new,delim,offset:out,not split]
405 {
406 ,not split <- If[[offset]<[[old]Length]]
407 {
408 left <- [[old]Length]-[offset]
409 If[_internal_memcmp_offset[old, offset, delim, 0, left]]
410 {
411 next <- [offset]+[1]
412 }{
413 If[_internal_memcmp_offset[new, 0, delim, left, [[delim]Length]-[left]]]
414 {
415 next <- [offset]+[1]
416 }{
417 out <- offset
418 }
419 }
420 out,not split <- _Check Split[old,new,delim,next]
421 }
422 }
423
424 _Read Delim@TCP Connection[con,delim,poffset,buflist:data,out con,err]
425 {
426 read[[con]Filedes >>, _internal_array_allocnaked[512i32, UInt8()], 512i64]
427 { numread <- Trunc Int32[~] }
428 { outbuf <- [~]Length <<[numread] }
429
430 If[[numread]=[-1]]
431 {
432 If[Wait for IO[[con]Filedes >>, 1]]
433 {
434 data,out con,err <- [con]_Read Delim[delim,poffset,buflist]
435 }{
436 err <- [Fold[_Add Len[?], 0, buflist]]+[Length[[con]Buffer >>]]
437 }
438 }{
439 ,checknew <- If[[poffset]>=[0]]
440 {
441 If[[[delim]Length]>[[outbuf]Length]]
442 {
443 //Avoid possibility of having to check across more than 2 buffers
444 data,out con,err <-[[con]Buffer <<[
445 _Merge Buffers[[[buflist]Append[[con]Buffer >>]]Append[outbuf]]
446 ]
447 ]Read Delim[delim]
448 }{
449 ,checknew <- _Check Split[[con]Buffer >>, outbuf, delim ,poffset]
450 {
451 before <- [_internal_array_copychunk[[con]Read Buffer >>, 0, _internal_array_allocnaked[~, UInt8()], 0, ~]
452 ]Length <<[~]
453
454 data <- _Merge Buffers[[buflist]Append[before]]
455 after off <- [[delim]Length]- [[[[con]Buffer >>]Length]-[~]]
456 rbufferlen <- [[outbuf]Length]-[after off]
457 out con <- [con]Read Buffer <<[
458 [_internal_array_copychunk[outbuf, after off,
459 _internal_array_allocnaked[rbufferlen, UInt8()], 0, rbufferlen]
460 ]Length <<[rbufferlen]
461 ]
462 }
463 }
464 }
465 Val[checknew]
466 {
467 ,npoffset <- _Check Buffer[outbuf,delim,0]
468 {
469 before <- [_internal_array_copychunk[outbuf, 0, _internal_array_allocnaked[~, UInt8()], 0, ~]
470 ]Length <<[~]
471 If[[[con]Read Buffer >>]Length]
472 {
473
474 tomerge <- [[buflist]Append[[con]Read Buffer >>]]Append[before]
475 }{
476 tomerge <- [buflist]Append[before]
477 }
478 data <- _Merge Buffers[tomerge]
479 rbufferlen <- [[outbuf]Length]-[[~]+[[delim]Length]]
480 out con <- [con]Read Buffer <<[
481 [_internal_array_copychunk[outbuf, [~]+[[delim]Length],
482 _internal_array_allocnaked[rbufferlen, UInt8()], 0, rbufferlen]
483 ]Length <<[rbufferlen]
484 ]
485 }{
486
487 If[[[con]Read Buffer >>]Length]
488 {
489 nbuflist <- [buflist]Append[[con]Read Buffer >>]
490 }{
491 nbuflist <- Val[buflist]
492 }
493 ncon <- [con]Read Buffer <<[outbuf]
494 }{
495 If[[[con]Read Buffer >>]Length]
496 {
497 nbuflist <- [[buflist]Append[[con]Read Buffer >>]]Append[outbuf]
498 ncon <- [con]Read Buffer <<[Array[]]
499 }{
500 nbuflist <- [buflist]Append[outbuf]
501 ncon <- Val[con]
502 }
503 }
504 }
505 data,out con,err <- [ncon]_Read Delim[delim,npoffset,nbuflist]
506 }
507 }
508
509 Read Delim@TCP Connection[con,delim(Array):data,out con,err]
510 {
511 ,poffset <-_Check Buffer[[con]Read Buffer >>, delim,0]
512 {
513 data <- [_internal_array_copychunk[[con]Read Buffer >>, 0, _internal_array_allocnaked[~, UInt8()], 0, ~]]Length <<[~]
514 rbufferlen <- [[[con]Read Buffer >>]Length]-[[~]+[[delim]Length]]
515 out con <- [con]Read Buffer <<[
516 [_internal_array_copychunk[[con]Read Buffer >>, [~]+[[delim]Length],
517 _internal_array_allocnaked[rbufferlen, UInt8()], 0, rbufferlen]
518 ]Length <<[rbufferlen]
519 ]
520 }{
521 buflist <- ()
522 ncon <- Val[con]
523 }{
524 If[[[con]Read Buffer >>]Length]
525 {
526 ncon <- [con]Read Buffer <<[Array[]]
527 buflist <- [()]Append[[con]Read Buffer >>]
528 }{
529 ncon <- Val[con]
530 buflist <- ()
531 }
532 poffset <- -1
533
534 }
535 data,out con,err <- [ncon]_Read Delim[delim,poffset,buflist]
536 }
537
367 Close@TCP Connection[con:out] 538 Close@TCP Connection[con:out]
368 { 539 {
369 out <- close[[con]Filedes >>] 540 out <- close[[con]Filedes >>]
370 } 541 }
371 542