StoreToCommWriter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 16 Cache Manager API */
10 
11 #include "squid.h"
12 #include "base/AsyncCbdataCalls.h"
13 #include "base/TextException.h"
14 #include "comm/Connection.h"
15 #include "comm/Write.h"
16 #include "CommCalls.h"
17 #include "ipc/FdNotes.h"
18 #include "mgr/StoreToCommWriter.h"
19 #include "Store.h"
20 #include "StoreClient.h"
21 
22 CBDATA_NAMESPACED_CLASS_INIT(Mgr, StoreToCommWriter);
23 
25  AsyncJob("Mgr::StoreToCommWriter"),
26  clientConnection(conn), entry(anEntry), sc(NULL), writeOffset(0), closer(NULL)
27 {
28  debugs(16, 6, HERE << clientConnection);
29  closer = asyncCall(16, 5, "Mgr::StoreToCommWriter::noteCommClosed",
32 }
33 
35 {
36  debugs(16, 6, HERE);
37  assert(!entry);
38  assert(!sc);
39  close();
40 }
41 
43 void
45 {
46  if (Comm::IsConnOpen(clientConnection)) {
47  if (closer != NULL) {
48  comm_remove_close_handler(clientConnection->fd, closer);
49  closer = NULL;
50  }
51  clientConnection->close();
52  }
53 }
54 
55 void
57 {
58  debugs(16, 6, HERE);
59  Must(Comm::IsConnOpen(clientConnection));
60  Must(entry != NULL);
61  AsyncCall::Pointer call = asyncCall(16, 4, "StoreToCommWriter::Abort", cbdataDialer(&StoreToCommWriter::HandleStoreAbort, this));
62  entry->registerAbortCallback(call);
63  sc = storeClientListAdd(entry, this);
64  Must(sc != NULL);
65 
66  // initiate the receive-from-store, write-to-comm sequence
67  scheduleStoreCopy();
68 }
69 
70 void
72 {
73  debugs(16, 6, HERE);
74  Must(entry != NULL);
75  Must(sc != NULL);
76  StoreIOBuffer readBuf(sizeof(buffer), writeOffset, buffer);
77  storeClientCopy(sc, entry, readBuf, &NoteStoreCopied, this);
78 }
79 
80 void
82 {
83  Must(data != NULL);
84  // make sync Store call async to get async call protections and features
85  StoreToCommWriter* writer = static_cast<StoreToCommWriter*>(data);
87  AsyncCall::Pointer call =
88  asyncCall(16, 5, "Mgr::StoreToCommWriter::noteStoreCopied",
89  MyDialer(writer, &StoreToCommWriter::noteStoreCopied, ioBuf));
90  ScheduleCallHere(call);
91 }
92 
93 void
95 {
96  debugs(16, 6, HERE);
97  Must(!ioBuf.flags.error);
98  if (ioBuf.length > 0)
99  scheduleCommWrite(ioBuf); // write received action results to client
100  else
101  Must(doneAll()); // otherwise, why would Store call us with no data?
102 }
103 
104 void
106 {
107  debugs(16, 6, HERE);
108  Must(Comm::IsConnOpen(clientConnection));
109  Must(ioBuf.data != NULL);
110  // write filled buffer
112  AsyncCall::Pointer writer =
113  asyncCall(16, 5, "Mgr::StoreToCommWriter::noteCommWrote",
114  MyDialer(this, &StoreToCommWriter::noteCommWrote));
115  Comm::Write(clientConnection, ioBuf.data, ioBuf.length, writer, NULL);
116 }
117 
118 void
120 {
121  debugs(16, 6, HERE);
122  Must(params.flag == Comm::OK);
123  Must(clientConnection != NULL && params.fd == clientConnection->fd);
124  Must(params.size != 0);
125  writeOffset += params.size;
126  if (!doneAll())
127  scheduleStoreCopy(); // retrieve the next data portion
128 }
129 
130 void
132 {
133  debugs(16, 6, HERE);
134  Must(!Comm::IsConnOpen(clientConnection));
135  mustStop("commClosed");
136 }
137 
138 void
140 {
141  debugs(16, 6, HERE);
142  if (entry != NULL) {
143  if (sc != NULL) {
144  storeUnregister(sc, entry, this);
145  sc = NULL;
146  }
147  entry->unregisterAbortCallback("StoreToCommWriter done");
148  entry->unlock("Mgr::StoreToCommWriter::swanSong");
149  entry = NULL;
150  }
151  close();
152 }
153 
154 bool
156 {
157  return entry &&
158  entry->store_status == STORE_OK && // the action is over
159  writeOffset >= entry->objectLen(); // we wrote all the results
160 }
161 
162 void
164 {
165  if (Comm::IsConnOpen(mgrWriter->clientConnection))
166  mgrWriter->clientConnection->close();
167 }
168 
virtual bool doneAll() const
whether positive goal has been reached
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:931
Comm::ConnectionPointer clientConnection
HTTP client descriptor.
static void NoteStoreCopied(void *data, StoreIOBuffer ioBuf)
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
StoreToCommWriter(const Comm::ConnectionPointer &conn, StoreEntry *anEntry)
AsyncCall::Pointer closer
comm_close handler
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
void noteCommWrote(const CommIoCbParams &params)
called by Comm after the action results are written
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:26
@ OK
Definition: Flag.h:16
void noteStoreCopied(StoreIOBuffer ioBuf)
receive some action results from the store
UnaryCbdataDialer< Argument1 > cbdataDialer(typename UnaryCbdataDialer< Argument1 >::Handler *handler, Argument1 *arg1)
virtual void start()
called by AsyncStart; do not call directly
void close()
closes the local connection to the HTTP client, if any
#define NULL
Definition: types.h:166
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
void scheduleCommWrite(const StoreIOBuffer &ioBuf)
tell Comm to write action results
unsigned error
Definition: StoreIOBuffer.h:52
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:157
void const char HLPCB void * data
Definition: stub_helper.cc:16
CBDATA_NAMESPACED_CLASS_INIT(Mgr, StoreToCommWriter)
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
static int sc[16]
Definition: smbdes.c:121
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
@ STORE_OK
Definition: enums.h:50
struct StoreIOBuffer::@149 flags
void noteCommClosed(const CommCloseCbParams &params)
called by Comm if the client socket got closed
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:73
static void HandleStoreAbort(StoreToCommWriter *param)
called by Store if the entry is no longer usable
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void scheduleStoreCopy()
request more action results from the store
int storeUnregister(store_client *sc, StoreEntry *e, void *data)
void storeClientCopy(store_client *sc, StoreEntry *e, StoreIOBuffer copyInto, STCB *callback, void *data)
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:958
store_client * storeClientListAdd(StoreEntry *e, void *data)
Cache Manager API.
Definition: Action.h:20

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors