| 1 | /* |
| 2 | * Copyright (C) 2009 Brent Fulgham. All rights reserved. |
| 3 | * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "config.h" |
| 33 | #include "SocketStreamHandle.h" |
| 34 | #include "SocketStreamError.h" |
| 35 | |
| 36 | #include "URL.h" |
| 37 | #include "Logging.h" |
| 38 | #include "NotImplemented.h" |
| 39 | #include "SocketStreamHandleClient.h" |
| 40 | |
| 41 | #include <wtf/text/CString.h> |
| 42 | #include <kernel/OS.h> |
| 43 | #include<iostream> |
| 44 | |
| 45 | namespace WebCore { |
| 46 | |
| 47 | int SocketStreamHandle::async_read_push(const char * data,int length) |
| 48 | { |
| 49 | RefPtr<SocketStreamHandle> protect(this); |
| 50 | (m_client)->didReceiveSocketStreamData(this,data,length); |
| 51 | std::cout<<"\nDATA PUSHED"<<data<<std::endl; |
| 52 | return 0; |
| 53 | } |
| 54 | long int async_read(void * ptrr) |
| 55 | { |
| 56 | SocketStreamHandle * strm=(SocketStreamHandle *)ptrr; |
| 57 | char *array=new char[5025]; |
| 58 | while(1) |
| 59 | { |
| 60 | std::cout<<"Entering-Loop with sock:"<<strm->sock<<std::endl; |
| 61 | int rd_bytes=(strm->sock)->Read(array,5024); |
| 62 | array[rd_bytes]='\0'; |
| 63 | std::cout<<"Bytes:"<<rd_bytes<<"Read:"<<array<<std::endl; |
| 64 | strm->async_read_push(array,rd_bytes); |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int SocketStreamHandle::async_sendPendingData() |
| 70 | { |
| 71 | sendPendingData(); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | long int async_write(void * ptrr) |
| 76 | { |
| 77 | SocketStreamHandle * strm=(SocketStreamHandle *)ptrr; |
| 78 | status_t resp=(strm->sock)->WaitForWritable(B_INFINITE_TIMEOUT); |
| 79 | std::cout<<resp<<" "<<B_OK<<" "<<B_WOULD_BLOCK<<" "<<B_TIMED_OUT<<std::endl;; |
| 80 | if(resp==B_OK) |
| 81 | strm->async_sendPendingData(); |
| 82 | else |
| 83 | return resp; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | SocketStreamHandle::SocketStreamHandle(const URL& url, SocketStreamHandleClient* client) |
| 88 | : SocketStreamHandleBase(url, client) |
| 89 | { |
| 90 | LOG(Network, "SocketStreamHandle %p new client %p", this, m_client); |
| 91 | rd_thread=wr_thread=0; |
| 92 | unsigned int port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443:80); |
| 93 | |
| 94 | peer = new BNetworkAddress(url.host().utf8().data(),port); |
| 95 | sock= new BSocket((*peer)); |
| 96 | if(sock->IsConnected()==false) |
| 97 | { |
| 98 | status_t err=sock->Connect((*peer)); |
| 99 | if(err!=B_OK) |
| 100 | { |
| 101 | (m_client)->didFailSocketStream(this,SocketStreamError(err)); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | rd_thread=spawn_thread(async_read,"async_read",63,(void*)this); |
| 106 | resume_thread(rd_thread); |
| 107 | std::cout<<"New request:"<<url.host().utf8().data()<<":"<<port<<std::endl<<"Sock:"<<sock<<"Read-thread-id:"<<rd_thread<<std::endl; |
| 108 | m_state=Open; |
| 109 | m_client->didOpenSocketStream(this); |
| 110 | |
| 111 | //char *array=new char[1025]; |
| 112 | //int rd_bytes=sock->Read(array,1024); |
| 113 | //m_client->didReceiveSocketStreamData(this,array,rd_bytes); |
| 114 | } |
| 115 | |
| 116 | SocketStreamHandle::~SocketStreamHandle() |
| 117 | { |
| 118 | LOG(Network, "SocketStreamHandle %p delete", this); |
| 119 | delete peer; |
| 120 | delete sock; |
| 121 | setClient(0); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | int SocketStreamHandle::platformSend(const char* buffer, int length) |
| 127 | { |
| 128 | int written=0,flag=0; |
| 129 | std::cout<<"To-Write:"<<buffer<<std::endl; |
| 130 | LOG(Network, "SocketStreamHandle %p platformSend", this); |
| 131 | if(sock->WaitForWritable(0)==B_OK) |
| 132 | { |
| 133 | written=sock->Write(buffer,length); |
| 134 | if(written<length) |
| 135 | flag=1; |
| 136 | std::cout<<"WRITTEN:"<<written<<std::endl; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | flag=1; |
| 141 | } |
| 142 | if(flag==1) |
| 143 | { |
| 144 | std::cout<<"VOILA"<<std::endl; |
| 145 | wr_thread=spawn_thread(async_write,"async_write",63,(void*)this); |
| 146 | resume_thread(wr_thread); |
| 147 | } |
| 148 | return written; |
| 149 | } |
| 150 | |
| 151 | void SocketStreamHandle::platformClose() |
| 152 | { |
| 153 | LOG(Network, "SocketStreamHandle %p platformClose", this); |
| 154 | kill_thread(rd_thread); |
| 155 | if(wr_thread!=0) |
| 156 | kill_thread(wr_thread); |
| 157 | sock->Disconnect(); |
| 158 | m_client->didCloseSocketStream(this); |
| 159 | } |
| 160 | |
| 161 | void SocketStreamHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge&) |
| 162 | { |
| 163 | notImplemented(); |
| 164 | } |
| 165 | |
| 166 | void SocketStreamHandle::receivedCredential(const AuthenticationChallenge&, const Credential&) |
| 167 | { |
| 168 | notImplemented(); |
| 169 | } |
| 170 | |
| 171 | void SocketStreamHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) |
| 172 | { |
| 173 | notImplemented(); |
| 174 | } |
| 175 | |
| 176 | void SocketStreamHandle::receivedCancellation(const AuthenticationChallenge&) |
| 177 | { |
| 178 | notImplemented(); |
| 179 | } |
| 180 | |
| 181 | } // namespace WebCore |