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

📄 uip-doc.txt

📁 uIP0.9版本
💻 TXT
📖 第 1 页 / 共 3 页
字号:
\codevoid example4_init(void) {   u16_t ipaddr[2];   uip_ipaddr(ipaddr, 192,168,0,1);   uip_connect(ipaddr, HTONS(80));}void example4_app(void) {   if(uip_connected() || uip_rexmit()) {      uip_send("GET /file HTTP/1.0\r\nServer:192.186.0.1\r\n\r\n",               48);      return;   }   if(uip_newdata()) {      device_enqueue(uip_appdata, uip_datalen());      if(device_queue_full()) {         uip_stop();      }   }   if(uip_poll() && uip_stopped()) {      if(!device_queue_full()) {         uip_restart();      }   }}\endcodeWhen the connection has been established, an HTTP request is sent tothe server. Since this is the only data that is sent, the applicationknows that if it needs to retransmit any data, it is that request thatshould be retransmitted. It is therefore possible to combine these twoevents as is done in the example.When the application receives new data from the remote host, it sendsthis data to the device by using the function device_enqueue(). It isimportant to note that this example assumes that this function copiesthe data into its own buffers. The data in the uip_appdata buffer willbe overwritten by the next incoming packet.If the device's queue is full, the application stops the data from theremote host by calling the uIP function uip_stop(). The applicationcan then be sure that it will not receive any new data untiluip_restart() is called. The application polling event is used tocheck if the device's queue is no longer full and if so, the data flowis restarted with uip_restart().\subsection example5 A simple web serverThis example shows a very simple file server application that listensto two ports and uses the port number to determine which file tosend. If the files are properly formatted, this simple application canbe used as a web server with static pages. The implementation follows.\codestruct example5_state {   char *dataptr;   unsigned int dataleft;};void example5_init(void) {   uip_listen(HTONS(80));   uip_listen(HTONS(81));}void example5_app(void) {   struct example5_state *s;   s = (struct example5_state)uip_conn->appstate;      if(uip_connected()) {      switch(uip_conn->lport) {      case HTONS(80):         s->dataptr = data_port_80;         s->dataleft = datalen_port_80;         break;      case HTONS(81):         s->dataptr = data_port_81;         s->dataleft = datalen_port_81;         break;      }      uip_send(s->dataptr, s->dataleft);      return;         }   if(uip_acked()) {      if(s->dataleft < uip_mss()) {         uip_close();         return;      }      s->dataptr += uip_conn->len;      s->dataleft -= uip_conn->len;      uip_send(s->dataptr, s->dataleft);         }}\endcodeThe application state consists of a pointer to the data that should besent and the size of the data that is left to send. When a remote hostconnects to the application, the local port number is used todetermine which file to send. The first chunk of data is sent usinguip_send(). uIP makes sure that no more than MSS bytes of data isactually sent, even though s->dataleft may be larger than the MSS. The application is driven by incoming acknowledgments. When data hasbeen acknowledged, new data can be sent. If there is no more data tosend, the connection is closed using uip_close().\subsection example6 Structured application program designWhen writing larger programs using uIP it is useful to be able toutilize the uIP API in a structured way. The following exampleprovides a structured design that has showed itself to be useful forwriting larger protocol implementations than the previous examplesshowed here. The program is divided into an uIP event handler functionthat calls seven application handler functions that process new data,act on acknowledged data, send new data, deal with connectionestablishment or closure events and handle errors. The functions arecalled newdata(), acked(), senddata(), connected(), closed(),aborted(), and timedout(), and needs to be written specifically forthe protocol that is being implemented.The uIP event handler function is shown below.\codevoid example6_app(void) {  if(uip_aborted()) {    aborted();  }  if(uip_timedout()) {    timedout();  }  if(uip_closed()) {    closed();  }  if(uip_connected()) {    connected();  }  if(uip_acked()) {    acked();  }  if(uip_newdata()) {    newdata();  }  if(uip_rexmit() ||     uip_newdata() ||     uip_acked() ||     uip_connected() ||     uip_poll()) {    senddata();  }}\endcodeThe function starts with dealing with any error conditions that mighthave happened by checking if uip_aborted() or uip_timedout() aretrue. If so, the appropriate error function is called. Also, if theconnection has been closed, the closed() function is called to the itdeal with the event.Next, the function checks if the connection has just been establishedby checking if uip_connected() is true. The connected() function iscalled and is supposed to do whatever needs to be done when theconnection is established, such as intializing the application statefor the connection. Since it may be the case that data should be sentout, the senddata() function is called to deal with the outgoing data.The following very simple application serves as an example of how theapplication handler functions might look. This application simplywaits for any data to arrive on the connection, and responds to thedata by sending out the message "Hello world!". To illustrate how todevelop an application state machine, this message is sent in twoparts, first the "Hello" part and then the "world!" part.\code#define STATE_WAITING 0#define STATE_HELLO   1#define STATE_WORLD   2struct example6_state {  u8_t state;  char *textptr;  int  textlen;};static void aborted(void) {}static void timedout(void) {}static void closed(void) {}static void connected(void) {  struct example6_state *s = (struct example6_state *)uip_conn->appstate;  s->state   = STATE_WAITING;  s->textlen = 0;}static void newdata(void) {  struct example6_state *s = (struct example6_state *)uip_conn->appstate;  if(s->state == STATE_WAITING) {    s->state   = STATE_HELLO;    s->textptr = "Hello ";    s->textlen = 6;  }}static void acked(void) {  struct example6_state *s = (struct example6_state *)uip_conn->appstate;    s->textlen -= uip_conn->len;  s->textptr += uip_conn->len;  if(s->textlen == 0) {    switch(s->state) {    case STATE_HELLO:      s->state   = STATE_WORLD;      s->textptr = "world!\n";      s->textlen = 7;      break;    case STATE_WORLD:      uip_close();      break;    }  }}static void senddata(void) {  struct example6_state *s = (struct example6_state *)uip_conn->appstate;  if(s->textlen > 0) {    uip_send(s->textptr, s->textlen);  }}\endcodeThe application state consists of a "state" variable, a "textptr"pointer to a text message and the "textlen" length of the textmessage. The "state" variable can be either "STATE_WAITING", meaningthat the application is waiting for data to arrive from the network,"STATE_HELLO", in which the application is sending the "Hello" part ofthe message, or "STATE_WORLD", in which the application is sending the"world!" message. The application does not handle errors or connection closing events,and therefore the aborted(), timedout() and closed() functions areimplemented as empty functions.The connected() function will be called when a connection has beenestablished, and in this case sets the "state" variable to be"STATE_WAITING" and the "textlen" variable to be zero, indicating thatthere is no message to be sent out.When new data arrives from the network, the newdata() function will becalled by the event handler function. The newdata() function willcheck if the connection is in the "STATE_WAITING" state, and if soswitches to the "STATE_HELLO" state and registers a 6 byte long "Hello" message with the connection. This message will later be sent out bythe senddata() function.The acked() function is called whenever data that previously was senthas been acknowleged by the receiving host. This acked() functionfirst reduces the amount of data that is left to send, by subtractingthe length of the previously sent data (obtained from "uip_conn->len")from the "textlen" variable, and also adjusts the "textptr" pointeraccordingly. It then checks if the "textlen" variable now is zero,which indicates that all data now has been successfully received, andif so changes application state. If the application was in the"STATE_HELLO" state, it switches state to "STATE_WORLD" and sets up a7 byte "world!\n" message to be sent. If the application was in the"STATE_WORLD" state, it closes the connection.Finally, the senddata() function takes care of actually sending thedata that is to be sent. It is called by the event handler functionwhen new data has been received, when data has been acknowledged, whena new connection has been established, when the connection is polledbecause of inactivity, or when a retransmission should be made. Thepurpose of the senddata() function is to optionally format the datathat is to be sent, and to call the uip_send() function to actuallysend out the data. In this particular example, the function simplycalls uip_send() with the appropriate arguments if data is to be sent,after checking if data should be sent out or not as indicated by the"textlen" variable.It is important to note that the senddata() function never shouldaffect the application state; this should only be done in the acked()and newdata() functions.*//** @} *//**\defgroup exampleapps Example applications@{The uIP distribution contains a number of example applications thatcan be either used directory or studied when learning to developapplications for uIP.*//** @} */

⌨️ 快捷键说明

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