Modified: websites/staging/thrift/trunk/content/tutorial/d.html ============================================================================== --- websites/staging/thrift/trunk/content/tutorial/d.html (original) +++ websites/staging/thrift/trunk/content/tutorial/d.html Thu Mar 20 14:51:19 2014 @@ -72,9 +72,128 @@ <h3 id="prerequisites">Prerequisites</h3> <h3 id="client">Client</h3> -<p>[snippet:path=tutorial/d/server.d:lang=cpp:lines=19,111]</p> +<p><div class="codehilite"><pre><span class="n">module</span> <span class="n">server</span><span class="p">;</span></p> +<p><span class="n">import</span> <span class="n">std</span><span class="p">.</span><span class="n">conv</span> <span class="o">:</span> <span class="n">to</span><span class="p">;</span> +<span class="n">import</span> <span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">codegen</span><span class="p">.</span><span class="n">processor</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">protocol</span><span class="p">.</span><span class="n">binary</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">server</span><span class="p">.</span><span class="n">simple</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">server</span><span class="p">.</span><span class="n">transport</span><span class="p">.</span><span class="n">socket</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span><span class="p">.</span><span class="n">buffered</span><span class="p">;</span></p> +<p><span class="n">import</span> <span class="n">share</span><span class="p">.</span><span class="n">SharedService</span><span class="p">;</span> +<span class="n">import</span> <span class="n">share</span><span class="p">.</span><span class="n">shared_types</span><span class="p">;</span> +<span class="n">import</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">Calculator</span><span class="p">;</span> +<span class="n">import</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">tutorial_types</span><span class="p">;</span></p> +<p><span class="cm">/*<em></span> +<span class="cm"> * The actual implementation of the Calculator interface that is called by</span> +<span class="cm"> * the server to answer the requests.</span> +<span class="cm"> </em>/</span> +<span class="k">class</span> <span class="nc">CalculatorHandler</span> <span class="o">:</span> <span class="n">Calculator</span> <span class="p">{</span> + <span class="kt">void</span> <span class="n">ping</span><span class="p">()</span> <span class="p">{</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"ping()"</span><span class="p">);</span> + <span class="p">}</span></p> +<p><span class="kt">int</span> <span class="n">add</span><span class="p">(</span><span class="kt">int</span> <span class="n">n1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n2</span><span class="p">)</span> <span class="p">{</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"add(%s,%s)"</span><span class="p">,</span> <span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">);</span> + <span class="k">return</span> <span class="n">n1</span> <span class="o">+</span> <span class="n">n2</span><span class="p">;</span> + <span class="p">}</span></p> +<p><span class="kt">int</span> <span class="n">calculate</span><span class="p">(</span><span class="kt">int</span> <span class="n">logid</span><span class="p">,</span> <span class="n">ref</span> <span class="k">const</span><span class="p">(</span><span class="n">Work</span><span class="p">)</span> <span class="n">work</span><span class="p">)</span> <span class="p">{</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"calculate(%s, {%s, %s, %s})"</span><span class="p">,</span> <span class="n">logid</span><span class="p">,</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span><span class="p">,</span> <span class="n">work</span><span class="p">.</span><span class="n">num1</span><span class="p">,</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span><span class="p">);</span> + <span class="kt">int</span> <span class="n">val</span><span class="p">;</span></p> +<div class="codehilite"><pre><span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>switch<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>(<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>op<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>)<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span> <span class="s">"p"</span><span class="nt">></span>{<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>case<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nl"</span><span class="nt">></span>ADD:<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>+<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>break<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>case<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nl"</span><span class="nt">></span>SUBTRACT:<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>-<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>break<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>case<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nl"</span><span class="nt">></span>MULTIPLY:<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>*<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>break<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>case<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nl"</span><span class="nt">></span>DIVIDE:<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>if<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>(<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><sp an class="s">"mi"</span><span class="nt">></span>0<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>)<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>{<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>auto<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>new<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>InvalidOperation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>();<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>what<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n&quo t;</span><span class="nt">></span>op<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>why<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"s"</span><span class="nt">></span><span class="ni">&quot;</span>Cannot divide by 0<span class="ni">&quot;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></ span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>throw<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>}<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>/<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>break<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>default<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>:<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>auto<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>new<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>InvalidOperation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>();<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>what<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n" </span><span class="nt">></span>op<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>why<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"s"</span><span class="nt">></span><span class="ni">&quot;</span>Invalid Operation<span class="ni">&quot;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></spa n></span> + <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>throw<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>io<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>}<span class="nt"></span></span> + +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>auto<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>ss<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>SharedStruct<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>();<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>ss<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>key<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>logid<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>ss<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>value<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>to<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>!<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</s pan><span class="nt">></span>string<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>(<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>);<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>[<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>logid<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>]<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>ss<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</ span><span class="nt">></span>;<span class="nt"></span></span> + +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>return<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +</pre></div> + + +<p><span class="p">}</span></p> +<p><span class="n">SharedStruct</span> <span class="n">getStruct</span><span class="p">(</span><span class="kt">int</span> <span class="n">logid</span><span class="p">)</span> <span class="p">{</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"getStruct(%s)"</span><span class="p">,</span> <span class="n">logid</span><span class="p">);</span> + <span class="k">return</span> <span class="n">log</span><span class="p">[</span><span class="n">logid</span><span class="p">];</span> + <span class="p">}</span></p> +<p><span class="kt">void</span> <span class="n">zip</span><span class="p">()</span> <span class="p">{</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"zip()"</span><span class="p">);</span> + <span class="p">}</span></p> +<p><span class="k">protected</span><span class="o">:</span> + <span class="n">SharedStruct</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span> <span class="n">log</span><span class="p">;</span> +<span class="p">}</span></p> +<p><span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> + <span class="k">auto</span> <span class="n">protocolFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TBinaryProtocolFactory</span><span class="o">!</span><span class="p">();</span> + <span class="k">auto</span> <span class="n">processor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TServiceProcessor</span><span class="o">!</span><span class="n">Calculator</span><span class="p">(</span><span class="k">new</span> <span class="n">CalculatorHandler</span><span class="p">);</span> + <span class="k">auto</span> <span class="n">serverTransport</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TServerSocket</span><span class="p">(</span><span class="mi">9090</span><span class="p">);</span> + <span class="k">auto</span> <span class="n">transportFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TBufferedTransportFactory</span><span class="p">;</span></p> +<p><span class="k">auto</span> <span class="n">server</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TSimpleServer</span><span class="p">(</span> + <span class="n">processor</span><span class="p">,</span> <span class="n">serverTransport</span><span class="p">,</span> <span class="n">transportFactory</span><span class="p">,</span> <span class="n">protocolFactory</span><span class="p">);</span></p> +<p><span class="n">writeln</span><span class="p">(</span><span class="s">"Starting the server on port 9090..."</span><span class="p">);</span> + <span class="n">server</span><span class="p">.</span><span class="n">serve</span><span class="p">();</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"done</span> +</pre></div></p> <h3 id="server">Server</h3> -<p>[snippet:path=tutorial/d/client.d:lang=cpp:lines=19,64]</p> +<p><div class="codehilite"><pre><span class="n">module</span> <span class="n">client</span><span class="p">;</span></p> +<p><span class="n">import</span> <span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">base</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">codegen</span><span class="p">.</span><span class="n">client</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">protocol</span><span class="p">.</span><span class="n">binary</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span><span class="p">.</span><span class="n">buffered</span><span class="p">;</span> +<span class="n">import</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span><span class="p">.</span><span class="n">socket</span><span class="p">;</span></p> +<p><span class="n">import</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">Calculator</span><span class="p">;</span> +<span class="n">import</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">tutorial_types</span><span class="p">;</span></p> +<p><span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> + <span class="k">auto</span> <span class="n">socket</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TSocket</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="mi">9090</span><span class="p">);</span> + <span class="k">auto</span> <span class="n">transport</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TBufferedTransport</span><span class="p">(</span><span class="n">socket</span><span class="p">);</span> + <span class="k">auto</span> <span class="n">protocol</span> <span class="o">=</span> <span class="n">tBinaryProtocol</span><span class="p">(</span><span class="n">transport</span><span class="p">);</span> + <span class="k">auto</span> <span class="n">client</span> <span class="o">=</span> <span class="n">tClient</span><span class="o">!</span><span class="n">Calculator</span><span class="p">(</span><span class="n">protocol</span><span class="p">);</span></p> +<p><span class="n">transport</span><span class="p">.</span><span class="n">open</span><span class="p">();</span></p> +<p><span class="n">client</span><span class="p">.</span><span class="n">ping</span><span class="p">();</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"ping()"</span><span class="p">);</span></p> +<p><span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"1 + 1 = %s"</span><span class="p">,</span> <span class="n">sum</span><span class="p">);</span></p> +<p><span class="k">auto</span> <span class="n">work</span> <span class="o">=</span> <span class="n">Work</span><span class="p">();</span> + <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">=</span> <span class="n">Operation</span><span class="p">.</span><span class="n">DIVIDE</span><span class="p">;</span> + <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> + <span class="n">work</span><span class="p">.</span><span class="n">num2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> + <span class="k">try</span> <span class="p">{</span> + <span class="kt">int</span> <span class="n">quotient</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">work</span><span class="p">);</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"Whoa we can divide by 0"</span><span class="p">);</span> + <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">InvalidOperation</span> <span class="n">io</span><span class="p">)</span> <span class="p">{</span> + <span class="n">writeln</span><span class="p">(</span><span class="s">"Invalid operation: "</span> <span class="o">~</span> <span class="n">io</span><span class="p">.</span><span class="n">why</span><span class="p">);</span> + <span class="p">}</span></p> +<p><span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">=</span> <span class="n">Operation</span><span class="p">.</span><span class="n">SUBTRACT</span><span class="p">;</span> + <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> + <span class="n">work</span><span class="p">.</span><span class="n">num2</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">diff</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">work</span><span class="p">);</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"15 - 10 = %s"</span><span class="p">,</span> <span class="n">diff</span><span class="p">);</span></p> +<p><span class="k">auto</span> <span class="n">log</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">getStruct</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> + <span class="n">writefln</span><span class="p">(</span><span class="s">"Check log: %s"</span><span class="p">,</span> <span class="n">log</span> +</pre></div></p> <h2 id="additional-information">Additional Information</h2> </div>