⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uip-doc.txt

📁 uIP0.9版本
💻 TXT
📖 第 1 页 / 共 3 页
字号:
application might want to abort the connection and does so by callingthe uip_abort() function.If the connection has been closed by the remote end, the test functionuip_closed() is true. The application may then do any necessarycleanups.\subsection errors Reporting errorsThere are two fatal errors that can happen to a connection, eitherthat the connection was aborted by the remote host, or that theconnection retransmitted the last data too many times and has beenaborted. uIP reports this by calling the application function. Theapplication can use the two test functions uip_aborted() anduip_timedout() to test for those error conditions.\subsection polling PollingWhen a connection is idle, uIP polls the application every time theperiodic timer fires. The application uses the test functionuip_poll() to check if it is being polled by uIP.The polling event has two purposes. The first is to let theapplication periodically know that a connection is idle, which allowsthe application to close connections that have been idle for toolong. The other purpose is to let the application send new data thathas been produced. The application can only send data when invoked byuIP, and therefore the poll event is the only way to send data on anotherwise idle connection.\subsection listen Listening portsuIP maintains a list of listening TCP ports. A new port is opened forlistening with the uip_listen() function. When a connection requestarrives on a listening port, uIP creates a new connection and callsthe application function. The test function uip_connected() is true ifthe application was invoked because a new connection was created.The application can check the lport field in the uip_conn structure tocheck to which port the new connection was connected.\subsection connect Opening connectionsNew connections can be opened from withinuIP by the function uip_connect(). This functionallocates a new connection and sets a flag in the connection statewhich will open a TCP connection to the specified IP address and portthe next time the connection is polled by uIP. The uip_connect()function returnsa pointer to the uip_conn structure for the newconnection. If there are no free connection slots, the functionreturns NULL. The function uip_ipaddr() may be used to pack an IP address into thetwo element 16-bit array used by uIP to represent IP addresses.Two examples of usage are shown below. The first example shows how toopen a connection to TCP port 8080 of the remote end of the currentconnection. If there are not enough TCP connection slots to allow anew connection to be opened, the uip_connect() function returns NULLand the current connection is aborted by uip_abort(). \codevoid connect_example1_app(void) {   if(uip_connect(uip_conn->ripaddr, HTONS(8080)) == NULL) {      uip_abort();   }}   \endcodeThe second example shows how to open a new connection to a specific IPaddress. No error checks are made in this example.\codevoid connect_example2(void) {   u16_t ipaddr[2];   uip_ipaddr(ipaddr, 192,168,0,1);   uip_connect(ipaddr, HTONS(8080));}\endcode\section drivers uIP device driversFrom the network device driver's standpoint, uIP consists of two Cfunctions: uip_input() and uip_periodic(). The uip_input() functionshould be called by the device driver when an IP packet has beenreceived and put into the uip_buf packet buffer. The uip_input()function will process the packet, and when it returns an outboundpacket may have been placed in the same uip_buf packet buffer(indicated by the uip_len variable being non-zero). The device drivershould then send out this packet onto the network.The uip_periodic() function should be invoked periodically once perconnection by the device driver, typically one per second. Thisfunction is used by uIP to drive protocol timers and retransmissions,and when it returns it may have placed an outbound packet in theuip_buf buffer.\section arch Architecture specific functionsuIP requires a few functions to be implemented specifically for thearchitecture on which uIP is intended to run. These functions shouldbe hand-tuned for the particular architecture, but generic Cimplementations are given as part of the uIP distribution.\subsection checksums Checksum calculationThe TCP and IP protocols implement a checksum that covers the data andheader portions of the TCP and IP packets. Since the calculation ofthis checksum is made over all bytes in every packet being sent andreceived it is important that the function that calculates thechecksum is efficient. Most often, this means that the checksumcalculation must be fine-tuned for the particular architecture onwhich the uIP stack runs.Because of this, uIP does not implement a generic checksum function,but leaves this to the architecture specific files which mustimplement the two functions uip_ipchksum() and uip_tcpchksum(). Thechecksum calculations in those functions can be written in highlyoptimized assembler rather than generic C code.An example C implementation of the checksum function is provided inthe uIP distribution.\subsection longarith 32-bit arithmeticThe TCP protocol uses 32-bit sequence numbers, and a TCPimplementation will have to do a number of 32-bit additions as part ofthe normal protocol processing. Since 32-bit arithmetic is notnatively available on many of the platforms for which uIP is intended,uIP leaves the 32-bit additions to be implemented by the architecturespecific module and does not make use of any 32-bit arithmetic in themain code base.The architecture specific code must implement a function uip_add32()which does a 32-bit addition and stores the result in a globalvariable uip_acc32. \section examples ExamplesThis section presents a number of very simple uIP applications. TheuIP code distribution contains several more complex applications.\subsection example1 A very simple applicationThis first example shows a very simple application. The applicationlistens for incoming connections on port 1234. When a connection hasbeen established, the application replies to all data sent to it bysaying "ok"The implementation of this application is shown below. The applicationis initialized with the function called example1_init() and the uIPcallback function is called example1_app(). For this application, theconfiguration variable UIP_APPCALL should be defined to beexample1_app().\codevoid example1_init(void) {   uip_listen(HTONS(1234));}void example1_app(void) {   if(uip_newdata() || uip_rexmit()) {      uip_send("ok\n", 3);   }}\endcodeThe initialization function calls the uIP function uip_listen() toregister a listening port. The actual application functionexample1_app() uses the test functions uip_newdata() and uip_rexmit()to determine why it was called. If the application was called becausethe remote end has sent it data, it responds with an "ok". If theapplication function was called because data was lost in the networkand has to be retransmitted, it also sends an "ok".  Note that thisexample actually shows a complete uIP application. It is not requiredfor an application to deal with all types of events such asuip_connected() or uip_timedout(). \subsection example2 A more advanced applicationThis second example is slightly more advanced than the previous one,and shows how the application state field in the uip_conn structure isused.This application is similar to the first application in that itlistens to a port for incoming connections and responds to data sentto it with a single "ok". The big difference is that this applicationprints out a welcoming "Welcome!" message when the connection has beenestablished.This seemingly small change of operation makes a big difference in howthe application is implemented. The reason for the increase incomplexity is that if data should be lost in the network, theapplication must know what data to retransmit. If the "Welcome!"message was lost, the application must retransmit the welcome and ifone of the "ok" messages is lost, the application must send a new"ok".The application knows that as long as the "Welcome!" message has notbeen acknowledged by the remote host, it might have been dropped inthe network. But once the remote host has sent an acknowledgmentback, the application can be sure that the welcome has been receivedand knows that any lost data must be an "ok" message. Thus theapplication can be in either of two states: either in the WELCOME-SENTstate where the "Welcome!" has been sent but not acknowledged, or inthe WELCOME-ACKED state where the "Welcome!" has been acknowledged.When a remote host connects to the application, the application sendsthe "Welcome!" message and sets it's state to WELCOME-SENT. When thewelcome message is acknowledged, the application moves to theWELCOME-ACKED state. If the application receives any new data from theremote host, it responds by sending an "ok" back.If the application is requested to retransmit the last message, itlooks at in which state the application is. If the application is inthe WELCOME-SENT state, it sends a "Welcome!"  message since itknows that the previous welcome message hasn't been acknowledged. Ifthe application is in the WELCOME-ACKED state, it knows that the lastmessage was an "ok" message and sends such a message.The implementation of this application is seen below. Thisconfiguration settings for the application is follows after itsimplementation.\codestruct example2_state {   enum {WELCOME_SENT, WELCOME_ACKED} state;};void example2_init(void) {   uip_listen(HTONS(2345));}void example2_app(void) {   struct example2_state *s;   s = (struct example2_state *)uip_conn->appstate;      if(uip_connected()) {      s->state = WELCOME_SENT;      uip_send("Welcome!\n", 9);      return;   }    if(uip_acked() && s->state == WELCOME_SENT) {      s->state = WELCOME_ACKED;   }   if(uip_newdata()) {      uip_send("ok\n", 3);   }   if(uip_rexmit()) {      switch(s->state) {      case WELCOME_SENT:         uip_send("Welcome!\n", 9);         break;      case WELCOME_ACKED:         uip_send("ok\n", 3);         break;      }   }}\endcodeThe configuration for the application:\code#define UIP_APPCALL       example2_app#define UIP_APPSTATE_SIZE sizeof(struct example2_state)\endcode\subsection example3 Differentiating between applicationsIf the system should run multiple applications, one technique todifferentiate between them is to use the TCP port number of either theremote end or the local end of the connection. The example below showshow the two examples above can be combined into one application.\codevoid example3_init(void) {   example1_init();   example2_init();   }void example3_app(void) {   switch(uip_conn->lport) {   case HTONS(1234):      example1_app();      break;   case HTONS(2345):      example2_app();      break;   }}\endcode\subsection example4 Utilizing TCP flow controlThis example shows a simple application that connects to a host, sendsan HTTP request for a file and downloads it to a slow device such adisk drive. This shows how to use the flow control functions of uIP.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -