ZealOS/docs/Home/Net/Programs/UDPChat.ZC.html
2021-12-11 06:10:58 -05:00

148 lines
12 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.07">
<style type="text/css">
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>/*</span><span class=cF0>
<a name="l2"></a> </span><span class=cF2>Chat program proof-of-concept using UDP.</span><span class=cF0>
<a name="l3"></a>
<a name="l4"></a> </span><span class=cF2>Tested on Bridged Mode with two VMs under the same router (same and different computers),</span><span class=cF0>
<a name="l5"></a> </span><span class=cF2>and on NAT Network Mode with two VMs using the same virtualized NAT.</span><span class=cF0>
<a name="l6"></a>
<a name="l7"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l8"></a>
<a name="l9"></a></span><span class=cF1>U8</span><span class=cF0> *dest_ip_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Destination IPV4: &quot;</span><span class=cF0>),
<a name="l10"></a> *port_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;RX &amp; TX UDP Port: &quot;</span><span class=cF0>);
<a name="l11"></a>CIPV4Address *dest_address = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CIPV4Address</span><span class=cF7>)</span><span class=cF0>);
<a name="l12"></a>
<a name="l13"></a></span><span class=cF9>I64</span><span class=cF0> port = </span><span class=cF5>Str2I64</span><span class=cF0>(port_string);
<a name="l14"></a></span><span class=cF9>CTask</span><span class=cF0> *chat_display_task = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l15"></a></span><span class=cF9>CTask</span><span class=cF0> *chat_message_task = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l16"></a>
<a name="l17"></a></span><span class=cF1>U0</span><span class=cF0> ChatDisplayTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l18"></a>{ </span><span class=cF2>// display received messages.</span><span class=cF0>
<a name="l19"></a> </span><span class=cF5>DocTermNew</span><span class=cF0>;
<a name="l20"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l21"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l22"></a>
<a name="l23"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>TaskValidate</span><span class=cF7>(</span><span class=cF0>chat_message_task</span><span class=cF7>)</span><span class=cF0>)
<a name="l24"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l25"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l26"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l27"></a>}
<a name="l28"></a>
<a name="l29"></a></span><span class=cF1>U0</span><span class=cF0> ChatMessageTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l30"></a>{ </span><span class=cF2>// take in text.</span><span class=cF0>
<a name="l31"></a> </span><span class=cF1>U8</span><span class=cF0> *message;
<a name="l32"></a> </span><span class=cF9>I64</span><span class=cF0> de_index;
<a name="l33"></a> </span><span class=cF1>U8</span><span class=cF0> *payload;
<a name="l34"></a>
<a name="l35"></a> </span><span class=cF5>DocTermNew</span><span class=cF0>;
<a name="l36"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l37"></a>
<a name="l38"></a> </span><span class=cF1>while</span><span class=cF0> (message = </span><span class=cF5>StrGet</span><span class=cF7>(</span><span class=cF6>&quot;&gt; &quot;</span><span class=cF0>,, </span><span class=cF3>SGF_SHIFT_ESC_EXIT</span><span class=cF7>)</span><span class=cF0>)
<a name="l39"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l40"></a> </span><span class=cF5>DocBottom</span><span class=cF0>(chat_display_task-&gt;put_doc);
<a name="l41"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(chat_display_task-&gt;put_doc, </span><span class=cF6>&quot;$BG,BLUE$$BLACK$&lt;local&gt;$FG$$BG$ %s\n&quot;</span><span class=cF0>, message);
<a name="l42"></a>
<a name="l43"></a> de_index = UDPPacketAllocate(&amp;payload, ipv4_globals.local_ip, port, dest_address-&gt;address, port, </span><span class=cF5>StrLen</span><span class=cF7>(</span><span class=cF0>message</span><span class=cF7>)</span><span class=cF0>);
<a name="l44"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(payload, message, </span><span class=cF5>StrLen</span><span class=cF7>(</span><span class=cF0>message</span><span class=cF7>)</span><span class=cF0>);
<a name="l45"></a> UDPPacketFinish(de_index);
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l49"></a>
<a name="l50"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l51"></a>}
<a name="l52"></a>
<a name="l53"></a></span><span class=cF1>U0</span><span class=cF0> ChatInit()
<a name="l54"></a>{
<a name="l55"></a> chat_message_task = </span><span class=cF5>Spawn</span><span class=cF0>(&amp;ChatMessageTask, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF6>&quot;UDP Chat Message&quot;</span><span class=cF0>);
<a name="l56"></a> chat_display_task = </span><span class=cF5>Spawn</span><span class=cF0>(&amp;ChatDisplayTask, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF6>&quot;UDP Chat&quot;</span><span class=cF0>);
<a name="l57"></a> chat_message_task-&gt;win_inhibit = </span><span class=cF3>WIG_USER_TASK_DEFAULT</span><span class=cF0>;
<a name="l58"></a> chat_display_task-&gt;win_inhibit = </span><span class=cF3>WIG_USER_TASK_DEFAULT</span><span class=cF0>;
<a name="l59"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;chat_message_task-&gt;display_flags, </span><span class=cF3>DISPLAYf_SHOW</span><span class=cF0>);
<a name="l60"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;chat_display_task-&gt;display_flags, </span><span class=cF3>DISPLAYf_SHOW</span><span class=cF0>);
<a name="l61"></a>
<a name="l62"></a> </span><span class=cF5>WinFocus</span><span class=cF0>(chat_display_task);
<a name="l63"></a> </span><span class=cF5>WinFocus</span><span class=cF0>(chat_message_task);
<a name="l64"></a>
<a name="l65"></a> chat_display_task-&gt;win_top = </span><span class=cFE>2</span><span class=cF0>;
<a name="l66"></a> chat_display_task-&gt;win_bottom = </span><span class=cF3>TEXT_ROWS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0>;
<a name="l67"></a> chat_display_task-&gt;win_left = </span><span class=cF3>TEXT_COLS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0>;
<a name="l68"></a> chat_display_task-&gt;win_right = </span><span class=cF3>TEXT_COLS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0> * </span><span class=cFE>2</span><span class=cF0>;
<a name="l69"></a>
<a name="l70"></a> chat_message_task-&gt;win_top = chat_display_task-&gt;win_bottom + </span><span class=cFE>2</span><span class=cF0>;
<a name="l71"></a> chat_message_task-&gt;win_bottom = chat_message_task-&gt;win_top + </span><span class=cFE>3</span><span class=cF0>;
<a name="l72"></a> chat_message_task-&gt;win_left = chat_display_task-&gt;win_left;
<a name="l73"></a> chat_message_task-&gt;win_right = chat_display_task-&gt;win_right;
<a name="l74"></a>
<a name="l75"></a>}
<a name="l76"></a>
<a name="l77"></a></span><span class=cF1>U0</span><span class=cF0> Chat()
<a name="l78"></a>{
<a name="l79"></a> CUDPSocket *udp_socket = UDPSocket(AF_INET);
<a name="l80"></a> CSocketAddressIPV4 *sock_addr = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CSocketAddressIPV4</span><span class=cF7>)</span><span class=cF0>);
<a name="l81"></a> </span><span class=cF1>U8</span><span class=cF0> *buffer[ETHERNET_FRAME_SIZE];
<a name="l82"></a>
<a name="l83"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>MemSet(buffer, 0, ETHERNET_FRAME_SIZE);</span><span class=cF0>
<a name="l84"></a>
<a name="l85"></a> </span><span class=cF6>&quot;\nIP entered: %s\n&quot;</span><span class=cF0>, dest_ip_string;
<a name="l86"></a>
<a name="l87"></a> </span><span class=cF1>while</span><span class=cF0> (PresentationToNetwork</span><span class=cF7>(</span><span class=cF0>AF_INET, dest_ip_string, dest_address</span><span class=cF7>)</span><span class=cF0> == -</span><span class=cFE>1</span><span class=cF0>)
<a name="l88"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l89"></a> </span><span class=cF6>&quot;ERROR: Bad IP entered. Retry.\n&quot;</span><span class=cF0>;
<a name="l90"></a> dest_ip_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Destination IPV4: &quot;</span><span class=cF0>);
<a name="l91"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l92"></a>
<a name="l93"></a> ChatInit;
<a name="l94"></a>
<a name="l95"></a> sock_addr-&gt;port = </span><span class=cF5>EndianU16</span><span class=cF0>(port);
<a name="l96"></a> sock_addr-&gt;family = AF_INET;
<a name="l97"></a> sock_addr-&gt;address.address = dest_address-&gt;address;
<a name="l98"></a> UDPSocketBind(udp_socket, sock_addr);
<a name="l99"></a>
<a name="l100"></a> </span><span class=cF6>&quot;\nIP entered: %s\n&quot;</span><span class=cF0>, dest_ip_string;
<a name="l101"></a>
<a name="l102"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>TaskValidate</span><span class=cF7>(</span><span class=cF0>chat_message_task</span><span class=cF7>)</span><span class=cF0>)
<a name="l103"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l104"></a> </span><span class=cF1>if</span><span class=cF0> (UDPSocketReceiveFrom</span><span class=cF7>(</span><span class=cF0>udp_socket, buffer, ETHERNET_FRAME_SIZE, </span><span class=cF3>NULL</span><span class=cF7>)</span><span class=cF0> != -</span><span class=cFE>1</span><span class=cF0>)
<a name="l105"></a> {
<a name="l106"></a> </span><span class=cF5>DocBottom</span><span class=cF0>(chat_display_task-&gt;put_doc);
<a name="l107"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(chat_display_task-&gt;put_doc, </span><span class=cF6>&quot;$BG,PURPLE$$BLACK$&lt;%s&gt;$FG$$BG$ %s\n&quot;</span><span class=cF0>, dest_ip_string, buffer);</span><span class=cF2>//NetworkToPresentation(AF_INET, dest_address));</span><span class=cF0>
<a name="l108"></a> </span><span class=cF5>MemSet</span><span class=cF0>(buffer, </span><span class=cFE>0</span><span class=cF0>, ETHERNET_FRAME_SIZE);
<a name="l109"></a> }
<a name="l110"></a>
<a name="l111"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l112"></a>
<a name="l113"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l114"></a>
<a name="l115"></a> </span><span class=cF5>Free</span><span class=cF0>(sock_addr);
<a name="l116"></a> UDPSocketClose(udp_socket);
<a name="l117"></a>
<a name="l118"></a>}
<a name="l119"></a>
<a name="l120"></a>Chat;</span></pre></body>
</html>