-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathIOObject.cs
148 lines (129 loc) · 5.31 KB
/
IOObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright (c) 2009-2011 250bpm s.r.o.
Copyright (c) 2007-2009 iMatix Corporation
Copyright (c) 2007-2015 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Diagnostics;
using System.Net.Sockets;
using AsyncIO;
namespace NetMQ.Core
{
/// <summary>
/// Simple base class for objects that live in I/O threads.
/// It makes communication with the poller object easier and
/// makes defining unneeded event handlers unnecessary.
/// </summary>
internal class IOObject : IProactorEvents
{
private IOThread? m_ioThread;
private IProactorEvents? m_handler;
/// <summary>
/// Create a new IOObject object and plug it into the given IOThread.
/// </summary>
/// <param name="ioThread">the IOThread to plug this new IOObject into.</param>
public IOObject(IOThread? ioThread)
{
if (ioThread != null)
Plug(ioThread);
}
/// <summary>
/// "Plug in" this IOObject to the given IOThread, - ie associate this with the specified IOThread.
/// </summary>
/// <param name="ioThread">the IOThread for this object to live in</param>
/// <remarks>
/// When migrating an object from one I/O thread to another, first
/// unplug it, then migrate it, then plug it to the new thread.
/// </remarks>
public void Plug(IOThread ioThread)
{
Debug.Assert(ioThread != null);
m_ioThread = ioThread;
}
/// <summary>
/// "Un-Plug" this IOObject from its current IOThread, and set its handler to null.
/// </summary>
/// <remarks>
/// When migrating an object from one I/O thread to another, first
/// unplug it, then migrate it, then plug it to the new thread.
/// </remarks>
public void Unplug()
{
Assumes.NotNull(m_ioThread);
// Forget about old poller in preparation to be migrated
// to a different I/O thread.
m_ioThread = null;
m_handler = null;
}
/// <summary>
/// Add the given socket to the Proactor.
/// </summary>
/// <param name="socket">the AsyncSocket to add</param>
public void AddSocket(AsyncSocket socket)
{
Assumes.NotNull(m_ioThread);
m_ioThread.Proactor.AddSocket(socket, this);
}
/// <summary>
/// Remove the given socket from the Proactor.
/// </summary>
/// <param name="socket">the AsyncSocket to remove</param>
public void RemoveSocket(AsyncSocket socket)
{
Assumes.NotNull(m_ioThread);
m_ioThread.Proactor.RemoveSocket(socket);
}
/// <summary>
/// This method is called when a message receive operation has been completed. This forwards it on to the handler's InCompleted method.
/// </summary>
/// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
/// <param name="bytesTransferred">the number of bytes that were transferred</param>
public virtual void InCompleted(SocketError socketError, int bytesTransferred)
{
Assumes.NotNull(m_handler);
m_handler.InCompleted(socketError, bytesTransferred);
}
/// <summary>
/// This method is called when a message Send operation has been completed. This forwards it on to the handler's OutCompleted method.
/// </summary>
/// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
/// <param name="bytesTransferred">the number of bytes that were transferred</param>
public virtual void OutCompleted(SocketError socketError, int bytesTransferred)
{
Assumes.NotNull(m_handler);
m_handler.OutCompleted(socketError, bytesTransferred);
}
/// <summary>
/// This is called when the timer expires.
/// </summary>
/// <param name="id">an integer used to identify the timer</param>
public virtual void TimerEvent(int id)
{
m_handler?.TimerEvent(id);
}
public void AddTimer(long timeout, int id)
{
Assumes.NotNull(m_ioThread);
m_ioThread.Proactor.AddTimer(timeout, this, id);
}
public void SetHandler(IProactorEvents handler)
{
m_handler = handler;
}
public void CancelTimer(int id)
{
Assumes.NotNull(m_ioThread);
m_ioThread.Proactor.CancelTimer(this, id);
}
}
}