Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/threadserversession_8cpp-source.html
changeset 1 25a17d01db0c
equal deleted inserted replaced
0:89d6a7a84779 1:25a17d01db0c
       
     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
       
     2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
       
     3 <title>TB10.1 Example Applications: examples/Base/IPC/AdvancedClientServerExample/ThreadServer/src/threadserversession.cpp Source File</title>
       
     4 <link href="doxygen.css" rel="stylesheet" type="text/css">
       
     5 <link href="tabs.css" rel="stylesheet" type="text/css">
       
     6 </head><body>
       
     7 <!-- Generated by Doxygen 1.5.3 -->
       
     8 <h1>examples/Base/IPC/AdvancedClientServerExample/ThreadServer/src/threadserversession.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
       
     9 <a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
       
    10 <a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
       
    11 <a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
       
    12 <a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
       
    13 <a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
       
    14 <a name="l00007"></a>00007 <span class="comment">//</span>
       
    15 <a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
       
    16 <a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
       
    17 <a name="l00010"></a>00010 <span class="comment">//</span>
       
    18 <a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
       
    19 <a name="l00012"></a>00012 <span class="comment">//</span>
       
    20 <a name="l00013"></a>00013 <span class="comment">// Description:</span>
       
    21 <a name="l00014"></a>00014 <span class="comment">//</span>
       
    22 <a name="l00015"></a>00015 
       
    23 <a name="l00016"></a>00016 <span class="preprocessor">#include "threadserversession.h"</span>
       
    24 <a name="l00017"></a>00017 
       
    25 <a name="l00018"></a>00018 
       
    26 <a name="l00019"></a>00019 <span class="comment">/*****************CThreadServerSession**********************/</span>
       
    27 <a name="l00023"></a><a class="code" href="class_c_thread_server_session.html#f108a1cbe1843ce74693f272482aa612">00023</a> <a class="code" href="class_c_thread_server_session.html">CThreadServerSession</a>* <a class="code" href="class_c_thread_server_session.html#f108a1cbe1843ce74693f272482aa612">CThreadServerSession::NewL</a>(<a class="code" href="class_c_thread_server.html">CThreadServer</a>&amp; aServer)
       
    28 <a name="l00024"></a>00024         {
       
    29 <a name="l00025"></a>00025         <a class="code" href="class_c_thread_server_session.html">CThreadServerSession</a>* s = <span class="keyword">new</span>(ELeave) <a class="code" href="class_c_thread_server_session.html">CThreadServerSession</a>(aServer);
       
    30 <a name="l00026"></a>00026         CleanupStack::PushL(s);
       
    31 <a name="l00027"></a>00027         s-&gt;<a class="code" href="class_c_thread_server_session.html#484bcf4dcb4b329fce39103194fd226e">ConstructL</a>();
       
    32 <a name="l00028"></a>00028         CleanupStack::Pop();
       
    33 <a name="l00029"></a>00029         <span class="keywordflow">return</span> s;       
       
    34 <a name="l00030"></a>00030         }
       
    35 <a name="l00034"></a>00034 CThreadServerSession::CThreadServerSession(<a class="code" href="class_c_thread_server.html">CThreadServer</a>&amp; aServer)
       
    36 <a name="l00035"></a>00035 :iServer(aServer)
       
    37 <a name="l00036"></a>00036         {
       
    38 <a name="l00037"></a>00037         }
       
    39 <a name="l00038"></a>00038 <span class="keywordtype">void</span> CThreadServerSession::ConstructL()
       
    40 <a name="l00039"></a>00039         {
       
    41 <a name="l00040"></a>00040         }
       
    42 <a name="l00044"></a><a class="code" href="class_c_thread_server_session.html#b5e11a0f7dc90af28b45d79748206237">00044</a> <a class="code" href="class_c_thread_server_session.html#b5e11a0f7dc90af28b45d79748206237">CThreadServerSession::~CThreadServerSession</a>()
       
    43 <a name="l00045"></a>00045         {
       
    44 <a name="l00046"></a>00046         <span class="keyword">delete</span> iAsyncRequestHandler;            
       
    45 <a name="l00047"></a>00047         iServer.<a class="code" href="class_c_thread_server.html#bebdf4dc3dbbe186b9bf20ea3d16681f">CloseLogicalChannel</a>();
       
    46 <a name="l00048"></a>00048         iServer.<a class="code" href="class_c_thread_server.html#dd52c0ec21335f60489aa9a2478c11ab">DecrementRefCount</a>();
       
    47 <a name="l00049"></a>00049         }
       
    48 <a name="l00054"></a><a class="code" href="class_c_thread_server_session.html#2c10e6f53ff5bf52b4d85243db3c8397">00054</a> <span class="keywordtype">void</span> <a class="code" href="class_c_thread_server_session.html#2c10e6f53ff5bf52b4d85243db3c8397">CThreadServerSession::CreateL</a>()
       
    49 <a name="l00055"></a>00055 <span class="comment">//</span>
       
    50 <a name="l00056"></a>00056 <span class="comment">// 2nd phase construct for sessions - called by the CServer framework</span>
       
    51 <a name="l00057"></a>00057 <span class="comment">//</span>
       
    52 <a name="l00058"></a>00058         {
       
    53 <a name="l00059"></a>00059         iServer.<a class="code" href="class_c_thread_server.html#e8800ec9ca3333ebffdbc790b38b218e">IncrementRefCount</a>();            
       
    54 <a name="l00060"></a>00060         <span class="comment">//load dummy ldd</span>
       
    55 <a name="l00061"></a>00061         User::LeaveIfError(iServer.<a class="code" href="class_c_thread_server.html#b276a628b73ccf6b080c3e5f69a9cbba">LoadDevice</a>());
       
    56 <a name="l00062"></a>00062         iAsyncRequestHandler = CAsyncHandler::NewL(iServer);    
       
    57 <a name="l00063"></a>00063         }
       
    58 <a name="l00067"></a><a class="code" href="class_c_thread_server_session.html#de90612fc801c75d8415aff6eba97027">00067</a> <span class="keywordtype">void</span> <a class="code" href="class_c_thread_server_session.html#de90612fc801c75d8415aff6eba97027">CThreadServerSession::ServiceL</a>(<span class="keyword">const</span> RMessage2&amp; aMessage)
       
    59 <a name="l00068"></a>00068         {
       
    60 <a name="l00069"></a>00069         TInt r = KErrNone;
       
    61 <a name="l00070"></a>00070         <span class="keywordflow">switch</span>(aMessage.Function())
       
    62 <a name="l00071"></a>00071                 {                               
       
    63 <a name="l00072"></a>00072                 <span class="keywordflow">case</span> EThreadServerLoadDeviceDriver:
       
    64 <a name="l00073"></a>00073                         aMessage.Complete(iServer.<a class="code" href="class_c_thread_server.html#b276a628b73ccf6b080c3e5f69a9cbba">LoadDevice</a>());
       
    65 <a name="l00074"></a>00074                         <span class="keywordflow">break</span>;
       
    66 <a name="l00075"></a>00075                 
       
    67 <a name="l00076"></a>00076                 <span class="keywordflow">case</span> EThreadServerOpenDriver:
       
    68 <a name="l00077"></a>00077                         aMessage.Complete(iServer.<a class="code" href="class_c_thread_server.html#50d46df581a8dd334babd2441bd21154">OpenLogicalChannel</a>());
       
    69 <a name="l00078"></a>00078                         <span class="keywordflow">break</span>;
       
    70 <a name="l00079"></a>00079                         
       
    71 <a name="l00080"></a>00080                 <span class="keywordflow">case</span> EThreadServerSendData: <span class="comment">//async</span>
       
    72 <a name="l00081"></a>00081                         r = iAsyncRequestHandler-&gt;ProcessRequest(aMessage);
       
    73 <a name="l00082"></a>00082                         <span class="keywordflow">if</span> (r!=KErrNone)
       
    74 <a name="l00083"></a>00083                                 aMessage.Complete(r);
       
    75 <a name="l00084"></a>00084                         <span class="keywordflow">break</span>;
       
    76 <a name="l00085"></a>00085                         
       
    77 <a name="l00086"></a>00086                 <span class="keywordflow">case</span> EThreadServerSendDataCancel: <span class="comment">//cancel async</span>
       
    78 <a name="l00087"></a>00087                         iAsyncRequestHandler-&gt;Cancel(); 
       
    79 <a name="l00088"></a>00088                         aMessage.Complete(KErrNone); 
       
    80 <a name="l00089"></a>00089                         <span class="keywordflow">break</span>;
       
    81 <a name="l00090"></a>00090                         
       
    82 <a name="l00091"></a>00091                 <span class="keywordflow">case</span> EThreadServerUnloadDeviceDriver:
       
    83 <a name="l00092"></a>00092                         aMessage.Complete(iServer.<a class="code" href="class_c_thread_server.html#5fff82f416011501a5d3e5040a24d515">UnloadDevice</a>());
       
    84 <a name="l00093"></a>00093                         <span class="keywordflow">break</span>;
       
    85 <a name="l00094"></a>00094                         
       
    86 <a name="l00095"></a>00095                 <span class="keywordflow">default</span>:
       
    87 <a name="l00096"></a>00096                         User::Leave(KErrNotSupported);
       
    88 <a name="l00097"></a>00097                         <span class="keywordflow">break</span>;
       
    89 <a name="l00098"></a>00098                 }
       
    90 <a name="l00099"></a>00099 
       
    91 <a name="l00100"></a>00100         }
       
    92 <a name="l00101"></a>00101 <span class="comment">/*****************CAsyncHandler**********************/</span>
       
    93 <a name="l00105"></a>00105 CThreadServerSession::CAsyncHandler* <a class="code" href="class_c_thread_server_session.html#f108a1cbe1843ce74693f272482aa612">CThreadServerSession::CAsyncHandler::NewL</a>(<a class="code" href="class_c_thread_server.html">CThreadServer</a>&amp; aServer)
       
    94 <a name="l00106"></a>00106         {
       
    95 <a name="l00107"></a>00107         CAsyncHandler* <span class="keyword">self</span> = <span class="keyword">new</span>(ELeave) CAsyncHandler(aServer);
       
    96 <a name="l00108"></a>00108         CleanupStack::PushL(<span class="keyword">self</span>);
       
    97 <a name="l00109"></a>00109         <span class="keyword">self</span> -&gt;ConstructL();
       
    98 <a name="l00110"></a>00110         CleanupStack::Pop();
       
    99 <a name="l00111"></a>00111         <span class="keywordflow">return</span> <span class="keyword">self</span>;
       
   100 <a name="l00112"></a>00112         }
       
   101 <a name="l00116"></a>00116 CThreadServerSession::CAsyncHandler::~CAsyncHandler()
       
   102 <a name="l00117"></a>00117         {
       
   103 <a name="l00118"></a>00118         Cancel();
       
   104 <a name="l00119"></a>00119         iMessageArray.Close();
       
   105 <a name="l00120"></a>00120         }
       
   106 <a name="l00124"></a>00124 <span class="keywordtype">void</span> CThreadServerSession::CAsyncHandler::RunL()
       
   107 <a name="l00125"></a>00125         {
       
   108 <a name="l00126"></a>00126         <span class="comment">// complete the request</span>
       
   109 <a name="l00127"></a>00127         Complete(iStatus.Int());
       
   110 <a name="l00128"></a>00128         
       
   111 <a name="l00129"></a>00129         <span class="comment">//continue to execute next request if there is any</span>
       
   112 <a name="l00130"></a>00130         ExecuteFirstRequestInArray();
       
   113 <a name="l00131"></a>00131         }
       
   114 <a name="l00135"></a>00135 <span class="keywordtype">void</span> CThreadServerSession::CAsyncHandler::DoCancel()
       
   115 <a name="l00136"></a>00136         {
       
   116 <a name="l00137"></a>00137         iServer.<a class="code" href="class_c_thread_server.html#fa4844b8aec06286cc5a29d11f12ae47">CancelSendData</a>();
       
   117 <a name="l00138"></a>00138         <span class="comment">// complete the request</span>
       
   118 <a name="l00139"></a>00139         Complete(iStatus.Int());
       
   119 <a name="l00140"></a>00140         }
       
   120 <a name="l00144"></a>00144 CThreadServerSession::CAsyncHandler::CAsyncHandler(<a class="code" href="class_c_thread_server.html">CThreadServer</a>&amp; aServer)
       
   121 <a name="l00145"></a>00145         :CActive(EPriorityStandard), iServer(aServer)
       
   122 <a name="l00146"></a>00146         {
       
   123 <a name="l00147"></a>00147         CActiveScheduler::Add(<span class="keyword">this</span>);
       
   124 <a name="l00148"></a>00148         }
       
   125 <a name="l00149"></a>00149 
       
   126 <a name="l00150"></a>00150 <span class="keywordtype">void</span> CThreadServerSession::CAsyncHandler::ConstructL()
       
   127 <a name="l00151"></a>00151         {
       
   128 <a name="l00152"></a>00152         }
       
   129 <a name="l00158"></a>00158 TInt CThreadServerSession::CAsyncHandler::ProcessRequest(<span class="keyword">const</span> RMessage2&amp; aMessage)
       
   130 <a name="l00159"></a>00159         {
       
   131 <a name="l00160"></a>00160         TInt ret;
       
   132 <a name="l00161"></a>00161         <span class="comment">//store message</span>
       
   133 <a name="l00162"></a>00162         TMessage newMessage;
       
   134 <a name="l00163"></a>00163         newMessage.Message() = aMessage;
       
   135 <a name="l00164"></a>00164         ret= iMessageArray.Append(newMessage);
       
   136 <a name="l00165"></a>00165         <span class="keywordflow">if</span> (ret != KErrNone)
       
   137 <a name="l00166"></a>00166                 <span class="keywordflow">return</span> ret;
       
   138 <a name="l00167"></a>00167         
       
   139 <a name="l00168"></a>00168         <span class="comment">// only kick off the first request in the array when there is only one in the array</span>
       
   140 <a name="l00169"></a>00169         <span class="keywordflow">if</span> (iMessageArray.Count() == 1) 
       
   141 <a name="l00170"></a>00170                 ret = ExecuteFirstRequestInArray();
       
   142 <a name="l00171"></a>00171 
       
   143 <a name="l00172"></a>00172         <span class="keywordflow">return</span> ret;
       
   144 <a name="l00173"></a>00173         }
       
   145 <a name="l00178"></a>00178 TInt CThreadServerSession::CAsyncHandler::ExecuteFirstRequestInArray()
       
   146 <a name="l00179"></a>00179         {
       
   147 <a name="l00180"></a>00180         TInt r = KErrNone;
       
   148 <a name="l00181"></a>00181         <span class="keywordflow">if</span> (iMessageArray.Count() != 0)
       
   149 <a name="l00182"></a>00182                 {
       
   150 <a name="l00183"></a>00183                 <span class="keyword">const</span> RMessage2&amp; message = iMessageArray[0].Message();
       
   151 <a name="l00184"></a>00184                 <span class="keywordflow">switch</span> (message.Function())
       
   152 <a name="l00185"></a>00185                         {
       
   153 <a name="l00186"></a>00186                         <span class="keywordflow">case</span> EThreadServerSendData:
       
   154 <a name="l00187"></a>00187                                 {
       
   155 <a name="l00188"></a>00188                                 TBuf8&lt;KThreadServerMaxMessageLen&gt; sendObject;
       
   156 <a name="l00189"></a>00189                                 r= message.Read(0, sendObject);
       
   157 <a name="l00190"></a>00190                                 <span class="keywordflow">if</span> (r == KErrNone)
       
   158 <a name="l00191"></a>00191                                         {
       
   159 <a name="l00192"></a>00192                                         r = iServer.<a class="code" href="class_c_thread_server.html#5d9fd415bca412e6f73b8c80cdf60f29">SendDataToDevice</a>(iStatus, sendObject);
       
   160 <a name="l00193"></a>00193                                         <span class="keywordflow">if</span> (r==KErrNone)
       
   161 <a name="l00194"></a>00194                                                 SetActive();
       
   162 <a name="l00195"></a>00195                                         }
       
   163 <a name="l00196"></a>00196                                 }
       
   164 <a name="l00197"></a>00197                                 <span class="keywordflow">break</span>;
       
   165 <a name="l00198"></a>00198                                 
       
   166 <a name="l00199"></a>00199                         <span class="keywordflow">default</span>:
       
   167 <a name="l00200"></a>00200                                 <span class="keywordflow">break</span>;
       
   168 <a name="l00201"></a>00201                         }
       
   169 <a name="l00202"></a>00202                 }
       
   170 <a name="l00203"></a>00203         <span class="keywordflow">return</span> r; 
       
   171 <a name="l00204"></a>00204         }
       
   172 <a name="l00208"></a>00208 <span class="keywordtype">void</span> CThreadServerSession::CAsyncHandler::Complete(TInt aResult)
       
   173 <a name="l00209"></a>00209         {
       
   174 <a name="l00210"></a>00210         iMessageArray[0].Message().Complete(aResult);
       
   175 <a name="l00211"></a>00211         iMessageArray.Remove(0);
       
   176 <a name="l00212"></a>00212         iServer.<a class="code" href="class_c_thread_server.html#631c9159f8d0d91fc3136f1012663851">UpdateDriverState</a>(<a class="code" href="class_c_thread_server.html#fb6315c557a5690492f0441ae493b512b5db379d7cf833defa8690a3f5d0738e">CThreadServer::ELogicalChannelOpened</a>);
       
   177 <a name="l00213"></a>00213         }
       
   178 <a name="l00214"></a>00214 
       
   179 <a name="l00215"></a>00215 <span class="comment">/***************TMessage****************/</span>
       
   180 <a name="l00219"></a>00219 <span class="keyword">const</span> RMessage2&amp; CThreadServerSession::CAsyncHandler::TMessage::Message()<span class="keyword"> const</span>
       
   181 <a name="l00220"></a>00220 <span class="keyword">        </span>{
       
   182 <a name="l00221"></a>00221         <span class="keywordflow">return</span> iMessage;
       
   183 <a name="l00222"></a>00222         }
       
   184 <a name="l00223"></a>00223 RMessage2&amp; CThreadServerSession::CAsyncHandler::TMessage::Message()
       
   185 <a name="l00224"></a>00224         {
       
   186 <a name="l00225"></a>00225         <span class="keywordflow">return</span> iMessage; 
       
   187 <a name="l00226"></a>00226         }
       
   188 <a name="l00227"></a>00227 
       
   189 <a name="l00228"></a>00228 <span class="comment">//EOF</span>
       
   190 </pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Thu Jan 21 10:32:56 2010 for TB10.1 Example Applications by&nbsp;
       
   191 <a href="http://www.doxygen.org/index.html">
       
   192 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
       
   193 </body>
       
   194 </html>