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/.
9 #include "SocketBase.h"
11 #include "nsThreadUtils.h"
20 UnixSocketRawData::UnixSocketRawData(size_t aSize
)
22 , mCurrentWriteOffset(0)
24 mData
= new uint8_t[mSize
];
27 UnixSocketRawData::UnixSocketRawData(const void* aData
, size_t aSize
)
29 , mCurrentWriteOffset(0)
31 MOZ_ASSERT(aData
|| !mSize
);
33 mData
= new uint8_t[mSize
];
34 memcpy(mData
, aData
, mSize
);
41 SocketConsumerBase::~SocketConsumerBase()
43 MOZ_ASSERT(mConnectionStatus
== SOCKET_DISCONNECTED
);
46 SocketConnectionStatus
47 SocketConsumerBase::GetConnectionStatus() const
49 MOZ_ASSERT(NS_IsMainThread());
51 return mConnectionStatus
;
55 SocketConsumerBase::GetSuggestedConnectDelayMs() const
57 MOZ_ASSERT(NS_IsMainThread());
59 return mConnectDelayMs
;
63 SocketConsumerBase::NotifySuccess()
65 MOZ_ASSERT(NS_IsMainThread());
67 mConnectionStatus
= SOCKET_CONNECTED
;
68 mConnectTimestamp
= PR_IntervalNow();
73 SocketConsumerBase::NotifyError()
75 MOZ_ASSERT(NS_IsMainThread());
77 mConnectionStatus
= SOCKET_DISCONNECTED
;
78 mConnectDelayMs
= CalculateConnectDelayMs();
79 mConnectTimestamp
= 0;
84 SocketConsumerBase::NotifyDisconnect()
86 MOZ_ASSERT(NS_IsMainThread());
88 mConnectionStatus
= SOCKET_DISCONNECTED
;
89 mConnectDelayMs
= CalculateConnectDelayMs();
90 mConnectTimestamp
= 0;
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...
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)
121 SocketConsumerBase::SetConnectionStatus(
122 SocketConnectionStatus aConnectionStatus
)
124 mConnectionStatus
= aConnectionStatus
;
131 SocketIOBase::~SocketIOBase()
135 SocketIOBase::EnqueueData(UnixSocketRawData
* aData
)
138 delete aData
; // delete empty data immediately
141 mOutgoingQ
.AppendElement(aData
);
145 SocketIOBase::HasPendingData() const
147 return !mOutgoingQ
.IsEmpty();
150 SocketIOBase::SocketIOBase(size_t aMaxReadSize
)
151 : mMaxReadSize(aMaxReadSize
)
153 MOZ_ASSERT(mMaxReadSize
);