00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdlib.h>
00027 #include "tclcl.h"
00028 #include "packet.h"
00029 #include "ip.h"
00030 #include "app.h"
00031 #include "tcp-simple.h"
00032
00033 static class SimpleTcpClass : public TclClass {
00034 public:
00035 SimpleTcpClass() : TclClass("Agent/TCP/SimpleTcp") {}
00036 TclObject* create(int, const char*const*) {
00037 return (new SimpleTcpAgent());
00038 }
00039 } class_simple_tcp_agent;
00040
00041 SimpleTcpAgent::SimpleTcpAgent() : TcpAgent(), seqno_(0)
00042 {
00043 }
00044
00045
00046 void SimpleTcpAgent::sendmsg(int bytes, const char* )
00047 {
00048 if (bytes == -1) {
00049 fprintf(stderr,
00050 "SimpleTcp doesn't support infinite send. Do not use FTP::start(), etc.\n");
00051 return;
00052 }
00053
00054 curseq_ += bytes;
00055 seqno_ ++;
00056
00057 Packet *p = allocpkt();
00058 hdr_tcp *tcph = HDR_TCP(p);
00059 tcph->seqno() = seqno_;
00060 tcph->ts() = Scheduler::instance().clock();
00061 tcph->ts_echo() = ts_peer_;
00062 hdr_cmn *th = HDR_CMN(p);
00063 th->size() = bytes + tcpip_base_hdr_size_;
00064 send(p, 0);
00065 }
00066
00067 void SimpleTcpAgent::recv(Packet *pkt, Handler *)
00068 {
00069 hdr_cmn *th = HDR_CMN(pkt);
00070 int datalen = th->size() - tcpip_base_hdr_size_;
00071 if (app_)
00072 app_->recv(datalen);
00073
00074 Packet::free(pkt);
00075 }
00076
00077 int SimpleTcpAgent::command(int argc, const char*const* argv)
00078 {
00079
00080
00081 if (argc == 2) {
00082 if (strcmp(argv[1], "listen") == 0) {
00083
00084 return (TCL_OK);
00085 }
00086 if (strcmp(argv[1], "close") == 0) {
00087
00088 Tcl::instance().evalf("%s done", name());
00089 return (TCL_OK);
00090 }
00091 }
00092 return (TcpAgent::command(argc, argv));
00093 }