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

📄 main.html

📁 最新的免费嵌入式系统TCP/IP协议栈!
💻 HTML
📖 第 1 页 / 共 5 页
字号:
      <span class="keywordflow">case</span> <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(81):         s-&gt;dataptr = data_port_81;         s-&gt;dataleft = datalen_port_81;         <span class="keywordflow">break</span>;      }      <a class="code" href="a00147.html#g04b053a623aac7cd4195157d470661b3">uip_send</a>(s-&gt;dataptr, s-&gt;dataleft);      <span class="keywordflow">return</span>;         }   <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gde6634974418e3240c212b9b16864368">uip_acked</a>()) {      <span class="keywordflow">if</span>(s-&gt;dataleft &lt; <a class="code" href="a00147.html#gb5fecbc62edd128012cea0f47b57ab9f">uip_mss</a>()) {         <a class="code" href="a00147.html#g61db1dcb7c760e4dd5d60bf4e5576dca">uip_close</a>();         <span class="keywordflow">return</span>;      }      s-&gt;dataptr += <a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a class="code" href="a00088.html#0ef3ae2764714bf90620075c374c262e">len</a>;      s-&gt;dataleft -= <a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a class="code" href="a00088.html#0ef3ae2764714bf90620075c374c262e">len</a>;      <a class="code" href="a00147.html#g04b053a623aac7cd4195157d470661b3">uip_send</a>(s-&gt;dataptr, s-&gt;dataleft);         }}</pre></div><p>The application state consists of a pointer to the data that should be sent and the size of the data that is left to send. When a remote host connects to the application, the local port number is used to determine which file to send. The first chunk of data is sent using <a class="el" href="a00147.html#g04b053a623aac7cd4195157d470661b3">uip_send()</a>. uIP makes sure that no more than MSS bytes of data is actually sent, even though s-&gt;dataleft may be larger than the MSS.<p>The application is driven by incoming acknowledgments. When data has been acknowledged, new data can be sent. If there is no more data to send, the connection is closed using <a class="el" href="a00147.html#g61db1dcb7c760e4dd5d60bf4e5576dca">uip_close()</a>.<h3><a class="anchor" name="example6">Structured Application Program Design</a></h3>When writing larger programs using uIP it is useful to be able to utilize the uIP API in a structured way. The following example provides a structured design that has showed itself to be useful for writing larger protocol implementations than the previous examples showed here. The program is divided into an uIP event handler function that calls seven application handler functions that process new data, act on acknowledged data, send new data, deal with connection establishment or closure events and handle errors. The functions are called newdata(), acked(), senddata(), connected(), closed(), aborted(), and timedout(), and needs to be written specifically for the protocol that is being implemented.<p>The uIP event handler function is shown below.<p><div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> example6_app(<span class="keywordtype">void</span>) {  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gfbd5fc486dfdf6bf6fc9db52b1f418c4">uip_aborted</a>()) {    aborted();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#g7b2ac4b18bd2ac3912fe67b3b17158c3">uip_timedout</a>()) {    timedout();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gef6c4140c632b6a406779342cf3b6eb6">uip_closed</a>()) {    closed();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>()) {    connected();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gde6634974418e3240c212b9b16864368">uip_acked</a>()) {    acked();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#g26a14b8dae3f861830af9e7cf1e03725">uip_newdata</a>()) {    newdata();  }  <span class="keywordflow">if</span>(<a class="code" href="a00147.html#ga8933ad15a2e2947dae4a5cff50e6007">uip_rexmit</a>() ||     <a class="code" href="a00147.html#g26a14b8dae3f861830af9e7cf1e03725">uip_newdata</a>() ||     <a class="code" href="a00147.html#gde6634974418e3240c212b9b16864368">uip_acked</a>() ||     <a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>() ||     <a class="code" href="a00147.html#g58bb90796c1cdad3aac2ecf44d87b20e">uip_poll</a>()) {    senddata();  }}</pre></div><p>The function starts with dealing with any error conditions that might have happened by checking if <a class="el" href="a00147.html#gfbd5fc486dfdf6bf6fc9db52b1f418c4">uip_aborted()</a> or <a class="el" href="a00147.html#g7b2ac4b18bd2ac3912fe67b3b17158c3">uip_timedout()</a> are true. If so, the appropriate error function is called. Also, if the connection has been closed, the closed() function is called to the it deal with the event.<p>Next, the function checks if the connection has just been established by checking if <a class="el" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected()</a> is true. The connected() function is called and is supposed to do whatever needs to be done when the connection is established, such as intializing the application state for the connection. Since it may be the case that data should be sent out, the senddata() function is called to deal with the outgoing data.<p>The following very simple application serves as an example of how the application handler functions might look. This application simply waits for any data to arrive on the connection, and responds to the data by sending out the message "Hello world!". To illustrate how to develop an application state machine, this message is sent in two parts, first the "Hello" part and then the "world!" part.<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#define STATE_WAITING 0</span><span class="preprocessor"></span><span class="preprocessor">#define STATE_HELLO   1</span><span class="preprocessor"></span><span class="preprocessor">#define STATE_WORLD   2</span><span class="preprocessor"></span><span class="keyword">struct </span>example6_state {  <a class="code" href="a00153.html#g4caecabca98b43919dd11be1c0d4cd8e">u8_t</a> state;  <span class="keywordtype">char</span> *textptr;  <span class="keywordtype">int</span>  textlen;};<span class="keyword">static</span> <span class="keywordtype">void</span> aborted(<span class="keywordtype">void</span>) {}<span class="keyword">static</span> <span class="keywordtype">void</span> timedout(<span class="keywordtype">void</span>) {}<span class="keyword">static</span> <span class="keywordtype">void</span> closed(<span class="keywordtype">void</span>) {}<span class="keyword">static</span> <span class="keywordtype">void</span> connected(<span class="keywordtype">void</span>) {  <span class="keyword">struct </span>example6_state *s = (<span class="keyword">struct </span>example6_state *)<a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a class="code" href="a00088.html#97f9e1fda815bfb8b1f4577c355ade20">appstate</a>;  s-&gt;state   = <a class="code" href="a00164.html#g7d7920c1e51cc4eef80206ebd6fee3f4">STATE_WAITING</a>;  s-&gt;textlen = 0;}<span class="keyword">static</span> <span class="keywordtype">void</span> newdata(<span class="keywordtype">void</span>) {  <span class="keyword">struct </span>example6_state *s = (<span class="keyword">struct </span>example6_state *)<a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a class="code" href="a00088.html#97f9e1fda815bfb8b1f4577c355ade20">appstate</a>;  <span class="keywordflow">if</span>(s-&gt;state == <a class="code" href="a00164.html#g7d7920c1e51cc4eef80206ebd6fee3f4">STATE_WAITING</a>) {    s-&gt;state   = STATE_HELLO;    s-&gt;textptr = <span class="stringliteral">"Hello "</span>;

⌨️ 快捷键说明

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