ZealOS/docs/Home/Net/Programs/TCPChatServer.CC.html
TomAwezome 1b75d91002 Fix Mount AHCI Port selection.
Add arg to SATARep to specify drive types to show.
Add checks in AHCIPortInit to verify port signatures, add helper method to get signatures from port.
2021-08-02 16:40:05 -04:00

254 lines
18 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V0.13">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF0>CTCPSocket *tcp = TCPSocket(AF_INET);
<a name="l2"></a>CTCPTreeQueue *connections = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CTCPTreeQueue</span><span class=cF7>)</span><span class=cF0>); </span><span class=cF2>// head</span><span class=cF0>
<a name="l3"></a></span><span class=cF1>U8</span><span class=cF0> *buffer = </span><span class=cF5>CAlloc</span><span class=cF0>(ETHERNET_FRAME_SIZE);
<a name="l4"></a>
<a name="l5"></a></span><span class=cF5>QueueInit</span><span class=cF0>(connections);
<a name="l6"></a>
<a name="l7"></a></span><span class=cF1>U0</span><span class=cF0> ChatServerKill()
<a name="l8"></a>{
<a name="l9"></a> CTCPTreeQueue *conn = connections-&gt;next;
<a name="l10"></a> CTCPTreeQueue *next_conn;
<a name="l11"></a>
<a name="l12"></a> </span><span class=cF1>while</span><span class=cF0> (conn != connections)
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l14"></a> </span><span class=cF6>&quot;\nClosing socket @ 0x%0X\n&quot;</span><span class=cF0>, conn-&gt;socket;
<a name="l15"></a> TCPSocketClose(conn-&gt;socket);
<a name="l16"></a> next_conn = conn-&gt;next;
<a name="l17"></a> </span><span class=cF5>Free</span><span class=cF0>(conn);
<a name="l18"></a> conn = next_conn;
<a name="l19"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l20"></a>
<a name="l21"></a> </span><span class=cF6>&quot;\nClosing listening socket.\n&quot;</span><span class=cF0>;
<a name="l22"></a> TCPSocketClose(tcp);
<a name="l23"></a>
<a name="l24"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l25"></a>}
<a name="l26"></a>
<a name="l27"></a></span><span class=cF1>U0</span><span class=cF0> ChatServerBroadcast(CTCPSocket *tcp_socket, </span><span class=cF9>I64</span><span class=cF0> length)
<a name="l28"></a>{ </span><span class=cF2>// Broadcast length bytes of msg in buffer to all but original socket.</span><span class=cF0>
<a name="l29"></a> CTCPTreeQueue *conn = connections-&gt;next;
<a name="l30"></a> CTCPSocket *dest_socket;
<a name="l31"></a> </span><span class=cF1>U8</span><span class=cF0> *ip_string;
<a name="l32"></a> CIPV4Address addr;
<a name="l33"></a> </span><span class=cF1>U8</span><span class=cF0> *message;
<a name="l34"></a> </span><span class=cF1>U8</span><span class=cF0> *message_prefix;
<a name="l35"></a>
<a name="l36"></a> </span><span class=cF1>while</span><span class=cF0> (conn != connections)
<a name="l37"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l38"></a> dest_socket = conn-&gt;socket;
<a name="l39"></a>
<a name="l40"></a> </span><span class=cF1>if</span><span class=cF0> (dest_socket != tcp_socket)
<a name="l41"></a> {
<a name="l42"></a> addr.address = </span><span class=cF5>EndianU32</span><span class=cF0>(dest_socket-&gt;destination_address</span><span class=cF7>(</span><span class=cF0>CSocketAddressIPV4</span><span class=cF7>)</span><span class=cF0>.address.address);
<a name="l43"></a>
<a name="l44"></a> ip_string = NetworkToPresentation(AF_INET, &amp;addr);
<a name="l45"></a> </span><span class=cF2>// TODO: is NetworkToPresentation backwards? or, do socket addrs store BE or LE ?</span><span class=cF0>
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF6>&quot;\nBroacasting msg to %s.\n&quot;</span><span class=cF0>, ip_string;
<a name="l48"></a>
<a name="l49"></a> addr.address = </span><span class=cF5>EndianU32</span><span class=cF0>(tcp_socket-&gt;destination_address</span><span class=cF7>(</span><span class=cF0>CSocketAddressIPV4</span><span class=cF7>)</span><span class=cF0>.address.address);
<a name="l50"></a>
<a name="l51"></a> ip_string = NetworkToPresentation(AF_INET, &amp;addr);
<a name="l52"></a> </span><span class=cF2>// TODO: is NetworkToPresentation backwards? or, do socket addrs store BE or LE ?</span><span class=cF0>
<a name="l53"></a>
<a name="l54"></a> message_prefix = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF6>&quot;$BG,PURPLE$$BLACK$&lt;%s&gt;$FG$$BG$ %%0%dts&quot;</span><span class=cF0>, ip_string, length);
<a name="l55"></a>
<a name="l56"></a> message = </span><span class=cF5>MStrPrint</span><span class=cF0>(message_prefix, buffer);
<a name="l57"></a>
<a name="l58"></a> TCPSocketSendString(dest_socket, message);
<a name="l59"></a>
<a name="l60"></a> </span><span class=cF5>Free</span><span class=cF0>(message);
<a name="l61"></a> </span><span class=cF5>Free</span><span class=cF0>(message_prefix);
<a name="l62"></a> </span><span class=cF5>Free</span><span class=cF0>(ip_string);
<a name="l63"></a>
<a name="l64"></a> }
<a name="l65"></a>
<a name="l66"></a> conn = conn-&gt;next;
<a name="l67"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l68"></a>
<a name="l69"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l70"></a>}
<a name="l71"></a>
<a name="l72"></a></span><span class=cF1>U0</span><span class=cF0> ChatServerBroadcastDisconnect()
<a name="l73"></a>{
<a name="l74"></a> CTCPTreeQueue *conn = connections-&gt;next;
<a name="l75"></a> CTCPSocket *conn_socket;
<a name="l76"></a> </span><span class=cF1>U8</span><span class=cF0> *message = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF6>&quot;$BG,LTGRAY$$DKGRAY$Client disconnected. Connected clients: %d$FG$$BG$&quot;</span><span class=cF0>,
<a name="l77"></a> </span><span class=cF5>QueueCount</span><span class=cF7>(</span><span class=cF0>connections</span><span class=cF7>)</span><span class=cF0>);
<a name="l78"></a> </span><span class=cF1>while</span><span class=cF0> (conn != connections)
<a name="l79"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l80"></a> conn_socket = conn-&gt;socket;
<a name="l81"></a>
<a name="l82"></a> TCPSocketSendString(conn_socket, message);
<a name="l83"></a>
<a name="l84"></a> conn = conn-&gt;next;
<a name="l85"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l86"></a>
<a name="l87"></a> </span><span class=cF5>Free</span><span class=cF0>(message);
<a name="l88"></a>
<a name="l89"></a>}
<a name="l90"></a>
<a name="l91"></a></span><span class=cF1>U0</span><span class=cF0> ChatServerReceive()
<a name="l92"></a>{
<a name="l93"></a> CTCPTreeQueue *conn = connections-&gt;next;
<a name="l94"></a> CTCPTreeQueue *next_conn;
<a name="l95"></a> CTCPSocket *socket;
<a name="l96"></a> </span><span class=cF9>I64</span><span class=cF0> message_len;
<a name="l97"></a> </span><span class=cF1>U8</span><span class=cF0> *ip_string;
<a name="l98"></a> CIPV4Address addr;
<a name="l99"></a>
<a name="l100"></a>
<a name="l101"></a> </span><span class=cF1>while</span><span class=cF0> (conn != connections)
<a name="l102"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l103"></a> socket = conn-&gt;socket;
<a name="l104"></a> message_len = TCPSocketReceive(socket, buffer, ETHERNET_FRAME_SIZE);
<a name="l105"></a>
<a name="l106"></a> </span><span class=cF1>if</span><span class=cF0> (message_len == </span><span class=cFE>0</span><span class=cF0>)
<a name="l107"></a> {
<a name="l108"></a> </span><span class=cF6>&quot;\nClosing a connection.\n&quot;</span><span class=cF0>;
<a name="l109"></a> socket-&gt;timeout = TCP_TIMEOUT;
<a name="l110"></a> TCPSocketClose(socket);
<a name="l111"></a>
<a name="l112"></a> next_conn = conn-&gt;next;
<a name="l113"></a> </span><span class=cF5>QueueRemove</span><span class=cF0>(conn);
<a name="l114"></a> </span><span class=cF5>Free</span><span class=cF0>(conn);
<a name="l115"></a> conn = next_conn;
<a name="l116"></a>
<a name="l117"></a> ChatServerBroadcastDisconnect();
<a name="l118"></a>
<a name="l119"></a> }
<a name="l120"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (message_len &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l121"></a> {
<a name="l122"></a> addr.address = </span><span class=cF5>EndianU32</span><span class=cF0>(socket-&gt;destination_address</span><span class=cF7>(</span><span class=cF0>CSocketAddressIPV4</span><span class=cF7>)</span><span class=cF0>.address.address);
<a name="l123"></a>
<a name="l124"></a> ip_string = NetworkToPresentation(AF_INET, &amp;addr);
<a name="l125"></a> </span><span class=cF2>// TODO: is NetworkToPresentation backwards? or, do socket addrs store BE or LE ?</span><span class=cF0>
<a name="l126"></a>
<a name="l127"></a> </span><span class=cF6>&quot;\nBroadcasting %d byte msg from %s: %Q\n&quot;</span><span class=cF0>, message_len, ip_string, buffer;
<a name="l128"></a>
<a name="l129"></a> </span><span class=cF2>//ClassRep(socket);</span><span class=cF0>
<a name="l130"></a> ChatServerBroadcast(socket, message_len);
<a name="l131"></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="l132"></a> conn = conn-&gt;next;
<a name="l133"></a> }
<a name="l134"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l135"></a> {
<a name="l136"></a> </span><span class=cF2>//&quot;\nReceived -1 [error], trying next connection.\n&quot;;</span><span class=cF0>
<a name="l137"></a> conn = conn-&gt;next;
<a name="l138"></a> }
<a name="l139"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l140"></a>
<a name="l141"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l142"></a>
<a name="l143"></a>}
<a name="l144"></a>
<a name="l145"></a></span><span class=cF1>U0</span><span class=cF0> ChatServer()
<a name="l146"></a>{
<a name="l147"></a> CSocketAddressIPV4 socket_addr;
<a name="l148"></a> </span><span class=cF1>U8</span><span class=cF0> *port_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Server Port: &quot;</span><span class=cF0>);
<a name="l149"></a> </span><span class=cF9>I64</span><span class=cF0> port = </span><span class=cF5>Str2I64</span><span class=cF0>(port_string);
<a name="l150"></a> CTCPSocket *new_socket;
<a name="l151"></a> CTCPTreeQueue *new_conn;
<a name="l152"></a> </span><span class=cF1>U8</span><span class=cF0> *join_msg;
<a name="l153"></a> CTCPTreeQueue *conn;
<a name="l154"></a> CTCPSocket *conn_socket;
<a name="l155"></a>
<a name="l156"></a>
<a name="l157"></a> </span><span class=cF5>Free</span><span class=cF0>(port_string);
<a name="l158"></a>
<a name="l159"></a> socket_addr.port = </span><span class=cF5>EndianU16</span><span class=cF0>(port);
<a name="l160"></a> socket_addr.family = AF_INET;
<a name="l161"></a> socket_addr.address.address = INADDR_ANY;
<a name="l162"></a>
<a name="l163"></a> tcp-&gt;timeout = </span><span class=cFE>0</span><span class=cF0>.</span><span class=cFE>3</span><span class=cF0> * </span><span class=cF3>JIFFY_FREQ</span><span class=cF0>;
<a name="l164"></a>
<a name="l165"></a> </span><span class=cF6>&quot;\nTrying to bind socket.\n&quot;</span><span class=cF0>;
<a name="l166"></a> </span><span class=cF1>if</span><span class=cF0> (TCPSocketBind</span><span class=cF7>(</span><span class=cF0>tcp, &amp;socket_addr</span><span class=cF7>)</span><span class=cF0> == </span><span class=cFE>0</span><span class=cF0>)
<a name="l167"></a> </span><span class=cF6>&quot;\nSocket bound.\n&quot;</span><span class=cF0>;
<a name="l168"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l169"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l170"></a> </span><span class=cF6>&quot;\nFailed to bind socket.\n&quot;</span><span class=cF0>;
<a name="l171"></a> ChatServerKill;
<a name="l172"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l173"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l174"></a>
<a name="l175"></a> </span><span class=cF6>&quot;\nTrying to listen on socket.\n&quot;</span><span class=cF0>;
<a name="l176"></a> </span><span class=cF1>if</span><span class=cF0> (TCPSocketListen</span><span class=cF7>(</span><span class=cF0>tcp, </span><span class=cFE>5</span><span class=cF7>)</span><span class=cF0> == </span><span class=cFE>0</span><span class=cF0>)
<a name="l177"></a> </span><span class=cF6>&quot;\nSocket now listening.\n&quot;</span><span class=cF0>;
<a name="l178"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l179"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l180"></a> </span><span class=cF6>&quot;\nFailed to listen on socket.\n&quot;</span><span class=cF0>;
<a name="l181"></a> ChatServerKill;
<a name="l182"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l183"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l184"></a>
<a name="l185"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>CharScan</span><span class=cF0> != </span><span class=cF3>CH_SHIFT_ESC</span><span class=cF0>)
<a name="l186"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l187"></a> new_socket = TCPSocketAccept(tcp);
<a name="l188"></a>
<a name="l189"></a> </span><span class=cF1>if</span><span class=cF0> (new_socket &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l190"></a> {
<a name="l191"></a> </span><span class=cF6>&quot;\nNew connection.\n&quot;</span><span class=cF0>;
<a name="l192"></a> new_conn = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CTCPTreeQueue</span><span class=cF7>)</span><span class=cF0>);
<a name="l193"></a> new_conn-&gt;socket = new_socket;
<a name="l194"></a> new_socket-&gt;timeout = </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>// Set new connection non-blocking.</span><span class=cF0>
<a name="l195"></a>
<a name="l196"></a> join_msg = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF6>&quot;$BG,LTGRAY$$DKGRAY$Connected clients: %d$FG$$BG$&quot;</span><span class=cF0>, </span><span class=cF5>QueueCount</span><span class=cF7>(</span><span class=cF0>connections</span><span class=cF7>)</span><span class=cF0> + </span><span class=cFE>1</span><span class=cF0>);
<a name="l197"></a>
<a name="l198"></a> TCPSocketSendString(new_socket, join_msg);
<a name="l199"></a> </span><span class=cF5>Free</span><span class=cF0>(join_msg);
<a name="l200"></a>
<a name="l201"></a> join_msg = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF6>&quot;$BG,LTGRAY$$DKGRAY$New connection. Connected clients: %d$FG$$BG$&quot;</span><span class=cF0>,
<a name="l202"></a> </span><span class=cF5>QueueCount</span><span class=cF7>(</span><span class=cF0>connections</span><span class=cF7>)</span><span class=cF0> + </span><span class=cFE>1</span><span class=cF0>);
<a name="l203"></a>
<a name="l204"></a> conn = connections-&gt;next;
<a name="l205"></a>
<a name="l206"></a> </span><span class=cF1>while</span><span class=cF0> (conn != connections)
<a name="l207"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l208"></a> </span><span class=cF6>&quot;\nNotifying clients of new connection.\n&quot;</span><span class=cF0>;
<a name="l209"></a> conn_socket = conn-&gt;socket;
<a name="l210"></a> TCPSocketSendString(conn_socket, join_msg);
<a name="l211"></a> conn = conn-&gt;next;
<a name="l212"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l213"></a> </span><span class=cF5>Free</span><span class=cF0>(join_msg);
<a name="l214"></a>
<a name="l215"></a> </span><span class=cF5>QueueInsertRev</span><span class=cF0>(new_conn, connections);
<a name="l216"></a> }
<a name="l217"></a>
<a name="l218"></a> ChatServerReceive; </span><span class=cF2>// Receive &amp; Broadcast</span><span class=cF0>
<a name="l219"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>50</span><span class=cF0>);
<a name="l220"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l221"></a>
<a name="l222"></a>
<a name="l223"></a> ChatServerKill;
<a name="l224"></a>}
<a name="l225"></a>
<a name="l226"></a>ChatServer;</span></pre></body>
</html>