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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors