NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Analyzer.Monitor.Server.Listener Class Reference
Inheritance diagram for Ntp.Analyzer.Monitor.Server.Listener:
Collaboration diagram for Ntp.Analyzer.Monitor.Server.Listener:

Public Member Functions

 Listener (string ip, int port, LogBase log)
 Initializes a new instance of the Listener class. More...
 
void Close ()
 Close this listener. More...
 
void Dispose ()
 
void Open ()
 Open this listener. More...
 
override string ToString ()
 

Private Member Functions

 ~Listener ()
 
void AcceptCallback (IAsyncResult asyncAccept)
 Accepts the callback from client. More...
 
void Dispose (bool disposing)
 
void ReceiveCallback (IAsyncResult asyncReceive)
 Receives the callback from client. More...
 
void SendCallback (IAsyncResult asyncSend)
 Sends the callback response to client. More...
 

Private Attributes

bool disposedValue
 
readonly IPEndPoint endPoint
 
Socket listenSocket
 
readonly LogBase log
 
bool shuttingDown
 

Detailed Description

Definition at line 32 of file Listener.cs.

Constructor & Destructor Documentation

Ntp.Analyzer.Monitor.Server.Listener.Listener ( string  ip,
int  port,
LogBase  log 
)
inline

Initializes a new instance of the Listener class.

Parameters
ipIP Address to listen on.
portPort number.
logLog.

Definition at line 40 of file Listener.cs.

41  {
42  IPAddress address = IPAddress.Parse(ip);
43  endPoint = new IPEndPoint(address, port);
44  this.log = log;
45  shuttingDown = false;
46  }
readonly IPEndPoint endPoint
Definition: Listener.cs:48
Ntp.Analyzer.Monitor.Server.Listener.~Listener ( )
inlineprivate

Definition at line 229 of file Listener.cs.

230  {
231  Dispose(false);
232  }

Member Function Documentation

void Ntp.Analyzer.Monitor.Server.Listener.AcceptCallback ( IAsyncResult  asyncAccept)
inlineprivate

Accepts the callback from client.

Parameters
asyncAcceptAsync accept.

Definition at line 96 of file Listener.cs.

97  {
98  try
99  {
100  Socket serverSocket = listenSocket.EndAccept(asyncAccept);
101 
102  if (serverSocket.Connected == false)
103  return;
104 
105  var req = new Request(128, serverSocket);
106 
107  serverSocket.BeginReceive(
108  req.Buffer,
109  0,
110  req.Buffer.Length,
111  SocketFlags.None,
113  req);
114  }
115  catch (Exception ex)
116  {
117  if (!shuttingDown)
118  {
119  log.WriteLine("Unexpected error in listener " + ToString(), Severity.Error);
120  log.WriteLine(ex.Message, Severity.Debug);
121  log.WriteLine(ex, Severity.Trace);
122  }
123  }
124  }
abstract void WriteLine(string text, Severity severity)
void ReceiveCallback(IAsyncResult asyncReceive)
Receives the callback from client.
Definition: Listener.cs:130
void Ntp.Analyzer.Monitor.Server.Listener.Close ( )
inline

Close this listener.

Definition at line 56 of file Listener.cs.

57  {
58  shuttingDown = true;
59  listenSocket.Close();
60  }
void Ntp.Analyzer.Monitor.Server.Listener.Dispose ( bool  disposing)
inlineprivate

Definition at line 209 of file Listener.cs.

210  {
211  if (disposedValue)
212  return;
213 
214  if (disposing && listenSocket != null)
215  {
216  if (listenSocket.Connected)
217  {
218  listenSocket.Shutdown(SocketShutdown.Both);
219  listenSocket.Close();
220  }
221 
222  listenSocket.Dispose();
223  listenSocket = null;
224  }
225 
226  disposedValue = true;
227  }
void Ntp.Analyzer.Monitor.Server.Listener.Dispose ( )
inline

Definition at line 234 of file Listener.cs.

235  {
236  Dispose(true);
237  GC.SuppressFinalize(this);
238  }
void Ntp.Analyzer.Monitor.Server.Listener.Open ( )
inline

Open this listener.

Definition at line 65 of file Listener.cs.

66  {
67  try
68  {
69  listenSocket = new Socket(
70  AddressFamily.InterNetwork,
71  SocketType.Stream,
72  ProtocolType.Tcp);
73 
74  listenSocket.Bind(endPoint);
75  listenSocket.Listen(3);
76 
77  listenSocket.BeginAccept(AcceptCallback, null);
78  }
79  catch (Exception ex)
80  {
81  log.WriteLine("Error while initializing listener " + ToString(), Severity.Error);
82  log.WriteLine(ex.Message, Severity.Debug);
83  log.WriteLine(ex, Severity.Trace);
84  }
85  }
abstract void WriteLine(string text, Severity severity)
readonly IPEndPoint endPoint
Definition: Listener.cs:48
void AcceptCallback(IAsyncResult asyncAccept)
Accepts the callback from client.
Definition: Listener.cs:96
void Ntp.Analyzer.Monitor.Server.Listener.ReceiveCallback ( IAsyncResult  asyncReceive)
inlineprivate

Receives the callback from client.

Parameters
asyncReceiveAsync receive.

Definition at line 130 of file Listener.cs.

References Ntp.Analyzer.Monitor.Server.Command.CommandType, Ntp.Analyzer.Monitor.Server.CommandFactory.Create(), and Ntp.Analyzer.Monitor.Server.Command.Execute().

131  {
132  try
133  {
134  var req = (Request) asyncReceive.AsyncState;
135  int size = req.Socket.EndReceive(asyncReceive);
136  req.ReSize(size);
137 
138  // Process request
139  Command command = CommandFactory.Create(req.Command, req.Arguments);
140 
141  // Force correct time format in strings, etc.
142  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
143 
144  byte[] sendBuffer;
145 
146  if (command == null)
147  {
148  sendBuffer = Encoding.UTF8.GetBytes("Unknown command");
149  }
150  else
151  switch (command.CommandType)
152  {
153  case CommandType.Binary:
154  sendBuffer = (byte[]) command.Execute();
155  break;
156  case CommandType.Text:
157  var text = command.Execute() as string;
158  sendBuffer = text != null ? Encoding.UTF8.GetBytes(text) : new byte[] {0};
159  break;
160  default:
161  sendBuffer = Encoding.UTF8.GetBytes("Error in command module. Unknown command type.");
162  break;
163  }
164 
165  req.Socket.BeginSend(
166  sendBuffer,
167  0,
168  sendBuffer.Length,
169  SocketFlags.None,
170  SendCallback,
171  req.Socket);
172  }
173  catch (Exception ex)
174  {
175  log.WriteLine("Unexpected error in listener " + ToString(), Severity.Error);
176  log.WriteLine(ex.Message, Severity.Debug);
177  log.WriteLine(ex, Severity.Trace);
178  }
179  }
abstract void WriteLine(string text, Severity severity)
void SendCallback(IAsyncResult asyncSend)
Sends the callback response to client.
Definition: Listener.cs:185

Here is the call graph for this function:

void Ntp.Analyzer.Monitor.Server.Listener.SendCallback ( IAsyncResult  asyncSend)
inlineprivate

Sends the callback response to client.

Parameters
asyncSendAsync send.

Definition at line 185 of file Listener.cs.

186  {
187  try
188  {
189  var serverSocket = (Socket) asyncSend.AsyncState;
190  serverSocket.EndSend(asyncSend);
191  serverSocket.Shutdown(SocketShutdown.Both);
192  serverSocket.Close();
193 
194  // Start new worker socket.
195  listenSocket.BeginAccept(AcceptCallback, null);
196  }
197  catch (Exception ex)
198  {
199  log.WriteLine("Unexpected error in listener " + ToString(), Severity.Error);
200  log.WriteLine(ex.Message, Severity.Debug);
201  log.WriteLine(ex, Severity.Trace);
202  }
203  }
abstract void WriteLine(string text, Severity severity)
void AcceptCallback(IAsyncResult asyncAccept)
Accepts the callback from client.
Definition: Listener.cs:96
override string Ntp.Analyzer.Monitor.Server.Listener.ToString ( )
inline

Definition at line 87 of file Listener.cs.

88  {
89  return endPoint.ToString();
90  }
readonly IPEndPoint endPoint
Definition: Listener.cs:48

Member Data Documentation

bool Ntp.Analyzer.Monitor.Server.Listener.disposedValue
private

Definition at line 207 of file Listener.cs.

readonly IPEndPoint Ntp.Analyzer.Monitor.Server.Listener.endPoint
private

Definition at line 48 of file Listener.cs.

Socket Ntp.Analyzer.Monitor.Server.Listener.listenSocket
private

Definition at line 50 of file Listener.cs.

readonly LogBase Ntp.Analyzer.Monitor.Server.Listener.log
private

Definition at line 49 of file Listener.cs.

bool Ntp.Analyzer.Monitor.Server.Listener.shuttingDown
private

Definition at line 51 of file Listener.cs.


The documentation for this class was generated from the following file: