Bumping manifests a=b2g-bump
[gecko.git] / ipc / unixsocket / SocketBase.cpp
blob9f8bd48d09d22b3f73bc3888531c927a92a3c961
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
9 #include "SocketBase.h"
10 #include <string.h>
11 #include "nsThreadUtils.h"
13 namespace mozilla {
14 namespace ipc {
17 // UnixSocketRawData
20 UnixSocketRawData::UnixSocketRawData(size_t aSize)
21 : mSize(aSize)
22 , mCurrentWriteOffset(0)
24 mData = new uint8_t[mSize];
27 UnixSocketRawData::UnixSocketRawData(const void* aData, size_t aSize)
28 : mSize(aSize)
29 , mCurrentWriteOffset(0)
31 MOZ_ASSERT(aData || !mSize);
33 mData = new uint8_t[mSize];
34 memcpy(mData, aData, mSize);
38 // SocketConsumerBase
41 SocketConsumerBase::~SocketConsumerBase()
43 MOZ_ASSERT(mConnectionStatus == SOCKET_DISCONNECTED);
46 SocketConnectionStatus
47 SocketConsumerBase::GetConnectionStatus() const
49 MOZ_ASSERT(NS_IsMainThread());
51 return mConnectionStatus;
54 int
55 SocketConsumerBase::GetSuggestedConnectDelayMs() const
57 MOZ_ASSERT(NS_IsMainThread());
59 return mConnectDelayMs;
62 void
63 SocketConsumerBase::NotifySuccess()
65 MOZ_ASSERT(NS_IsMainThread());
67 mConnectionStatus = SOCKET_CONNECTED;
68 mConnectTimestamp = PR_IntervalNow();
69 OnConnectSuccess();
72 void
73 SocketConsumerBase::NotifyError()
75 MOZ_ASSERT(NS_IsMainThread());
77 mConnectionStatus = SOCKET_DISCONNECTED;
78 mConnectDelayMs = CalculateConnectDelayMs();
79 mConnectTimestamp = 0;
80 OnConnectError();
83 void
84 SocketConsumerBase::NotifyDisconnect()
86 MOZ_ASSERT(NS_IsMainThread());
88 mConnectionStatus = SOCKET_DISCONNECTED;
89 mConnectDelayMs = CalculateConnectDelayMs();
90 mConnectTimestamp = 0;
91 OnDisconnect();
94 uint32_t
95 SocketConsumerBase::CalculateConnectDelayMs() const
97 MOZ_ASSERT(NS_IsMainThread());
99 uint32_t connectDelayMs = mConnectDelayMs;
101 if (mConnectTimestamp && (PR_IntervalNow()-mConnectTimestamp) > connectDelayMs) {
102 // reset delay if connection has been opened for a while, or...
103 connectDelayMs = 0;
104 } else if (!connectDelayMs) {
105 // ...start with a delay of ~1 sec, or...
106 connectDelayMs = 1<<10;
107 } else if (connectDelayMs < (1<<16)) {
108 // ...otherwise increase delay by a factor of 2
109 connectDelayMs <<= 1;
111 return connectDelayMs;
114 SocketConsumerBase::SocketConsumerBase()
115 : mConnectionStatus(SOCKET_DISCONNECTED)
116 , mConnectTimestamp(0)
117 , mConnectDelayMs(0)
120 void
121 SocketConsumerBase::SetConnectionStatus(
122 SocketConnectionStatus aConnectionStatus)
124 mConnectionStatus = aConnectionStatus;
128 // SocketIOBase
131 SocketIOBase::~SocketIOBase()
134 void
135 SocketIOBase::EnqueueData(UnixSocketRawData* aData)
137 if (!aData->mSize) {
138 delete aData; // delete empty data immediately
139 return;
141 mOutgoingQ.AppendElement(aData);
144 bool
145 SocketIOBase::HasPendingData() const
147 return !mOutgoingQ.IsEmpty();
150 SocketIOBase::SocketIOBase(size_t aMaxReadSize)
151 : mMaxReadSize(aMaxReadSize)
153 MOZ_ASSERT(mMaxReadSize);