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

📄 buffer__sh_8h-source.html

📁 用来介绍ZIG Library游戏网络引擎的文档
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>buffer_sh.h Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center><hr><h1>buffer_sh.h</h1><div class="fragment"><pre>00001 <span class="comment">/*</span>00002 <span class="comment">    ZIG - An extendable, portable game engine focused on networking &amp; scripting</span>00003 <span class="comment">    Project Home: http://zige.sourceforge.net</span>00004 <span class="comment">    Copyright (C) 2002  F醔io Reis Cecin &lt;fcecin AT inf DOT ufrgs DOT br&gt;</span>00005 <span class="comment"></span>00006 <span class="comment">    This library is free software; you can redistribute it and/or</span>00007 <span class="comment">    modify it under the terms of the GNU Lesser General Public</span>00008 <span class="comment">    License as published by the Free Software Foundation; either</span>00009 <span class="comment">    version 2.1 of the License, or (at your option) any later version.</span>00010 <span class="comment"></span>00011 <span class="comment">    This library is distributed in the hope that it will be useful,</span>00012 <span class="comment">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span>00013 <span class="comment">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>00014 <span class="comment">    Lesser General Public License for more details.</span>00015 <span class="comment"></span>00016 <span class="comment">    You should have received a copy of the GNU Lesser General Public</span>00017 <span class="comment">    License along with this library; if not, write to the Free Software</span>00018 <span class="comment">    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   </span>00019 <span class="comment">*/</span>00020 <span class="comment">/*</span>00021 <span class="comment"></span>00022 <span class="comment">        buffer.h / shared version</span>00023 <span class="comment"></span>00024 <span class="comment">*/</span>00025 00026 <span class="preprocessor">#ifndef NO_DOXYGEN              //  not working; exclude from doxygen</span>00027 <span class="preprocessor"></span>00028 <span class="comment">// includes</span>00029 <span class="preprocessor">#include "shared.h"</span>00030 <span class="preprocessor">#include "thread.h"</span>00031 <span class="preprocessor">#include &lt;string&gt;</span>00032 <span class="comment">//using namespace std;  FIXME</span>00033 00034 <span class="comment">//this is the buffer's allocation unit. if big: less alocation, more wasted memory. if small, too much</span>00035 <span class="comment">//allocation, possibly less wasted memory</span>00036 <span class="preprocessor">#define BUFFER_CHUNK_SIZE 128</span>00037 <span class="preprocessor"></span>00038 <span class="keyword">extern</span> mutex_c cbuffer_mutex;00039 <span class="keyword">extern</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> cbuffer_count;00040 <span class="keyword">extern</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> cbuffer_totmem;00041 <span class="keyword">extern</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> cbuffer_maxmem;00042 00043 <span class="keywordtype">void</span> check_cbuffer_maxmem(<span class="keywordtype">int</span> yeah);00044 00045 <span class="comment">// shared buffer reg - a growable buffer</span>00046 <span class="keyword">class </span>buffer_t {00047 <span class="keyword">public</span>:00048         <span class="keywordtype">char</span> *data;             <span class="comment">//pointer to buffer</span>00049         <span class="keywordtype">int</span>     alen;                   <span class="comment">//allocated length</span>00050         <span class="keywordtype">int</span> ulen;                       <span class="comment">//used length</span>00051         buffer_t() {00052                 data = 0;00053                 alen = 0;00054                 ulen = 0;00055         }00056 };00057 00059 <span class="comment">//concrete subclass of shared_c</span>00060 <span class="keyword">class </span><a class="code" href="classbuffer__c.html">buffer_c</a> : <span class="keyword">public</span> shared_c {00061 <span class="keyword">public</span>:00062 00064         <a class="code" href="classbuffer__c.html#a0">buffer_c</a>();00065 00067         <a class="code" href="classbuffer__c.html#a0">buffer_c</a>(<span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a14">size</a>);00068 00070         <a class="code" href="classbuffer__c.html#a0">buffer_c</a>(<span class="keyword">const</span> <a class="code" href="classbuffer__c.html">buffer_c</a> &amp;b);00071 00073         <a class="code" href="classbuffer__c.html">buffer_c</a>&amp; <a class="code" href="classbuffer__c.html#a3">operator=</a>(<span class="keyword">const</span> <a class="code" href="classbuffer__c.html">buffer_c</a> &amp;b);00074 00076         <span class="keyword">virtual</span> <a class="code" href="classbuffer__c.html#a4">~buffer_c</a>();00077 00081         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a5">wrap</a>(<span class="keywordtype">char</span> *buffer, <span class="keywordtype">int</span> buffer_size);00082 00084         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a6">purge</a>() {00085                 writeAnnounce();00086                 <span class="keywordflow">if</span> (buf.data != 0) {00087                         00088                         cbuffer_mutex.lock();00089                         cbuffer_totmem -= buf.alen;00090                         cbuffer_mutex.unlock();00091 00092                         <span class="keyword">delete</span> buf.data; 00093                         buf.data = 0; 00094                 } 00095                 buf.ulen = buf.alen = pos = 0;00096         }00097 00099         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a9">clear</a>() { writeAnnounce(); buf.ulen = pos = 0; }00100 00102         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a10">seek</a>(<span class="keywordtype">int</span> ptarg) { pos = ptarg; }00103 00105         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a11">seek_end</a>() { <a class="code" href="classbuffer__c.html#a10">seek</a>( <a class="code" href="classbuffer__c.html#a14">size</a>() ); }00106 00108         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a12">skip</a>(<span class="keywordtype">int</span> amount) { <a class="code" href="classbuffer__c.html#a10">seek</a>( <a class="code" href="classbuffer__c.html#a13">getpos</a>() + amount ); }00109 00111         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a13">getpos</a>() { <span class="keywordflow">return</span> pos; }00112 00114         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a14">size</a>() { <span class="keywordflow">return</span> buf.ulen; }00115 00117         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a15">size_left</a>() { <span class="keywordflow">return</span> (buf.ulen - pos); }00118 00120         <span class="keywordtype">char</span> *<a class="code" href="classbuffer__c.html#a16">data</a>();00121 00123         <span class="keywordtype">char</span> *<a class="code" href="classbuffer__c.html#a17">data_cur</a>();00124 00126         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a18">manual_put_block</a>(<span class="keywordtype">int</span> amount) { buf.ulen += amount; pos += amount; <span class="keywordflow">if</span> (buf.ulen &gt; buf.alen) <a class="code" href="utils_8h.html#a1">FAILSTOP</a>(); }00127 00129         NLubyte <a class="code" href="classbuffer__c.html#a19">getByte</a>();00130 00132         NLbyte <a class="code" href="classbuffer__c.html#a20">getBytes</a>();00133 00135         NLushort <a class="code" href="classbuffer__c.html#a21">getShort</a>();00136 00138         NLshort <a class="code" href="classbuffer__c.html#a22">getShorts</a>();00139 00141         NLulong <a class="code" href="classbuffer__c.html#a23">getLong</a>();00142 00144         NLlong <a class="code" href="classbuffer__c.html#a24">getLongs</a>();00145 00147         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a27">getBlock</a>(<span class="keywordtype">char</span> *buffer, <span class="keywordtype">int</span> length);00148 00150         string <a class="code" href="classbuffer__c.html#a28">getString</a>();00151 00153         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a29">getDataToSocket</a>(NLsocket sock, <span class="keywordtype">int</span> length);00154 00156         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a50">putByte</a>(NLubyte i);00157 00159         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a51">putBytes</a>(NLbyte i);00160 00162         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a52">putShort</a>(NLushort i);00163 00165         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a53">putShorts</a>(NLshort i);00166 00168         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a54">putLong</a>(NLulong i);00169 00171         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a55">putLongs</a>(NLlong i);00172 00174         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a58">putBlock</a>(<span class="keywordtype">char</span> *buffer, <span class="keywordtype">int</span> length);00175 00177         <span class="keywordtype">void</span> <a class="code" href="classbuffer__c.html#a59">putString</a>(string str);00178 00182         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a60">putDataFromSocket</a>(NLsocket sock);00183 00187         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a60">putDataFromSocket</a>(NLsocket sock, <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#a14">size</a>);00188         00190         <span class="keywordtype">int</span> <a class="code" href="classbuffer__c.html#m0">code</a>, <a class="code" href="classbuffer__c.html#m1">id</a>, <a class="code" href="classbuffer__c.html#m2">internal</a>;00191 00192 <span class="keyword">protected</span>:00193 00194         <span class="comment">//delete shared object (last client's destructor called). MUST BE OVERRIDEN</span>00195         <span class="keyword">virtual</span> <span class="keywordtype">void</span> deleteSharedObject();00196         00197         <span class="comment">//make a private copy of the object -- prior to writing/updating when there </span>00198         <span class="comment">//is more than one reference. MUST BE OVERRIDEN</span>00199         <span class="keyword">virtual</span> <span class="keywordtype">void</span> copySharedObject();00200 00201         <span class="comment">//make a shallow copy -- MUST BE OVERRIDEN</span>00202         <span class="keyword">virtual</span> <span class="keywordtype">void</span> shallowCopyObject(shared_c *other) {00203 00204                 <a class="code" href="classbuffer__c.html">buffer_c</a> *b = (<a class="code" href="classbuffer__c.html">buffer_c</a>*)other;00205                 assert(b);00206                 <span class="comment">//CHANGED: let's copy pos too</span>00207                 pos = b-&gt;<a class="code" href="classbuffer__c.html#n1">pos</a>;           <span class="comment">//moved to shallow copy obj</span>00208                 buf = b-&gt;<a class="code" href="classbuffer__c.html#n0">buf</a>;00209                 <a class="code" href="classbuffer__c.html#m0">code</a> = b-&gt;<a class="code" href="classbuffer__c.html#m0">code</a>;00210                 <a class="code" href="classbuffer__c.html#m1">id</a> = b-&gt;<a class="code" href="classbuffer__c.html#m1">id</a>;00211                 <a class="code" href="classbuffer__c.html#m2">internal</a> = b-&gt;<a class="code" href="classbuffer__c.html#m2">internal</a>;00212         }00213                 00214         <span class="comment">//make buffer grow if needed, to make room for more "amount" bytes of data at "pos"</span>00215         <span class="keywordtype">void</span> grow(<span class="keywordtype">long</span> amount, <span class="keywordtype">bool</span> writing = <span class="keyword">true</span>);00216 00217         <span class="comment">//buffer struct: NOT a handle (speeeed!!)</span>00218         buffer_t buf;00219 00220         <span class="comment">//seek cursor in buffer</span>00221         <span class="keywordtype">long</span> pos;00222 };00223 00224 <span class="preprocessor">#endif // #ifndef NO_DOXYGEN            //  not working; exclude from doxygen</span>00225 <span class="preprocessor"></span></pre></div><hr><address style="align: right;"><small>Generated on Mon Jan 24 21:14:23 2005 for ZIG 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 + -