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 #ifndef PVMF_CLIENTSERVER_SOCKET_NODE_H_INCLUDED 19 #define PVMF_CLIENTSERVER_SOCKET_NODE_H_INCLUDED 20 21 #ifndef OSCL_SOCKET_TYPES_H_INCLUDED 22 #include "oscl_socket_types.h" 23 #endif 24 #ifndef PVMF_CLIENTSERVER_SOCKET_TUNEABLES_H_INCLUDED 25 #include "pvmf_clientserver_socket_tuneables.h" 26 #endif 27 28 #ifndef PVMF_CLIENTSERVER_SOCKET_PORT_H_INCLUDED 29 #include "pvmf_clientserver_socket_port.h" 30 #endif 31 32 //Logger macros 33 #define PVMF_SOCKETNODE_LOGSTACKTRACE(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_REL,iLogger,PVLOGMSG_STACK_TRACE,m); 34 #define PVMF_SOCKETNODE_LOGINFO(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_REL,iLogger,PVLOGMSG_INFO,m); 35 #define PVMF_SOCKETNODE_LOGERROR(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_REL,iLogger,PVLOGMSG_ERR,m); 36 37 //memory allocator type for this node. 38 typedef OsclMemAllocator PVMFSocketNodeAllocator; 39 40 //Node command type. 41 typedef PVMFGenericNodeCommand<PVMFSocketNodeAllocator> PVMFSocketNodeCommandBase; 42 class PVMFSocketNodeCommand: public PVMFSocketNodeCommandBase 43 { 44 public: 45 46 }; 47 48 //Default vector reserve size 49 #define PVMF_SOCKET_NODE_COMMAND_VECTOR_RESERVE 10 50 51 //Starting value for command IDs 52 #define PVMF_SOCKET_NODE_COMMAND_ID_START 6000 53 54 class PVLogger; 55 class PVMFClientServerSocketPort; 56 57 /** 58 * Mem pool class 59 */ 60 class PVMFSocketNodeMemPool 61 { 62 public: 63 PVMFSocketNodeMemPool(uint32 aMemPoolNumBufs = DEFAULT_SOCKET_NODE_MEDIA_MSGS_NUM); 64 ~PVMFSocketNodeMemPool()65 virtual ~PVMFSocketNodeMemPool() 66 { 67 if (iMediaDataMemPool != NULL) 68 { 69 iMediaDataMemPool->removeRef(); 70 iMediaDataMemPool = NULL; 71 } 72 }; 73 getMediaDataImpl(uint32 size)74 OsclSharedPtr<PVMFMediaDataImpl> getMediaDataImpl(uint32 size) 75 { 76 return (iSocketAllocSharedPtr->createSharedBuffer(size)); 77 } 78 resizeSocketDataBuffer(OsclSharedPtr<PVMFMediaDataImpl> & aSharedBuffer)79 void resizeSocketDataBuffer(OsclSharedPtr<PVMFMediaDataImpl>& aSharedBuffer) 80 { 81 if (iSocketAllocSharedPtr.GetRep() != NULL) 82 { 83 iSocketAllocSharedPtr->ResizeMemoryFragment(aSharedBuffer); 84 } 85 } 86 87 void notifyfreechunkavailable(OsclMemPoolFixedChunkAllocatorObserver& aObserver, 88 uint32 aSize, 89 OsclAny* aContextData = NULL) 90 { 91 if (iSocketAllocSharedPtr.GetRep() != NULL) 92 { 93 iSocketAllocSharedPtr->notifyfreechunkavailable(aObserver, aSize, aContextData); 94 } 95 } 96 CancelFreeChunkAvailableCallback()97 void CancelFreeChunkAvailableCallback() 98 { 99 if (iSocketAllocSharedPtr.GetRep() != NULL) 100 { 101 iSocketAllocSharedPtr->CancelFreeChunkAvailableCallback(); 102 } 103 } 104 105 OsclSharedPtr<PVMFSharedSocketDataBufferAlloc> iSocketAllocSharedPtr; 106 107 // Memory pool for media data objects 108 OsclMemPoolFixedChunkAllocator* iMediaDataMemPool; 109 110 int32 iPortTag; 111 112 // Allocator created in this node, for TCP only. 113 PVMFSMSharedBufferAllocWithReSize* iInternalAlloc; 114 }; 115 116 class PVMFClientServerSocketNode; 117 118 /* 119 ** Socket activity class is used to save Oscl socket or DNS results 120 */ 121 class PVMFSocketActivity 122 { 123 public: PVMFSocketActivity()124 PVMFSocketActivity(): iValid(false) 125 { 126 } PVMFSocketActivity(PVMFStatus aStatus,int32 aId,int32 aFxn,int32 aEvent,int32 aError)127 PVMFSocketActivity(PVMFStatus aStatus, int32 aId, int32 aFxn, int32 aEvent, int32 aError) 128 { 129 Set(aStatus, aId, aFxn, aEvent, aError); 130 } Set(PVMFStatus aStatus,int32 aId,int32 aFxn,int32 aEvent,int32 aError)131 void Set(PVMFStatus aStatus, int32 aId, int32 aFxn, int32 aEvent, int32 aError) 132 { 133 iValid = true; 134 iStatus = aStatus; 135 iId = aId; 136 iFxn = aFxn; 137 iEvent = aEvent; 138 iError = aError; 139 } 140 bool iValid; 141 PVMFStatus iStatus; 142 int32 iId; 143 int32 iFxn; 144 int32 iEvent; 145 int32 iError; 146 147 private: 148 149 }; 150 151 //Individual asynchronous operations and wait states that make up a send sequence. 152 enum TPVSocketPortSendOperation 153 { 154 EPVSocketPortSendOperation_None 155 , EPVSocketPortSendOperation_Send 156 , EPVSocketPortSendOperation_Last 157 }; 158 159 //Individual asynchronous operations and wait states that make up a receive sequence. 160 enum TPVSocketPortRecvOperation 161 { 162 EPVSocketPortRecvOperation_None 163 , EPVSocketPortRecvOperation_Recv 164 , EPVSocketPortRecvOperation_WaitOnConnectedPort 165 , EPVSocketPortRecvOperation_WaitOnMemory 166 , EPVSocketPortRecvOperation_Last 167 }; 168 169 enum TPVSocketPortSequence 170 { 171 EPVSocketPortSequence_None 172 , EPVSocketPortSequence_RequestPort 173 , EPVSocketPortSequence_InputDataMsg 174 , EPVSocketPortSequence_InputDisconnectMsg 175 , EPVSocketPortSequence_SocketCleanup 176 , EPVSocketPortSequence_Last 177 }; 178 179 class SocketPortState 180 { 181 public: SocketPortState()182 SocketPortState(): iSequence(EPVSocketPortSequence_None) 183 , iSequenceStatus(PVMFSuccess) 184 , iSendOperation(EPVSocketPortSendOperation_None) 185 , iSendOperationStatus(PVMFSuccess) 186 , iSendOperationCanceled(false) 187 , iRecvOperation(EPVSocketPortRecvOperation_None) 188 , iRecvOperationStatus(PVMFSuccess) 189 , iRecvOperationCanceled(false) 190 {} 191 192 TPVSocketPortSequence iSequence; 193 PVMFStatus iSequenceStatus; 194 195 TPVSocketPortSendOperation iSendOperation; 196 PVMFStatus iSendOperationStatus; 197 bool iSendOperationCanceled; 198 199 TPVSocketPortRecvOperation iRecvOperation; 200 PVMFStatus iRecvOperationStatus; 201 bool iRecvOperationCanceled; 202 }; 203 204 /* 205 ** SocketPortConfig contains all the information associated with a port including 206 ** the Oscl socket, memory pool, and status. 207 */ 208 class SocketPortConfig : public OsclMemPoolFixedChunkAllocatorObserver 209 { 210 public: SocketPortConfig()211 SocketPortConfig() 212 { 213 iPVMFPort = NULL; 214 iSockId = 0; 215 iTCPSocket = NULL; 216 iMemPool = NULL; 217 iContainer = NULL; 218 iTag = PVMF_CLIENTSERVER_SOCKET_NODE_PORT_TYPE_UNKNOWN; 219 }; 220 221 void DoSetSocketPortMemAllocator(PVLogger*, OsclSharedPtr<PVMFSharedSocketDataBufferAlloc> aAlloc); 222 void CleanupMemPools(); 223 224 void freechunkavailable(OsclAny* aContextData); 225 226 SocketPortState iState; 227 228 PVMFClientServerSocketPort *iPVMFPort; 229 uint32 iSockId; 230 OsclTCPSocket* iTCPSocket; 231 232 //socket pending request and state 233 PVMFSharedMediaDataPtr iPendingRecvMediaData, iPendingSendMediaData; 234 235 //Fixed chunk memory pool for media messages 236 PVMFSocketNodeMemPool *iMemPool; 237 238 PVMFClientServerSocketNode* iContainer; 239 int32 iTag; 240 241 PVMFSocketActivity iSocketRecvActivity; 242 }; 243 244 /* 245 ** Memory pool sizes. 246 */ 247 #define SIMPLE_MEDIA_BUF_CLASS_SIZE 128 /*oscl_mem_aligned_size(sizeof(PVMFSimpleMediaBuffer) + oscl_mem_aligned_size(sizeof(OsclRefCounterSA<SimpleMediaBufferCombinedCleanupSA>)*/ 248 #define MEDIA_DATA_CLASS_SIZE 128 /*oscl_mem_aligned_size(sizeof(PVMFMediaData)) + oscl_mem_aligned_size(sizeof(OsclRefCounterDA)) + oscl_mem_aligned_size(sizeof(MediaDataCleanupDA)) + sizeof(PVMFMediaMsgHeader))*/ 249 250 /* 251 ** The Socket Node 252 */ 253 class PVMFClientServerSocketNode : public PVMFNodeInterface 254 , public OsclActiveObject 255 , public OsclSocketObserver 256 { 257 public: 258 259 OSCL_IMPORT_REF PVMFClientServerSocketNode(OsclTCPSocket* aSocketHandle, int32 aPriority); 260 OSCL_IMPORT_REF ~PVMFClientServerSocketNode(); 261 262 263 //**********begin PVMFSocketNodeExtensionInterface 264 OSCL_IMPORT_REF void addRef(); 265 OSCL_IMPORT_REF void removeRef(); 266 OSCL_IMPORT_REF bool queryInterface(const PVUuid& uuid, PVInterface*& iface); 267 OSCL_IMPORT_REF PVMFStatus AllocateConsecutivePorts(PvmfMimeString* aPortConfig, 268 uint32& aLowerPortNum, 269 uint32& aHigherPortNum, uint32& aStartPortNum); 270 OSCL_IMPORT_REF PVMFStatus SetMaxTCPRecvBufferSize(uint32 aBufferSize); 271 OSCL_IMPORT_REF PVMFStatus GetMaxTCPRecvBufferSize(uint32& aSize); 272 OSCL_IMPORT_REF PVMFStatus SetMaxTCPRecvBufferCount(uint32 aBufferSize); 273 OSCL_IMPORT_REF PVMFStatus GetMaxTCPRecvBufferCount(uint32& aSize); 274 275 //**********end PVMFSocketNodeExtensionInterface 276 277 //************ begin OsclSocketObserver 278 OSCL_IMPORT_REF void HandleSocketEvent(int32 aId, TPVSocketFxn aFxn, TPVSocketEvent aEvent, int32 aError); 279 //************ end OsclSocketObserver 280 281 //from PVMFNodeInterface 282 OSCL_IMPORT_REF PVMFStatus ThreadLogon(); 283 OSCL_IMPORT_REF PVMFStatus ThreadLogoff(); 284 OSCL_IMPORT_REF PVMFStatus GetCapability(PVMFNodeCapability& aNodeCapability); 285 OSCL_IMPORT_REF PVMFPortIter* GetPorts(const PVMFPortFilter* aFilter = NULL); 286 OSCL_IMPORT_REF PVMFCommandId QueryUUID(PVMFSessionId, const PvmfMimeString& aMimeType, 287 Oscl_Vector<PVUuid, PVMFSocketNodeAllocator>& aUuids, 288 bool aExactUuidsOnly = false, 289 const OsclAny* aContext = NULL); 290 OSCL_IMPORT_REF PVMFCommandId QueryInterface(PVMFSessionId, const PVUuid& aUuid, 291 PVInterface*& aInterfacePtr, 292 const OsclAny* aContext = NULL); 293 294 OSCL_IMPORT_REF PVMFCommandId RequestPort(PVMFSessionId aSession 295 , int32 aPortTag 296 , const PvmfMimeString* aPortConfig = NULL 297 , const OsclAny* aContext = NULL); 298 299 OSCL_IMPORT_REF PVMFCommandId ReleasePort(PVMFSessionId, PVMFPortInterface& aPort, const OsclAny* aContext = NULL); 300 OSCL_IMPORT_REF PVMFCommandId Init(PVMFSessionId, const OsclAny* aContext = NULL); 301 OSCL_IMPORT_REF PVMFCommandId Prepare(PVMFSessionId, const OsclAny* aContext = NULL); 302 OSCL_IMPORT_REF PVMFCommandId Start(PVMFSessionId, const OsclAny* aContext = NULL); 303 OSCL_IMPORT_REF PVMFCommandId Stop(PVMFSessionId, const OsclAny* aContext = NULL); 304 OSCL_IMPORT_REF PVMFCommandId Flush(PVMFSessionId, const OsclAny* aContext = NULL); 305 OSCL_IMPORT_REF PVMFCommandId Pause(PVMFSessionId, const OsclAny* aContext = NULL); 306 OSCL_IMPORT_REF PVMFCommandId Reset(PVMFSessionId, const OsclAny* aContext = NULL); 307 OSCL_IMPORT_REF PVMFCommandId CancelAllCommands(PVMFSessionId, const OsclAny* aContextData = NULL); 308 OSCL_IMPORT_REF PVMFCommandId CancelCommand(PVMFSessionId, PVMFCommandId aCmdId, const OsclAny* aContextData = NULL); 309 310 //from PVMFPortActivityHandler 311 void HandlePortActivity(const PVMFPortActivity& aActivity); 312 313 //These are some extra APIs that are used by Streaming Manager and are unique to socket node. 314 OSCL_IMPORT_REF bool GetPortConfig(PVMFPortInterface &aPort, OsclNetworkAddress &aLocalAdd, OsclNetworkAddress &aRemoteAdd); 315 OSCL_IMPORT_REF bool SetPortConfig(PVMFPortInterface &aPort, OsclNetworkAddress aLocalAdd, OsclNetworkAddress aRemoteAdd); 316 317 PVMFStatus CloseSocketConnection(); 318 OSCL_IMPORT_REF bool setSocketPortMemAllocator(PVMFPortInterface* aPort, 319 OsclSharedPtr<PVMFSharedSocketDataBufferAlloc> aAlloc); 320 321 322 private: 323 friend class SocketPortConfig; 324 325 //from OsclActiveObject 326 void Run(); 327 328 /********************************************* 329 * Socket extension interface 330 **********************************************/ 331 332 // Reference counter for extension 333 uint32 iExtensionRefCount; 334 335 /********************************************* 336 * Command Processing and Event Notification 337 **********************************************/ 338 339 //Command queue type 340 typedef PVMFNodeCommandQueue<PVMFSocketNodeCommand, PVMFSocketNodeAllocator> PVMFSocketNodeCmdQ; 341 342 PVMFSocketNodeCmdQ iPendingCmdQueue; 343 PVMFSocketNodeCmdQ iCurrentCmdQueue; 344 PVMFSocketNodeCmdQ iCancelCmdQueue; 345 346 void MoveCmdToCancelQueue(PVMFSocketNodeCommand& aCmd); 347 348 void CommandComplete(PVMFSocketNodeCmdQ&, 349 PVMFSocketNodeCommand&, 350 PVMFStatus, 351 OsclAny* aData = NULL, 352 PVUuid* aEventUUID = NULL, 353 int32* aEventCode = NULL); 354 355 void ReportErrorEvent(PVMFEventType aEventType, 356 OsclAny* aEventData = NULL, 357 PVUuid* aEventUUID = NULL, 358 int32* aEventCode = NULL); 359 360 void ReportInfoEvent(PVMFEventType aEventType, 361 OsclAny* aEventData = NULL, 362 PVUuid* aEventUUID = NULL, 363 int32* aEventCode = NULL); 364 365 PVMFCommandId QueueCommandL(PVMFSocketNodeCommand& aCmd); 366 367 bool CanProcessCommand(); 368 void ProcessCommand(PVMFSocketNodeCmdQ& aCmdQ, PVMFSocketNodeCommand&); 369 370 //Command handlers. 371 PVMFStatus DoRequestPort(PVMFSocketNodeCommand& aCmd); 372 PVMFStatus DoReset(PVMFSocketNodeCommand&); 373 PVMFStatus DoQueryUuid(PVMFSocketNodeCommand&); 374 PVMFStatus DoQueryInterface(PVMFSocketNodeCommand&); 375 PVMFStatus DoReleasePort(PVMFSocketNodeCommand&); 376 PVMFStatus DoInit(PVMFSocketNodeCommand&); 377 PVMFStatus DoPrepare(PVMFSocketNodeCommand&); 378 PVMFStatus DoStart(PVMFSocketNodeCommand&); 379 PVMFStatus DoStop(PVMFSocketNodeCommand&); 380 PVMFStatus DoFlush(PVMFSocketNodeCommand&); 381 PVMFStatus DoPause(PVMFSocketNodeCommand&); 382 PVMFStatus DoCancelAllCommands(PVMFSocketNodeCommand&); 383 PVMFStatus DoCancelCommand(PVMFSocketNodeCommand&); 384 385 PVMFStatus DoCancelCurrentCommand(PVMFSocketNodeCmdQ& aCmdQ, PVMFSocketNodeCommand& aCmd); 386 PVMFStatus DoStopNodeActivity(); 387 //PVMFClientServerSocketPort* iRequestedPort; 388 389 //node state ChangeExternalState(TPVMFNodeInterfaceState aNewState)390 void ChangeExternalState(TPVMFNodeInterfaceState aNewState) 391 { 392 iInterfaceState = aNewState; 393 } 394 395 //for error messages. 396 int32 iCommandErrorCode; 397 int32 iErrorEventErrorCode; ReportSocketNodeError(PVMFStatus aStatus,PVMFSocketNodeErrorEventType aEvent)398 void ReportSocketNodeError(PVMFStatus aStatus, PVMFSocketNodeErrorEventType aEvent) 399 { 400 PVUuid eventuuid = PVMFSocketNodeEventTypeUUID; 401 iErrorEventErrorCode = aEvent; 402 ReportErrorEvent(aStatus, NULL, &eventuuid, &iErrorEventErrorCode); 403 } 404 405 //Used to stop all port activity, this var counts down from N to 0, or 406 //has a value of (-1) to indicate it's inactive 407 int32 iNumStopPortActivityPending; 408 409 /********************************************* 410 * Oscl Socket Handling 411 **********************************************/ 412 413 void HandleRecvComplete(PVMFStatus, PVMFSocketActivity*, bool); 414 415 OsclSocketServ *iSockServ; 416 417 const int TIMEOUT_CONNECT; 418 const int TIMEOUT_SEND; 419 const int TIMEOUT_RECV; 420 const int TIMEOUT_SHUTDOWN; 421 422 int32 iMaxTcpRecvBufferSize; 423 int32 iMaxTcpRecvBufferCount; 424 425 /********************************************* 426 * Port Data Handling 427 **********************************************/ 428 429 bool CanProcessIncomingMsg(); 430 431 void ProcessIncomingMsg(); 432 433 434 /********************************************* 435 * Port & Socket Creation 436 **********************************************/ 437 438 PVMFStatus AllocatePortMemPool(int32 tag, PVMFSocketNodeMemPool* & aMemPool); 439 PVMFStatus AddPort(int32 tag); 440 void CleanupTCP(); 441 void CleanupPorts(); 442 443 uint32 iSocketID; 444 445 /********************************************* 446 * Port Data 447 **********************************************/ 448 449 PVMFClientServerSocketPort* iPVMFPort; 450 451 /********************************************* 452 * Operation and Sequence Handlers 453 **********************************************/ 454 455 PVMFStatus StartSequence(TPVSocketPortSequence, OsclAny* param = NULL); 456 void SequenceComplete(PVMFStatus); 457 458 bool CanReceive(); 459 460 PVMFStatus StartRecvOperation(); 461 PVMFStatus StartSendOperation(PVMFSharedMediaMsgPtr& aMsg); 462 void StartRecvWaitOnMemory(int32 aSize = 0); 463 void StartRecvWaitOnConnectedPort(PVMFSocketActivity& aSocketActivity); 464 465 PVMFStatus SendOperationComplete(PVMFStatus, PVMFSocketActivity*); 466 PVMFStatus RecvOperationComplete(PVMFStatus, PVMFSocketActivity*); 467 468 PVMFStatus CancelSendOperation(); 469 PVMFStatus CancelRecvOperation(); 470 471 /********************************************* 472 * Loggers, Error Trap, Allocator 473 **********************************************/ 474 475 PVLogger *iLogger; 476 PVLogger *iDataPathLogger; 477 OsclErrorTrapImp* iOsclErrorTrapImp; 478 OsclTCPSocket* iSocketHandle; 479 480 PVMFSocketNodeAllocator iAlloc; 481 SocketPortConfig* iSockConfig; 482 }; 483 484 #endif //PVMF_CLIENTSERVER_SOCKET_NODE_H_INCLUDED 485 486