/* * Get remote's name. */ PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock, pj_sockaddr_t *addr, int *namelen) { PJ_CHECK_STACK(); PJ_ASSERT_RETURN(sock && addr && namelen && *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); CPjSocket *pjSock = (CPjSocket*)sock; RSocket &rSock = pjSock->Socket(); // Socket must be connected. PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP); TInetAddr inetAddr; rSock.RemoteName(inetAddr); return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen); }
/* * Send data. */ PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock, const void *buf, pj_ssize_t *len, unsigned flags, const pj_sockaddr_t *to, int tolen) { pj_status_t status; PJ_CHECK_STACK(); PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL); // Return failure if access point is marked as down by app. PJ_SYMBIAN_CHECK_CONNECTION(); CPjSocket *pjSock = (CPjSocket*)sock; RSocket &rSock = pjSock->Socket(); // Only supports AF_INET for now PJ_ASSERT_RETURN(tolen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); TInetAddr inetAddr; status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)to, tolen, inetAddr); if (status != PJ_SUCCESS) return status; TPtrC8 data((const TUint8*)buf, (TInt)*len); TRequestStatus reqStatus; TSockXfrLength sentLen; rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen); User::WaitForRequest(reqStatus); if (reqStatus.Int()==KErrNone) { //For some reason TSockXfrLength is not returning correctly! //*len = (TInt) sentLen.Length(); return PJ_SUCCESS; } else return PJ_RETURN_OS_ERROR(reqStatus.Int()); }
/* * Accept incoming connections */ PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd, pj_sock_t *newsock, pj_sockaddr_t *addr, int *addrlen) { PJ_CHECK_STACK(); PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL); CPjSocket *pjSock = (CPjSocket*)serverfd; RSocket &rSock = pjSock->Socket(); // Create a 'blank' socket RSocket newSock; newSock.Open(PjSymbianOS::Instance()->SocketServ()); // Call Accept() TRequestStatus reqStatus; rSock.Accept(newSock, reqStatus); User::WaitForRequest(reqStatus); if (reqStatus != KErrNone) { return PJ_RETURN_OS_ERROR(reqStatus.Int()); } // Create PJ socket CPjSocket *newPjSock = new CPjSocket(pjSock->GetAf(), pjSock->GetSockType(), newSock); newPjSock->SetConnected(true); *newsock = (pj_sock_t) newPjSock; if (addr && addrlen) { return pj_sock_getpeername(*newsock, addr, addrlen); } return PJ_SUCCESS; }
/* * Receive data. */ PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock, void *buf, pj_ssize_t *len, unsigned flags, pj_sockaddr_t *from, int *fromlen) { PJ_CHECK_STACK(); PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL); PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL); PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL); // Return failure if access point is marked as down by app. PJ_SYMBIAN_CHECK_CONNECTION(); CPjSocket *pjSock = (CPjSocket*)sock; RSocket &rSock = pjSock->Socket(); if (pjSock->Reader()) { CPjSocketReader *reader = pjSock->Reader(); while (reader->IsActive() && !reader->HasData()) { User::WaitForAnyRequest(); } if (reader->HasData()) { TPtr8 data((TUint8*)buf, (TInt)*len); TInetAddr inetAddr; reader->ReadData(data, &inetAddr); *len = data.Length(); if (from && fromlen) { return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen); } else { return PJ_SUCCESS; } } } TInetAddr inetAddr; TRequestStatus reqStatus; TSockXfrLength recvLen; TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len); rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen); User::WaitForRequest(reqStatus); if (reqStatus == KErrNone) { //*len = (TInt)recvLen.Length(); *len = data.Length(); return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen); } else { *len = -1; *fromlen = -1; return PJ_RETURN_OS_ERROR(reqStatus.Int()); } }