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 #include "oscl_scheduler_ao.h" 20 #include "oscl_socket_accept.h" 21 22 //////////// Method ///////////////////// 23 NewL(OsclIPSocketI & c)24OsclAcceptMethod *OsclAcceptMethod::NewL(OsclIPSocketI& c) 25 { 26 OsclAny*p = c.Alloc().ALLOCATE(sizeof(OsclAcceptMethod)); 27 OsclError::LeaveIfNull(p); 28 OsclAcceptMethod* self = OSCL_PLACEMENT_NEW(p, OsclAcceptMethod(c)); 29 OsclError::LeaveIfNull(self); 30 OsclError::PushL(self); 31 self->ConstructL(); 32 OsclError::Pop(); 33 return self; 34 } 35 ConstructL()36void OsclAcceptMethod::ConstructL() 37 { 38 OsclAny*p = iContainer.Alloc().ALLOCATE(sizeof(OsclAcceptRequest)); 39 OsclError::LeaveIfNull(p); 40 OsclAcceptRequest* self = OSCL_PLACEMENT_NEW(p, OsclAcceptRequest(*this)); 41 OsclError::LeaveIfNull(self); 42 OsclError::PushL(self); 43 self->ConstructL(); 44 OsclError::Pop(); 45 iSocketRequestAO = self; 46 OsclSocketMethod::ConstructL(iSocketRequestAO); 47 } 48 ~OsclAcceptMethod()49OsclAcceptMethod::~OsclAcceptMethod() 50 { 51 if (AcceptRequest()) 52 { 53 AcceptRequest()->~OsclAcceptRequest(); 54 Alloc().deallocate(AcceptRequest()); 55 } 56 DiscardAcceptedSocket(); 57 } 58 Accept(int32 aTimeout)59TPVSocketEvent OsclAcceptMethod::Accept(int32 aTimeout) 60 { 61 //in case previous accepted socket was never 62 //retrieved... 63 DiscardAcceptedSocket(); 64 65 iAcceptedSocket = OsclSocketI::NewL(Alloc()); 66 67 if (iAcceptedSocket->Open(*SocketServ()) != OsclErrNone) 68 { 69 DiscardAcceptedSocket(); 70 return EPVSocketFailure; 71 } 72 73 if (!StartMethod(aTimeout)) 74 { 75 DiscardAcceptedSocket(); 76 return EPVSocketFailure; 77 } 78 79 AcceptRequest()->Accept(*iAcceptedSocket); 80 return EPVSocketPending; 81 } 82 DiscardAcceptedSocket()83void OsclAcceptMethod::DiscardAcceptedSocket() 84 { 85 if (iAcceptedSocket) 86 { 87 iAcceptedSocket->~OsclSocketI(); 88 Alloc().deallocate(iAcceptedSocket); 89 } 90 iAcceptedSocket = NULL; 91 } 92 GetAcceptedSocket()93OsclSocketI *OsclAcceptMethod::GetAcceptedSocket() 94 { 95 if (iAcceptedSocket) 96 { 97 //Clear accepted socket once it has been retrieved. 98 OsclSocketI *sock = iAcceptedSocket; 99 iAcceptedSocket = NULL; 100 return sock; 101 } 102 else 103 return NULL; 104 } 105 Run()106void OsclAcceptMethod::Run() 107 { 108 //The request timed out! 109 110 //must discard accepted socket in addition 111 //to the usual timeout Run 112 DiscardAcceptedSocket(); 113 114 //call the base class Run 115 OsclSocketMethod::Run(); 116 } 117 118 //////////// Request ///////////////////// 119 Accept(OsclSocketI & aSocket)120void OsclAcceptRequest::Accept(OsclSocketI &aSocket) 121 { 122 OsclAny *p = NewRequest(sizeof(AcceptParam)); 123 if (!p) 124 PendComplete(OsclErrNoMemory); 125 else 126 { 127 iParam = OSCL_PLACEMENT_NEW(p, AcceptParam(aSocket)); 128 if (!iParam) 129 PendComplete(OsclErrNoMemory); 130 else 131 SocketI()->Accept(*Param(), *this); 132 } 133 } 134 Run()135void OsclAcceptRequest::Run() 136 { 137 //The request was completed by the socket server 138 139 //may need to discard accepted socket in addition to 140 //the usual request completion. 141 if (Status() != OSCL_REQUEST_ERR_NONE) 142 ((OsclAcceptMethod*)&iContainer)->DiscardAcceptedSocket(); 143 144 //call the base class Run 145 OsclSocketRequestAO::Run(); 146 } 147 148 149