StoreToCommWriter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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/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"
18 #include "Store.h"
19 #include "StoreClient.h"
20 
21 CBDATA_NAMESPACED_CLASS_INIT(Mgr, StoreToCommWriter);
22 
24  AsyncJob("Mgr::StoreToCommWriter"),
25  clientConnection(conn), entry(anEntry), sc(NULL), writeOffset(0), closer(NULL)
26 {
27  debugs(16, 6, HERE << clientConnection);
28  closer = asyncCall(16, 5, "Mgr::StoreToCommWriter::noteCommClosed",
31 }
32 
34 {
35  debugs(16, 6, HERE);
36  assert(!entry);
37  assert(!sc);
38  close();
39 }
40 
42 void
44 {
45  if (Comm::IsConnOpen(clientConnection)) {
46  if (closer != NULL) {
47  comm_remove_close_handler(clientConnection->fd, closer);
48  closer = NULL;
49  }
50  clientConnection->close();
51  }
52 }
53 
54 void
56 {
57  debugs(16, 6, HERE);
58  Must(Comm::IsConnOpen(clientConnection));
59  Must(entry != NULL);
60  entry->registerAbort(&StoreToCommWriter::Abort, this);
61  sc = storeClientListAdd(entry, this);
62  Must(sc != NULL);
63 
64  // initiate the receive-from-store, write-to-comm sequence
65  scheduleStoreCopy();
66 }
67 
68 void
70 {
71  debugs(16, 6, HERE);
72  Must(entry != NULL);
73  Must(sc != NULL);
74  StoreIOBuffer readBuf(sizeof(buffer), writeOffset, buffer);
75  storeClientCopy(sc, entry, readBuf, &NoteStoreCopied, this);
76 }
77 
78 void
80 {
81  Must(data != NULL);
82  // make sync Store call async to get async call protections and features
83  StoreToCommWriter* writer = static_cast<StoreToCommWriter*>(data);
85  AsyncCall::Pointer call =
86  asyncCall(16, 5, "Mgr::StoreToCommWriter::noteStoreCopied",
87  MyDialer(writer, &StoreToCommWriter::noteStoreCopied, ioBuf));
88  ScheduleCallHere(call);
89 }
90 
91 void
93 {
94  debugs(16, 6, HERE);
95  Must(!ioBuf.flags.error);
96  if (ioBuf.length > 0)
97  scheduleCommWrite(ioBuf); // write received action results to client
98  else
99  Must(doneAll()); // otherwise, why would Store call us with no data?
100 }
101 
102 void
104 {
105  debugs(16, 6, HERE);
106  Must(Comm::IsConnOpen(clientConnection));
107  Must(ioBuf.data != NULL);
108  // write filled buffer
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);
114 }
115 
116 void
118 {
119  debugs(16, 6, HERE);
120  Must(params.flag == Comm::OK);
121  Must(clientConnection != NULL && params.fd == clientConnection->fd);
122  Must(params.size != 0);
123  writeOffset += params.size;
124  if (!doneAll())
125  scheduleStoreCopy(); // retrieve the next data portion
126 }
127 
128 void
130 {
131  debugs(16, 6, HERE);
132  Must(!Comm::IsConnOpen(clientConnection));
133  mustStop("commClosed");
134 }
135 
136 void
138 {
139  debugs(16, 6, HERE);
140  if (entry != NULL) {
141  if (sc != NULL) {
142  storeUnregister(sc, entry, this);
143  sc = NULL;
144  }
145  entry->unregisterAbort();
146  entry->unlock("Mgr::StoreToCommWriter::swanSong");
147  entry = NULL;
148  }
149  close();
150 }
151 
152 bool
154 {
155  return entry &&
156  entry->store_status == STORE_OK && // the action is over
157  writeOffset >= entry->objectLen(); // we wrote all the results
158 }
159 
160 void
162 {
163  StoreToCommWriter* mgrWriter = static_cast<StoreToCommWriter*>(param);
164  if (Comm::IsConnOpen(mgrWriter->clientConnection))
165  mgrWriter->clientConnection->close();
166 }
167 
#define assert(EX)
Definition: assert.h:17
void storeClientCopy(store_client *sc, StoreEntry *e, StoreIOBuffer copyInto, STCB *callback, void *data)
static int sc[16]
Definition: smbdes.c:121
CBDATA_NAMESPACED_CLASS_INIT(Mgr, StoreToCommWriter)
Comm::ConnectionPointer clientConnection
HTTP client descriptor.
static void NoteStoreCopied(void *data, StoreIOBuffer ioBuf)
Definition: Flag.h:16
store_client * storeClientListAdd(StoreEntry *e, void *data)
Definition: store_client.cc:87
int conn
the current server connection FD
Definition: Transport.cc:26
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void noteStoreCopied(StoreIOBuffer ioBuf)
receive some action results from the store
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
void const char HLPCB void * data
Definition: stub_helper.cc:16
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void noteCommClosed(const CommCloseCbParams &params)
called by Comm if the client socket got closed
virtual bool doneAll() const
whether positive goal has been reached
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
StoreToCommWriter(const Comm::ConnectionPointer &conn, StoreEntry *anEntry)
AsyncCall::Pointer closer
comm_close handler
struct StoreIOBuffer::@146 flags
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
#define Must(cond)
Definition: TextException.h:89
unsigned error
Definition: StoreIOBuffer.h:52
int storeUnregister(store_client *sc, StoreEntry *e, void *data)
Definition: enums.h:46
void close()
closes the local connection to the HTTP client, if any
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
static void Abort(void *param)
called by Store if the entry is no longer usable
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:988
#define NULL
Definition: types.h:166
virtual void start()
called by AsyncStart; do not call directly
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
void scheduleStoreCopy()
request more action results from the store

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors