📄 tcp.lst
字号:
458 3 // them, do not have to send them
459 3 // After sending the SYN ACK the client browser will
460 3 // blast me with 2 messages, an ACK, and a HTTP GET
461 3 tcp_send(FLG_SYN | FLG_ACK, 28, nr);
462 3
463 3 // My SYN flag increments my sequence number
464 3 // My sequence number is always updated to point to
465 3 // the next byte to be sent. So the incoming ack
466 3 // number should equal my sequence number
467 3 conxn[nr].my_sequence++;
468 3
469 3 conxn[nr].state = STATE_SYN_RCVD;
470 3 if (debug) serial_send("TCP: Entered SYN RCVD state\r");
471 3 }
472 2 else
473 2 {
474 3 // Sender is out of sync so send reset
475 3 conxn[nr].ipaddr = 0;
476 3 tcp_send(FLG_RST, 20, NO_CONNECTION);
477 3 }
478 2 break;
479 2
480 2
481 2 case STATE_SYN_RCVD:
482 2 // He may already be sending me data - should process it
483 2 conxn[nr].his_sequence += data_len;
484 2 conxn[nr].his_ack = tcp->ack_number;
485 2
486 2 if (tcp->flags & FLG_FIN)
487 2 {
488 3 // His FIN counts as a byte of data
489 3 conxn[nr].his_sequence++;
C51 COMPILER V7.09 TCP 07/07/2004 14:57:21 PAGE 9
490 3 tcp_send(FLG_ACK, 20, nr);
491 3 conxn[nr].state = STATE_CLOSE_WAIT;
492 3 if (debug) serial_send("TCP: Entered CLOSE_WAIT state\r");
493 3
494 3 // At this point we would normally wait for the application
495 3 // to close. For now, send FIN right away.
496 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
497 3 conxn[nr].my_sequence++; // For my FIN
498 3 conxn[nr].state = STATE_LAST_ACK;
499 3 if (debug) serial_send("TCP: Entered LAST ACK state\r");
500 3 }
501 2
502 2 // Make sure he is ACKing my SYN
503 2 else if (tcp->ack_number == conxn[nr].my_sequence)
504 2 {
505 3 conxn[nr].state = STATE_ESTABLISHED;
506 3 if (debug) serial_send("TCP: Entered ESTABLISHED state\r");
507 3 // If sender sent data ignore it and he will resend
508 3 // Do not send response because we received no
509 3 // data... wait for client to send something to me
510 3 }
511 2 break;
512 2
513 2
514 2 case STATE_ESTABLISHED:
515 2 conxn[nr].his_ack = tcp->ack_number;
516 2
517 2 if (tcp->flags & FLG_FIN)
518 2 {
519 3 // His FIN counts as a byte of data
520 3 conxn[nr].his_sequence++;
521 3 tcp_send(FLG_ACK, 20, nr);
522 3 conxn[nr].state = STATE_CLOSE_WAIT;
523 3 if (debug) serial_send("TCP: Entered CLOSE_WAIT state\r");
524 3
525 3 // At this point we would normally wait for the application
526 3 // to close. For now, send FIN immediately.
527 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
528 3 conxn[nr].my_sequence++; // For my FIN
529 3 conxn[nr].state = STATE_LAST_ACK;
530 3 if (debug) serial_send("TCP: Entered LAST ACK state\r");
531 3 }
532 2 else if (data_len != 0)
533 2 {
534 3 // Received normal TCP segment from sender with data
535 3 // Send an ACK immediately and pass the data on to
536 3 // the application
537 3 conxn[nr].his_sequence += data_len;
538 3 tcp_send(FLG_ACK, 20, nr); // Send ACK
539 3
540 3
541 3 // Send pointer to start of TCP payload
542 3 // http_server increments my sequence number when
543 3 // sending so don't worry about it here
544 3 result = http_server(inbuf, header_len, nr, 0);
545 3
546 3 // Start timer to close conxn if no activity
547 3 conxn[nr].inactivity = INACTIVITY_TIME;
548 3 }
549 2 break;
550 2
551 2
C51 COMPILER V7.09 TCP 07/07/2004 14:57:21 PAGE 10
552 2 case STATE_CLOSE_WAIT:
553 2 // With this code, should not get here
554 2 if (debug) serial_send("TCP: Oops! Rcvd unexpected message\r");
555 2
556 2 break;
557 2
558 2
559 2 case STATE_LAST_ACK:
560 2 conxn[nr].his_ack = tcp->ack_number;
561 2
562 2 // If he ACK's my FIN then close
563 2 if (tcp->ack_number == conxn[nr].my_sequence)
564 2 {
565 3 conxn[nr].state = STATE_CLOSED;
566 3 conxn[nr].ipaddr = 0; // Free up struct area
567 3 just_closed = TRUE;
568 3 }
569 2 break;
570 2
571 2
572 2 case STATE_FIN_WAIT_1:
573 2 // He may still be sending me data - should process it
574 2 conxn[nr].his_sequence += data_len;
575 2 conxn[nr].his_ack = tcp->ack_number;
576 2
577 2 if (tcp->flags & FLG_FIN)
578 2 {
579 3 // His FIN counts as a byte of data
580 3 conxn[nr].his_sequence++;
581 3 tcp_send(FLG_ACK, 20, nr);
582 3
583 3 // If he has ACK'd my FIN then we can close connection
584 3 if (tcp->ack_number == conxn[nr].my_sequence)
585 3 {
586 4 conxn[nr].state = STATE_TIME_WAIT;
587 4 if (debug) serial_send("TCP: Entered TIME_WAIT state\r");
588 4
589 4 conxn[nr].state = STATE_CLOSED;
590 4 conxn[nr].ipaddr = 0; // Free up connection
591 4 just_closed = TRUE;
592 4 }
593 3 else
594 3 {
595 4 // He has not ACK'd my FIN. This happens when there is a simultaneous
596 4 // close - I got his FIN but he has not yet ACK'd my FIN
597 4 conxn[nr].state = STATE_CLOSING;
598 4 if (debug) serial_send("TCP: Entered CLOSING state\r");
599 4 }
600 3 }
601 2 else if (tcp->ack_number == conxn[nr].my_sequence)
602 2 {
603 3 // He has ACK'd my FIN but has not sent a FIN yet himself
604 3 conxn[nr].state = STATE_FIN_WAIT_2;
605 3 if (debug) serial_send("TCP: Entered FIN_WAIT_2 state\r");
606 3 }
607 2 break;
608 2
609 2
610 2 case STATE_FIN_WAIT_2:
611 2 // He may still be sending me data - should process it
612 2 conxn[nr].his_sequence += data_len;
613 2 conxn[nr].his_ack = tcp->ack_number;
C51 COMPILER V7.09 TCP 07/07/2004 14:57:21 PAGE 11
614 2
615 2 if (tcp->flags & FLG_FIN)
616 2 {
617 3 conxn[nr].his_sequence++; // For his FIN flag
618 3 tcp_send(FLG_ACK, 20, nr);
619 3 conxn[nr].state = STATE_TIME_WAIT;
620 3 if (debug) serial_send("TCP: Entered TIME_WAIT state\r");
621 3 conxn[nr].state = STATE_CLOSED;
622 3 conxn[nr].ipaddr = 0; // Free up struct area
623 3 just_closed = TRUE;
624 3 }
625 2 break;
626 2
627 2
628 2 case STATE_TIME_WAIT:
629 2 // With this code, should not get here
630 2 if (debug) serial_send("TCP: Oops! In TIME_WAIT state\r");
631 2 break;
632 2
633 2
634 2 case STATE_CLOSING:
635 2 // Simultaneous close has happened. I have received his FIN
636 2 // but he has not yet ACK'd my FIN. Waiting for ACK.
637 2 // Will not receive data in this state
638 2 conxn[nr].his_ack = tcp->ack_number;
639 2
640 2 if (tcp->ack_number == conxn[nr].my_sequence)
641 2 {
642 3 conxn[nr].state = STATE_TIME_WAIT;
643 3 if (debug) serial_send("TCP: Entered TIME_WAIT state\r");
644 3
645 3 // Do not send any response to his ACK
646 3 conxn[nr].state = STATE_CLOSED;
647 3 conxn[nr].ipaddr = 0; // Free up struct area
648 3 just_closed = TRUE;
649 3 }
650 2 break;
651 2
652 2
653 2 default:
654 2 if (debug) serial_send("TCP: Error, no handler\r");
655 2 break;
656 2 }
657 1
658 1 // This is for debug, to see when conxn closes
659 1 if (just_closed)
660 1 {
661 2 just_closed = FALSE;
662 2 if (debug)
663 2 {
664 3 serial_send("TCP: Closed connection ");
665 3 memset(text, 0, 10);
666 3 itoa((UINT)nr, text, 10);
667 3 serial_send(text);
668 3 serial_send("\r");
669 3 }
670 2 }
671 1 }
672
673
674
C51 COMPILER V7.09 TCP 07/07/2004 14:57:21 PAGE 12
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2973 ----
CONSTANT SIZE = 838 ----
XDATA SIZE = 231 ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 19
IDATA SIZE = 6 25
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -