| 1 | /* |
| 2 | The MIT License (MIT) |
| 3 | |
| 4 | Copyright (c) 2014 Akshay Jaggi. |
| 5 | Copyright (c) 2014 Haiku, Inc. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | of this software and associated documentation files (the "Software"), to deal |
| 9 | in the Software without restriction, including without limitation the rights |
| 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | copies of the Software, and to permit persons to whom the Software is |
| 12 | furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be included in |
| 15 | all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "SocketStreamHandle.h" |
| 28 | #include "SocketStreamError.h" |
| 29 | |
| 30 | #include "URL.h" |
| 31 | #include "Logging.h" |
| 32 | #include "NotImplemented.h" |
| 33 | #include "SocketStreamHandleClient.h" |
| 34 | |
| 35 | #include <wtf/text/CString.h> |
| 36 | #include <wtf/MainThread.h> |
| 37 | #include <kernel/OS.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | static const int kReadBufferSize = 1024; |
| 42 | |
| 43 | int32 SocketStreamHandle::AsyncHandleRead(int32 length) |
| 44 | { |
| 45 | if(length<0) |
| 46 | { |
| 47 | m_client->didFailSocketStream(this,SocketStreamError(length)); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | m_client->didReceiveSocketStreamData(this,readBuffer,length); |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | int32 SocketStreamHandle::AsyncHandleWrite() |
| 58 | { |
| 59 | sendPendingData(); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void CallOnMainThreadAndWaitRead(SocketStreamHandle* handle, int32 length) |
| 65 | { |
| 66 | if(isMainThread()) |
| 67 | { |
| 68 | handle->AsyncHandleRead(length); |
| 69 | return; |
| 70 | } |
| 71 | sem_id sem; |
| 72 | sem = create_sem(1,"AsyncHandleRead"); |
| 73 | acquire_sem(sem); |
| 74 | callOnMainThread([&]{ |
| 75 | handle->AsyncHandleRead(length); |
| 76 | release_sem(sem); |
| 77 | }); |
| 78 | acquire_sem(sem); |
| 79 | release_sem(sem); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void CallOnMainThreadAndWaitConnect(SocketStreamHandle* handle, int32 error) |
| 84 | { |
| 85 | if(isMainThread()) |
| 86 | { |
| 87 | handle->AsyncHandleConnect(error); |
| 88 | return; |
| 89 | } |
| 90 | sem_id sem; |
| 91 | sem = create_sem(1,"AsyncHandleConnect"); |
| 92 | acquire_sem(sem); |
| 93 | callOnMainThread([&]{ |
| 94 | handle->AsyncHandleConnect(error); |
| 95 | release_sem(sem); |
| 96 | }); |
| 97 | acquire_sem(sem); |
| 98 | release_sem(sem); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void CallOnMainThreadAndWaitWrite(SocketStreamHandle* handle) |
| 103 | { |
| 104 | if(isMainThread()) |
| 105 | { |
| 106 | handle->AsyncHandleWrite(); |
| 107 | return; |
| 108 | } |
| 109 | sem_id sem; |
| 110 | sem = create_sem(1,"AsyncHandleWrite"); |
| 111 | acquire_sem(sem); |
| 112 | callOnMainThread([&]{ |
| 113 | handle->AsyncHandleWrite(); |
| 114 | release_sem(sem); |
| 115 | }); |
| 116 | acquire_sem(sem); |
| 117 | release_sem(sem); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | int32 AsyncReadThread(void* data) |
| 122 | { |
| 123 | thread_id sender; |
| 124 | int32 code; |
| 125 | SocketStreamHandle* handle = (SocketStreamHandle*)data; |
| 126 | while(true) |
| 127 | { |
| 128 | if(has_data(find_thread(NULL))) |
| 129 | { |
| 130 | code = receive_data(&sender,NULL,0); |
| 131 | if(code==1) |
| 132 | exit_thread(0); |
| 133 | } |
| 134 | int32 numberReadBytes = (handle->socket)->Read(handle->readBuffer,kReadBufferSize); |
| 135 | CallOnMainThreadAndWaitRead(handle,numberReadBytes); |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | int32 SocketStreamHandle::AsyncHandleConnect(int32 error) |
| 142 | { |
| 143 | if(error != B_OK) |
| 144 | { |
| 145 | m_client->didFailSocketStream(this,SocketStreamError(error)); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | fReadThreadId = spawn_thread(AsyncReadThread,"AsyncReadThread",63,(void*)this); |
| 150 | resume_thread(fReadThreadId); |
| 151 | m_state = Open; |
| 152 | m_client->didOpenSocketStream(this); |
| 153 | } |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | int32 AsyncWriteThread(void* data) |
| 159 | { |
| 160 | SocketStreamHandle* handle = (SocketStreamHandle*)data; |
| 161 | status_t response=(handle->socket)->WaitForWritable(B_INFINITE_TIMEOUT); |
| 162 | CallOnMainThreadAndWaitWrite(handle); |
| 163 | return response; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | int32 AsyncConnectThread(void* data) |
| 168 | { |
| 169 | SocketStreamHandle* handle = (SocketStreamHandle*)data; |
| 170 | status_t error=(handle->socket)->Connect(*(handle->peer)); |
| 171 | CallOnMainThreadAndWaitConnect(handle, error); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | SocketStreamHandle::SocketStreamHandle(const URL& url, SocketStreamHandleClient* client) |
| 176 | : |
| 177 | SocketStreamHandleBase(url, client) |
| 178 | { |
| 179 | LOG(Network, "SocketStreamHandle %p new client %p", this, m_client); |
| 180 | fReadThreadId = 0; |
| 181 | fWriteThreadId = 0; |
| 182 | fConnectThreadId=0; |
| 183 | readBuffer = new char[kReadBufferSize]; |
| 184 | unsigned int port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443 : 80); |
| 185 | peer = new BNetworkAddress(url.host().utf8().data(),port); |
| 186 | socket = new BSocket(); |
| 187 | fConnectThreadId = spawn_thread(AsyncConnectThread,"AsyncConnectThread",63,(void*)this); |
| 188 | resume_thread(fConnectThreadId); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | SocketStreamHandle::~SocketStreamHandle() |
| 193 | { |
| 194 | LOG(Network, "SocketStreamHandle %p delete", this); |
| 195 | delete readBuffer; |
| 196 | delete peer; |
| 197 | delete socket; |
| 198 | setClient(0); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int SocketStreamHandle::platformSend(const char* buffer, int length) |
| 203 | { |
| 204 | int32 writtenLength = 0; |
| 205 | bool flagForPending = false; |
| 206 | LOG(Network, "SocketStreamHandle %p platformSend", this); |
| 207 | status_t response=socket->WaitForWritable(0); |
| 208 | if(response == B_OK) |
| 209 | { |
| 210 | writtenLength=socket->Write(buffer,length); |
| 211 | if(writtenLength < length) |
| 212 | flagForPending = true; |
| 213 | } |
| 214 | else if(response == B_TIMED_OUT |
| 215 | || response == B_WOULD_BLOCK) |
| 216 | { |
| 217 | flagForPending = true; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | m_client->didFailSocketStream(this,SocketStreamError(response)); |
| 222 | } |
| 223 | if(flagForPending) |
| 224 | { |
| 225 | fWriteThreadId = spawn_thread(AsyncWriteThread,"AsyncWriteThread",63,(void*)this); |
| 226 | resume_thread(fWriteThreadId); |
| 227 | } |
| 228 | return writtenLength; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void SocketStreamHandle::platformClose() |
| 233 | { |
| 234 | LOG(Network, "SocketStreamHandle %p platformClose", this); |
| 235 | send_data(fReadThreadId,1,NULL,0); |
| 236 | socket->Disconnect(); |
| 237 | m_client->didCloseSocketStream(this); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void SocketStreamHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge&) |
| 242 | { |
| 243 | notImplemented(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void SocketStreamHandle::receivedCredential(const AuthenticationChallenge&, const Credential&) |
| 248 | { |
| 249 | notImplemented(); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void SocketStreamHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) |
| 254 | { |
| 255 | notImplemented(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void SocketStreamHandle::receivedCancellation(const AuthenticationChallenge&) |
| 260 | { |
| 261 | notImplemented(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | } // namespace WebCore |