1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 19 #ifndef OSCL_SOCKET_SERV_IMP_PV_H_INCLUDED 20 #define OSCL_SOCKET_SERV_IMP_PV_H_INCLUDED 21 22 #include "oscl_socket_serv_imp_base.h" 23 #include "oscl_socket_serv_imp_reqlist.h" 24 #include "oscl_socket_tuneables.h" 25 26 27 /** PV socket server implementation 28 */ 29 30 #if PV_SOCKET_SERVER_IS_THREAD 31 #include "oscl_semaphore.h" 32 #include "oscl_mutex.h" 33 #else 34 #include "oscl_scheduler_ao.h" 35 #endif 36 37 class PVServiStats; 38 39 /** PV socket server implementation 40 */ 41 #if (PV_SOCKET_SERVER_IS_THREAD) 42 class OsclSocketServI: public HeapBase, public OsclSocketServIBase 43 #else 44 class OsclSocketServI: public OsclTimerObject, public OsclSocketServIBase 45 #endif 46 { 47 public: 48 static OsclSocketServI* NewL(Oscl_DefAlloc &a); 49 int32 Connect(uint32 aMessageSlots); 50 void Close(bool); 51 52 //check if calling context is server thread. 53 //in non-threaded implementation, will always return "true". 54 bool IsServerThread(); 55 56 private: 57 OsclSocketServI(Oscl_DefAlloc &a); 58 ~OsclSocketServI(); 59 void ConstructL(); 60 61 //socket request list. 62 OsclSocketServRequestList iSockServRequestList; 63 64 #if PV_SOCKET_SERVER_SELECT_LOOPBACK_SOCKET 65 //blocking select wakeup feature 66 class LoopbackSocket 67 { 68 public: LoopbackSocket()69 LoopbackSocket() 70 { 71 iEnable = false; 72 iContainer = NULL; 73 } 74 bool iEnable; 75 void Read(); 76 void ProcessSelect(TOsclSocket&); 77 void Init(OsclSocketServI* aContainer); 78 void Cleanup(); 79 void Write(); 80 #if PV_OSCL_SOCKET_STATS_LOGGING 81 OsclSocketStats iStats; 82 #endif 83 private: 84 TOsclSockAddr iAddr; 85 TOsclSocket iSocket; 86 OsclSocketServI* iContainer; 87 }; 88 LoopbackSocket iLoopbackSocket; 89 #endif 90 uint32 iSelectPollIntervalMsec; 91 WakeupBlockingSelect()92 void WakeupBlockingSelect() 93 { 94 #if PV_SOCKET_SERVER_SELECT_LOOPBACK_SOCKET 95 if (iLoopbackSocket.iEnable) 96 iLoopbackSocket.Write(); 97 #endif 98 } 99 100 int32 StartServImp(); 101 void ConstructServImp(); 102 void CleanupServImp(); 103 void StopServImp(); 104 void ServerEntry(); 105 void ServerExit(); 106 107 #if PV_SOCKET_SERVER_IS_THREAD 108 TOsclThreadId iThreadId; 109 //start & exit semaphores. 110 OsclSemaphore iStart, iExit; 111 //thread exit flag 112 bool iClose; 113 public: 114 //this needs to be public for use by the thread routine. 115 void InThread(); 116 private: 117 #else 118 //for AO implemenation. 119 void Run(); 120 void WakeupAO(); 121 #if PV_SOCKET_SERVER_SELECT 122 int iNhandles; 123 int iNfds; 124 #endif 125 #endif 126 127 #if PV_SOCKET_SERVER_SELECT 128 //select flags. 129 fd_set iReadset, iWriteset, iExceptset; 130 void ProcessSocketRequests(int &, int &n); 131 #else 132 void ProcessSocketRequests(); 133 #endif 134 135 friend class OsclSocketServRequestList; 136 friend class LoopbackSocket; 137 138 friend class OsclTCPSocketI; 139 friend class OsclUDPSocketI; 140 friend class OsclSocketI; 141 friend class OsclDNSI; 142 friend class OsclSocketRequest; 143 friend class OsclSocketServ; 144 145 PVServiStats* iServiStats; 146 147 }; 148 149 /** A bitmask for socket select operations 150 */ 151 #define OSCL_READSET_FLAG 0x04 152 #define OSCL_WRITESET_FLAG 0x02 153 #define OSCL_EXCEPTSET_FLAG 0x01 154 155 156 #endif 157 158 159 160