📄 udp__demo_8c-example.html
字号:
DEBUGOUT(<span class="stringliteral">"No free UDP sockets!! \r\n"</span>); <a name="a1"></a><a class="code" href="system_8h.html#a6">RESET_SYSTEM</a>(); } <span class="comment">/* open socket for receiving/sending of the data on defined port*/</span> <a name="a2"></a><a class="code" href="group__udp__app__api.html#a2">udp_open</a>(<a class="code" href="udp__demo_8c.html#a4">udp_demo_soch</a>,<a class="code" href="udp__demo_8c.html#a0">UDP_DEMO_PORT</a>); <span class="comment">/* for now no data sending */</span> <a class="code" href="udp__demo_8c.html#a5">udp_demo_senddata</a>=0;}<span class="comment">/* UDP Demo app main loop that is periodically invoked from the</span><span class="comment"> * main loop (see main_demo.c)</span><span class="comment"> */</span><span class="keywordtype">void</span> udp_demo_run(<span class="keywordtype">void</span>){ <span class="comment">/* do maybe some other UDP demo app stuff</span><span class="comment"> * .....</span><span class="comment"> */</span> <span class="keywordflow">if</span>(udp_demo_senddata) { <span class="keywordflow">switch</span>(udp_demo_send()){ <span class="keywordflow">case</span> -1: DEBUGOUT(<span class="stringliteral">"Error (General error, e.g. parameters)\r\n"</span>); <span class="keywordflow">break</span>; <span class="keywordflow">case</span> -2: DEBUGOUT(<span class="stringliteral">"ARP or lower layer not ready, try again\r\n"</span>); <span class="keywordflow">break</span>; <span class="keywordflow">case</span> -3: DEBUGOUT(<span class="stringliteral">"Socket closed or socket handle not valid!\r\n"</span>); <span class="keywordflow">break</span>; <span class="keywordflow">default</span>: <span class="comment">/* data sent (could check how many bytes too)</span><span class="comment"> * if no more data to send, reset flag</span><span class="comment"> */</span> <a class="code" href="udp__demo_8c.html#a5">udp_demo_senddata</a>=0; <span class="keywordflow">break</span>; } }}<span class="comment">/*</span><span class="comment"> * Event listener invoked when TCP/IP stack receives UDP datagram for</span><span class="comment"> * a given socket. Parameters:</span><span class="comment"> * - cbhandle - handle of the socket this packet is intended for. Check it</span><span class="comment"> * just to be sure, but in general case not needed</span><span class="comment"> * - event - event that is notified. For UDP, only UDP_EVENT_DATA</span><span class="comment"> * - ipaddr - IP address of remote host who sent the UDP datagram</span><span class="comment"> * - port - port number of remote host who sent the UDP datagram</span><span class="comment"> * - buffindex - buffer index in RTL8019AS allowing you to read </span><span class="comment"> * received data more than once from Ethernet controller by</span><span class="comment"> * invoking NETWORK_RECEIVE_INITIALIZE(buffindex) and then start</span><span class="comment"> * reading the bytes all over again</span><span class="comment"> */</span> INT32 udp_demo_eventlistener(INT8 cbhandle, UINT8 event, UINT32 ipaddr, UINT16 port, UINT16 buffindex, UINT16 datalen){ UINT16 i; <span class="keywordflow">if</span>(cbhandle!=<a class="code" href="udp__demo_8c.html#a4">udp_demo_soch</a>){ DEBUGOUT(<span class="stringliteral">"Not my handle!!!!"</span>); <span class="keywordflow">return</span> (-1); } <span class="keywordflow">switch</span>(event){ <span class="keywordflow">case</span> <a class="code" href="tcp__ip_8h.html#a31">UDP_EVENT_DATA</a>: <span class="comment">/* read data that was received (and </span><span class="comment"> * probably do something with it :-)</span><span class="comment"> */</span> <span class="keywordflow">for</span>(i=0;i<datalen;i++) <a name="a3"></a><a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>(); <span class="comment">/* If needed initialize data sending</span><span class="comment"> * by setting udp_demo_senddata variable or</span><span class="comment"> * send data directly from event listener (</span><span class="comment"> * only possible for UDP applications!!!!</span><span class="comment"> */</span> <span class="keywordflow">break</span>; <span class="keywordflow">default</span>: <span class="comment">/* should never get here */</span> DEBUGOUT(<span class="stringliteral">"Unknown UDP event :-("</span>); <span class="keywordflow">break</span>; } <span class="keywordflow">return</span> 0;}<span class="comment">/* internal function invoked to send UDP message to, in this case,</span><span class="comment"> * some predefined host.</span><span class="comment"> */</span><span class="preprocessor">#define MSG_SIZE 20</span><span class="preprocessor"></span>INT16 udp_demo_send(<span class="keywordtype">void</span>){ UINT8 i; <span class="comment">/* put message in buffer. Message needs to start from UDP_APP_OFFSET</span><span class="comment"> * because TCP/IP stack will put headers in front of the message to</span><span class="comment"> * avoid data copying</span><span class="comment"> */</span> <span class="keywordflow">for</span>(i=0;i<MSG_SIZE;i++) net_buf[<a class="code" href="tcp__ip_8h.html#a62">UDP_APP_OFFSET</a>+i]=i; <span class="comment">/* send message */</span> <span class="keywordflow">return</span> <a name="a4"></a><a class="code" href="group__udp__app__api.html#a4">udp_send</a>(<a class="code" href="udp__demo_8c.html#a4">udp_demo_soch</a>,<a class="code" href="udp__demo_8c.html#a1">UDP_DEMO_RMTHOST_IP</a>,<a class="code" href="udp__demo_8c.html#a2">UDP_DEMO_RMTHOST_PRT</a>,net_buf+<a class="code" href="tcp__ip_8h.html#a62">UDP_APP_OFFSET</a>,<a class="code" href="group__opentcp__config.html#a4">NETWORK_TX_BUFFER_SIZE</a>-<a class="code" href="tcp__ip_8h.html#a62">UDP_APP_OFFSET</a>,MSG_SIZE); }</pre></div><hr><address style="align: right;"><small>Generated on Sun Aug 3 20:32:59 2003 for OpenTCP by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.18 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -