23 using System.Globalization;
30 namespace Ntp.Analyzer.Monitor.Server
42 IPAddress address = IPAddress.Parse(ip);
43 endPoint =
new IPEndPoint(address, port);
69 listenSocket =
new Socket(
70 AddressFamily.InterNetwork,
74 listenSocket.Bind(endPoint);
75 listenSocket.Listen(3);
77 listenSocket.BeginAccept(AcceptCallback, null);
81 log.WriteLine(
"Error while initializing listener " + ToString(),
Severity.Error);
82 log.WriteLine(ex.Message,
Severity.Debug);
89 return endPoint.ToString();
100 Socket serverSocket = listenSocket.EndAccept(asyncAccept);
102 if (serverSocket.Connected ==
false)
105 var req =
new Request(128, serverSocket);
107 serverSocket.BeginReceive(
119 log.WriteLine(
"Unexpected error in listener " + ToString(),
Severity.Error);
120 log.WriteLine(ex.Message,
Severity.Debug);
134 var req = (
Request) asyncReceive.AsyncState;
135 int size = req.Socket.EndReceive(asyncReceive);
142 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
148 sendBuffer = Encoding.UTF8.GetBytes(
"Unknown command");
154 sendBuffer = (byte[]) command.
Execute();
157 var text = command.
Execute() as string;
158 sendBuffer = text != null ? Encoding.UTF8.GetBytes(text) :
new byte[] {0};
161 sendBuffer = Encoding.UTF8.GetBytes(
"Error in command module. Unknown command type.");
165 req.Socket.BeginSend(
175 log.WriteLine(
"Unexpected error in listener " + ToString(),
Severity.Error);
176 log.WriteLine(ex.Message,
Severity.Debug);
189 var serverSocket = (Socket) asyncSend.AsyncState;
190 serverSocket.EndSend(asyncSend);
191 serverSocket.Shutdown(SocketShutdown.Both);
192 serverSocket.Close();
195 listenSocket.BeginAccept(AcceptCallback, null);
199 log.WriteLine(
"Unexpected error in listener " + ToString(),
Severity.Error);
200 log.WriteLine(ex.Message,
Severity.Debug);
205 #region IDisposable Support 214 if (disposing && listenSocket != null)
216 if (listenSocket.Connected)
218 listenSocket.Shutdown(SocketShutdown.Both);
219 listenSocket.Close();
222 listenSocket.Dispose();
226 disposedValue =
true;
237 GC.SuppressFinalize(
this);
void ReceiveCallback(IAsyncResult asyncReceive)
Receives the callback from client.
void Open()
Open this listener.
void SendCallback(IAsyncResult asyncSend)
Sends the callback response to client.
abstract CommandType CommandType
Listener(string ip, int port, LogBase log)
Initializes a new instance of the Listener class.
readonly IPEndPoint endPoint
override string ToString()
void AcceptCallback(IAsyncResult asyncAccept)
Accepts the callback from client.
void Close()
Close this listener.
void Dispose(bool disposing)
static Command Create(string command, string[] args)
abstract object Execute()