xref: /trafficserver/iocore/net/P_UDPIOEvent.h (revision 7651e269)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 // ugly -- just encapsulate the I/O result so that it can be passed
26 // back to the caller via continuation handler.
27 
28 class UDPIOEvent : public Event
29 {
30 public:
UDPIOEvent()31   UDPIOEvent() : b(nullptr){};
~UDPIOEvent()32   ~UDPIOEvent() override{};
33 
34   void
setInfo(int fd_,const Ptr<IOBufferBlock> & b_,int bytesTransferred_,int errno_)35   setInfo(int fd_, const Ptr<IOBufferBlock> &b_, int bytesTransferred_, int errno_)
36   {
37     fd               = fd_;
38     b                = b_;
39     bytesTransferred = bytesTransferred_;
40     err              = errno_;
41   };
42 
43   void
setInfo(int fd_,struct msghdr * m_,int bytesTransferred_,int errno_)44   setInfo(int fd_, struct msghdr *m_, int bytesTransferred_, int errno_)
45   {
46     fd               = fd_;
47     m                = m_;
48     bytesTransferred = bytesTransferred_;
49     err              = errno_;
50   };
51 
52   void
setHandle(void * v)53   setHandle(void *v)
54   {
55     handle = v;
56   }
57 
58   void *
getHandle()59   getHandle()
60   {
61     return handle;
62   }
63 
64   int
getBytesTransferred() const65   getBytesTransferred() const
66   {
67     return bytesTransferred;
68   }
69 
70   IOBufferBlock *
getIOBufferBlock() const71   getIOBufferBlock() const
72   {
73     return b.get();
74   }
75 
76   int
getError() const77   getError() const
78   {
79     return err;
80   }
81 
82   Continuation *
getContinuation() const83   getContinuation() const
84   {
85     return continuation;
86   }
87 
88   void free();
89   static void free(UDPIOEvent *e);
90 
91 private:
92   void *operator new(size_t size); // undefined
93   int fd           = -1;
94   int err          = 0; // error code
95   struct msghdr *m = nullptr;
96   void *handle     = nullptr; // some extra data for the client handler
97   Ptr<IOBufferBlock> b;       // holds buffer that I/O will go to
98   int bytesTransferred = 0;   // actual bytes transferred
99 };
100 
101 extern ClassAllocator<UDPIOEvent> UDPIOEventAllocator;
102 TS_INLINE void
free(UDPIOEvent * e)103 UDPIOEvent::free(UDPIOEvent *e)
104 {
105   e->b     = nullptr;
106   e->mutex = nullptr;
107   UDPIOEventAllocator.free(e);
108 }
109