void UnixSocketConsumerIO::OnAccepted(int aFd, const sockaddr_any* aAddr, socklen_t aAddrLen) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(aAddr); MOZ_ASSERT(aAddrLen <= sizeof(mAddr)); memcpy (&mAddr, aAddr, aAddrLen); mAddrSize = aAddrLen; if (!mConnector->SetUp(aFd)) { NS_WARNING("Could not set up socket!"); return; } RemoveWatchers(READ_WATCHER|WRITE_WATCHER); Close(); if (!SetSocketFlags(aFd)) { return; } SetSocket(aFd, SOCKET_IS_CONNECTED); nsRefPtr<nsRunnable> r = new SocketIOEventRunnable<UnixSocketConsumerIO>( this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS); NS_DispatchToMainThread(r); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(mConnector); MOZ_ASSERT(aCOSocketIO); struct sockaddr* address = reinterpret_cast<struct sockaddr*>(&mAddress); mAddressLength = sizeof(mAddress); if (!IsOpen()) { int fd; nsresult rv = mConnector->CreateListenSocket(address, &mAddressLength, fd); if (NS_FAILED(rv)) { FireSocketError(); return; } SetFd(fd); } mCOSocketIO = aCOSocketIO; // calls OnListening on success, or OnError otherwise nsresult rv = UnixSocketWatcher::Listen(address, mAddressLength); NS_WARN_IF(NS_FAILED(rv)); }
void ListenSocketIO::OnSocketCanAcceptWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(mCOSocketIO); RemoveWatchers(READ_WATCHER|WRITE_WATCHER); struct sockaddr_storage storage; socklen_t addressLength = sizeof(storage); int fd; nsresult rv = mConnector->AcceptStreamSocket( GetFd(), reinterpret_cast<struct sockaddr*>(&storage), &addressLength, fd); if (NS_FAILED(rv)) { FireSocketError(); return; } mCOSocketIO->Accept(fd, reinterpret_cast<struct sockaddr*>(&storage), addressLength); }
void UnixSocketWatcher::Close() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); mConnectionStatus = SOCKET_IS_DISCONNECTED; UnixFdWatcher::Close(); }
void UnixSocketConsumerIO::Listen() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(mConnector); // This will set things we don't particularly care about, but it will hand // back the correct structure size which is what we do care about. if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) { NS_WARNING("Cannot create socket address!"); FireSocketError(); return; } if (!IsOpen()) { int fd = mConnector->Create(); if (fd < 0) { NS_WARNING("Cannot create socket fd!"); FireSocketError(); return; } if (!SetSocketFlags(fd)) { NS_WARNING("Cannot set socket flags!"); FireSocketError(); return; } SetFd(fd); // calls OnListening on success, or OnError otherwise nsresult rv = UnixSocketWatcher::Listen( reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize); NS_WARN_IF(NS_FAILED(rv)); } }
void UnixSocketConsumerIO::Connect() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(mConnector); if (!IsOpen()) { int fd = mConnector->Create(); if (fd < 0) { NS_WARNING("Cannot create socket fd!"); FireSocketError(); return; } if (!SetSocketFlags(fd)) { NS_WARNING("Cannot set socket flags!"); FireSocketError(); return; } SetFd(fd); } if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) { NS_WARNING("Cannot create socket address!"); FireSocketError(); return; } // calls OnConnected() on success, or OnError() otherwise nsresult rv = UnixSocketWatcher::Connect( reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize); NS_WARN_IF(NS_FAILED(rv)); }
void ListenSocketIO::OnAccepted(int aFd, const sockaddr_any* aAddr, socklen_t aAddrLen) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(mCOSocketIO); RemoveWatchers(READ_WATCHER|WRITE_WATCHER); nsRefPtr<nsRunnable> runnable; if (NS_SUCCEEDED(mCOSocketIO->Accept(aFd, aAddr, aAddrLen))) { runnable = new SocketIOEventRunnable<ListenSocketIO>( this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_SUCCESS); return; } else { runnable = new SocketIOEventRunnable<ListenSocketIO>( this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_ERROR); } NS_DispatchToMainThread(runnable); }
void ListenSocketIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); NS_NOTREACHED("Invalid call to |ListenSocketIO::OnConnected|"); }
nsresult ConnectionOrientedSocketIO::Connect() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(!IsOpen()); struct sockaddr* peerAddress = reinterpret_cast<struct sockaddr*>(&mPeerAddress); mPeerAddressLength = sizeof(mPeerAddress); int fd; nsresult rv = mConnector->CreateStreamSocket(peerAddress, &mPeerAddressLength, fd); if (NS_FAILED(rv)) { // Tell the consumer thread we've errored GetConsumerThread()->PostTask( MakeAndAddRef<SocketEventTask>(this, SocketEventTask::CONNECT_ERROR)); return NS_ERROR_FAILURE; } SetFd(fd); // calls OnConnected() on success, or OnError() otherwise rv = UnixSocketWatcher::Connect(peerAddress, mPeerAddressLength); if (NS_FAILED(rv)) { return rv; } return NS_OK; }
void ConnectionOrientedSocketIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); NS_NOTREACHED("Invalid call to |ConnectionOrientedSocketIO::OnListening|"); }
void UnixSocketConsumerIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); if (!SetSocketFlags(GetFd())) { NS_WARNING("Cannot set socket flags!"); FireSocketError(); return; } if (!mConnector->SetUp(GetFd())) { NS_WARNING("Could not set up socket!"); FireSocketError(); return; } nsRefPtr<nsRunnable> r = new SocketIOEventRunnable<UnixSocketConsumerIO>( this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS); NS_DispatchToMainThread(r); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ListenSocketIO::OnError(const char* aFunction, int aErrno) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); UnixFdWatcher::OnError(aFunction, aErrno); FireSocketError(); }
void ConnectionOrientedSocketIO::Send(UnixSocketIOBuffer* aBuffer) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); EnqueueData(aBuffer); AddWatchers(WRITE_WATCHER, false); }
void UnixSocketWatcher::SetSocket(int aFd, ConnectionStatus aConnectionStatus) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); SetFd(aFd); mConnectionStatus = aConnectionStatus; }
void BluetoothDaemonConnectionIO::OnSocketCanSendWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); MOZ_ASSERT(!IsShutdownOnIOThread()); if (NS_WARN_IF(NS_FAILED(SendPendingData(GetFd())))) { RemoveWatchers(WRITE_WATCHER); } }
void ListenSocketIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); AddWatchers(READ_WATCHER, true); /* We signal a successful 'connection' to a local address for listening. */ GetConsumerThread()->PostTask( FROM_HERE, new SocketEventTask(this, SocketEventTask::CONNECT_SUCCESS)); }
void ListenSocketIO::FireSocketError() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); // Clean up watchers, statuses, fds Close(); // Tell the main thread we've errored NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); }
void ListenSocketIO::FireSocketError() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); // Clean up watchers, statuses, fds Close(); // Tell the consumer thread we've errored GetConsumerThread()->PostTask( FROM_HERE, new SocketEventTask(this, SocketEventTask::CONNECT_ERROR)); }
void UnixSocketConsumerIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 nsresult rv = ReceiveData(GetFd(), this); if (NS_FAILED(rv)) { RemoveWatchers(READ_WATCHER|WRITE_WATCHER); return; } }
void ListenSocketIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); AddWatchers(READ_WATCHER, true); /* We signal a successful 'connection' to a local address for listening. */ NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); }
void BluetoothDaemonConnectionIO::OnError(const char* aFunction, int aErrno) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); UnixFdWatcher::OnError(aFunction, aErrno); // Clean up watchers, status, fd Close(); // Tell the main thread we've errored NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); }
void BluetoothDaemonConnectionIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ConnectionOrientedSocketIO::OnError(const char* aFunction, int aErrno) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); UnixFdWatcher::OnError(aFunction, aErrno); // Clean up watchers, status, fd Close(); // Tell the consumer thread we've errored GetConsumerThread()->PostTask( MakeAndAddRef<SocketEventTask>(this, SocketEventTask::CONNECT_ERROR)); }
void UnixSocketConsumerIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); if (!mConnector->SetUpListenSocket(GetFd())) { NS_WARNING("Could not set up listen socket!"); FireSocketError(); return; } AddWatchers(READ_WATCHER, true); }
void ConnectionOrientedSocketIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); GetConsumerThread()->PostTask( MakeAndAddRef<SocketEventTask>(this, SocketEventTask::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ConnectionOrientedSocketIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 ssize_t res = ReceiveData(GetFd()); if (res < 0) { /* I/O error */ RemoveWatchers(READ_WATCHER|WRITE_WATCHER); } else if (!res) { /* EOF or peer shutdown */ RemoveWatchers(READ_WATCHER); } }
void UnixSocketConsumerIO::OnSocketCanSendWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 nsresult rv = SendPendingData(GetFd(), this); if (NS_FAILED(rv)) { return; } if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void UnixSocketConsumerIO::FireSocketError() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); // Clean up watchers, statuses, fds Close(); // Tell the main thread we've errored nsRefPtr<nsRunnable> r = new SocketIOEventRunnable<UnixSocketConsumerIO>( this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_ERROR); NS_DispatchToMainThread(r); }
void BluetoothDaemonConnectionIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); MOZ_ASSERT(!IsShutdownOnIOThread()); ssize_t res = ReceiveData(GetFd()); if (res < 0) { /* I/O error */ RemoveWatchers(READ_WATCHER|WRITE_WATCHER); } else if (!res) { /* EOF or peer shutdown */ RemoveWatchers(READ_WATCHER); } }
void UnixSocketWatcher::OnFileCanReadWithoutBlocking(int aFd) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(aFd == GetFd()); if (mConnectionStatus == SOCKET_IS_CONNECTED) { OnSocketCanReceiveWithoutBlocking(); } else if (mConnectionStatus == SOCKET_IS_LISTENING) { int fd = TEMP_FAILURE_RETRY(accept(GetFd(), NULL, NULL)); if (fd < 0) { OnError("accept", errno); } else { OnAccepted(fd); } } else { NS_NOTREACHED("invalid connection state for reading"); } }