]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mgr/StoreToCommWriter.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 16 Cache Manager API */
12 #include "base/TextException.h"
13 #include "comm/Connection.h"
14 #include "comm/Write.h"
15 #include "CommCalls.h"
16 #include "ipc/FdNotes.h"
17 #include "mgr/StoreToCommWriter.h"
19 #include "StoreClient.h"
21 CBDATA_NAMESPACED_CLASS_INIT(Mgr
, StoreToCommWriter
);
23 Mgr::StoreToCommWriter::StoreToCommWriter(const Comm::ConnectionPointer
&conn
, StoreEntry
* anEntry
):
24 AsyncJob("Mgr::StoreToCommWriter"),
25 clientConnection(conn
), entry(anEntry
), sc(NULL
), writeOffset(0), closer(NULL
)
27 debugs(16, 6, HERE
<< clientConnection
);
28 closer
= asyncCall(16, 5, "Mgr::StoreToCommWriter::noteCommClosed",
29 CommCbMemFunT
<StoreToCommWriter
, CommCloseCbParams
>(this, &StoreToCommWriter::noteCommClosed
));
30 comm_add_close_handler(clientConnection
->fd
, closer
);
33 Mgr::StoreToCommWriter::~StoreToCommWriter()
41 /// closes our copy of the client HTTP connection socket
43 Mgr::StoreToCommWriter::close()
45 if (Comm::IsConnOpen(clientConnection
)) {
47 comm_remove_close_handler(clientConnection
->fd
, closer
);
50 clientConnection
->close();
55 Mgr::StoreToCommWriter::start()
58 Must(Comm::IsConnOpen(clientConnection
));
60 entry
->registerAbort(&StoreToCommWriter::Abort
, this);
61 sc
= storeClientListAdd(entry
, this);
64 // initiate the receive-from-store, write-to-comm sequence
69 Mgr::StoreToCommWriter::scheduleStoreCopy()
74 StoreIOBuffer
readBuf(sizeof(buffer
), writeOffset
, buffer
);
75 storeClientCopy(sc
, entry
, readBuf
, &NoteStoreCopied
, this);
79 Mgr::StoreToCommWriter::NoteStoreCopied(void* data
, StoreIOBuffer ioBuf
)
82 // make sync Store call async to get async call protections and features
83 StoreToCommWriter
* writer
= static_cast<StoreToCommWriter
*>(data
);
84 typedef UnaryMemFunT
<StoreToCommWriter
, StoreIOBuffer
> MyDialer
;
85 AsyncCall::Pointer call
=
86 asyncCall(16, 5, "Mgr::StoreToCommWriter::noteStoreCopied",
87 MyDialer(writer
, &StoreToCommWriter::noteStoreCopied
, ioBuf
));
88 ScheduleCallHere(call
);
92 Mgr::StoreToCommWriter::noteStoreCopied(StoreIOBuffer ioBuf
)
95 Must(!ioBuf
.flags
.error
);
97 scheduleCommWrite(ioBuf
); // write received action results to client
99 Must(doneAll()); // otherwise, why would Store call us with no data?
103 Mgr::StoreToCommWriter::scheduleCommWrite(const StoreIOBuffer
& ioBuf
)
106 Must(Comm::IsConnOpen(clientConnection
));
107 Must(ioBuf
.data
!= NULL
);
108 // write filled buffer
109 typedef CommCbMemFunT
<StoreToCommWriter
, CommIoCbParams
> MyDialer
;
110 AsyncCall::Pointer writer
=
111 asyncCall(16, 5, "Mgr::StoreToCommWriter::noteCommWrote",
112 MyDialer(this, &StoreToCommWriter::noteCommWrote
));
113 Comm::Write(clientConnection
, ioBuf
.data
, ioBuf
.length
, writer
, NULL
);
117 Mgr::StoreToCommWriter::noteCommWrote(const CommIoCbParams
& params
)
120 Must(params
.flag
== Comm::OK
);
121 Must(clientConnection
!= NULL
&& params
.fd
== clientConnection
->fd
);
122 Must(params
.size
!= 0);
123 writeOffset
+= params
.size
;
125 scheduleStoreCopy(); // retrieve the next data portion
129 Mgr::StoreToCommWriter::noteCommClosed(const CommCloseCbParams
&)
132 Must(!Comm::IsConnOpen(clientConnection
));
133 mustStop("commClosed");
137 Mgr::StoreToCommWriter::swanSong()
142 storeUnregister(sc
, entry
, this);
145 entry
->unregisterAbort();
146 entry
->unlock("Mgr::StoreToCommWriter::swanSong");
153 Mgr::StoreToCommWriter::doneAll() const
156 entry
->store_status
== STORE_OK
&& // the action is over
157 writeOffset
>= entry
->objectLen(); // we wrote all the results
161 Mgr::StoreToCommWriter::Abort(void* param
)
163 StoreToCommWriter
* mgrWriter
= static_cast<StoreToCommWriter
*>(param
);
164 if (Comm::IsConnOpen(mgrWriter
->clientConnection
))
165 mgrWriter
->clientConnection
->close();