]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/Connection.cc
Bug 4278: Docs: typo in the refresh_pattern freshness algorithm
[thirdparty/squid.git] / src / comm / Connection.cc
CommitLineData
bbc27441 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
bbc27441
AJ
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
f7f3304a 9#include "squid.h"
a011edee 10#include "CachePeer.h"
cfd66529
AJ
11#include "cbdata.h"
12#include "comm.h"
13#include "comm/Connection.h"
be364179 14#include "fde.h"
e8dca475 15#include "neighbors.h"
c5c06f02 16#include "SquidConfig.h"
be364179 17#include "SquidTime.h"
cfd66529 18
a011edee 19class CachePeer;
97c81191
AJ
20bool
21Comm::IsConnOpen(const Comm::ConnectionPointer &conn)
22{
23 return conn != NULL && conn->isOpen();
24}
25
cfd66529 26Comm::Connection::Connection() :
f53969cc
SM
27 local(),
28 remote(),
29 peerType(HIER_NONE),
30 fd(-1),
31 tos(0),
32 nfmark(0),
33 flags(COMM_NONBLOCKING),
34 peer_(NULL),
35 startTime_(squid_curtime)
73c36fd9
AJ
36{
37 *rfc931 = 0; // quick init the head. the rest does not matter.
38}
cfd66529 39
be364179 40static int64_t lost_conn = 0;
aed188fd 41Comm::Connection::~Connection()
739b352a 42{
be364179 43 if (fd >= 0) {
c460b3d7
AJ
44 debugs(5, 4, "BUG #3329: Orphan Comm::Connection: " << *this);
45 debugs(5, 4, "NOTE: " << ++lost_conn << " Orphans since last started.");
42a61e53 46 close();
be364179
AJ
47 }
48
a3c6762c 49 cbdataReferenceDone(peer_);
739b352a
AJ
50}
51
5ae21d99 52Comm::ConnectionPointer
aed188fd 53Comm::Connection::copyDetails() const
739b352a 54{
aed188fd 55 ConnectionPointer c = new Comm::Connection;
739b352a 56
7fb5be3e 57 c->setAddrs(local, remote);
5229395c 58 c->peerType = peerType;
aed188fd 59 c->tos = tos;
f123f5e9 60 c->nfmark = nfmark;
aed188fd 61 c->flags = flags;
8aec3e1b 62 c->startTime_ = startTime_;
802540f2 63
aed188fd
AJ
64 // ensure FD is not open in the new copy.
65 c->fd = -1;
f9b72e0c 66
a3c6762c
FC
67 // ensure we have a cbdata reference to peer_ not a straight ptr copy.
68 c->peer_ = cbdataReference(getPeer());
cfd66529 69
aed188fd 70 return c;
cfd66529 71}
739b352a 72
55cbb02b
AJ
73void
74Comm::Connection::close()
75{
5229395c 76 if (isOpen()) {
55cbb02b 77 comm_close(fd);
b54a7c5a
CT
78 noteClosure();
79 }
80}
81
82void
83Comm::Connection::noteClosure()
84{
85 if (isOpen()) {
5229395c 86 fd = -1;
a3c6762c 87 if (CachePeer *p=getPeer())
e8dca475 88 peerConnClosed(p);
5229395c 89 }
55cbb02b
AJ
90}
91
a3c6762c 92CachePeer *
418b3087
AJ
93Comm::Connection::getPeer() const
94{
a3c6762c
FC
95 if (cbdataReferenceValid(peer_))
96 return peer_;
418b3087
AJ
97
98 return NULL;
99}
100
739b352a 101void
a3c6762c 102Comm::Connection::setPeer(CachePeer *p)
739b352a
AJ
103{
104 /* set to self. nothing to do. */
418b3087 105 if (getPeer() == p)
739b352a
AJ
106 return;
107
a3c6762c 108 cbdataReferenceDone(peer_);
746beefe 109 if (p) {
a3c6762c 110 peer_ = cbdataReference(p);
746beefe 111 }
739b352a 112}
f53969cc 113
c5c06f02
CT
114time_t
115Comm::Connection::timeLeft(const time_t idleTimeout) const
116{
117 if (!Config.Timeout.pconnLifetime)
118 return idleTimeout;
119
120 const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
121 return min(lifeTimeLeft, idleTimeout);
122}
6a515ac4 123