1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Diagnostics;
24 using System.Net;
25 using System.Net.Sockets;
26 using System.Text;
27 using Ntp.Common.Process;
28 
29 namespace Ntp.Analyzer.Monitor.Client
30 {
31     public sealed class TextRequest : Request, IRequest
32     {
TextRequest(IPAddress ip, int port)33         public TextRequest(IPAddress ip, int port)
34             : base(ip, port)
35         {
36         }
37 
TextRequest(IPEndPoint server)38         public TextRequest(IPEndPoint server)
39             : base(server)
40         {
41         }
42 
43         private StringBuilder result;
44 
Send(string command)45         public string Send(string command)
46         {
47             var clientSocket = new Socket(
48                 AddressFamily.InterNetwork,
49                 SocketType.Stream,
50                 ProtocolType.Tcp);
51 
52             try
53             {
54                 IAsyncResult conRes = clientSocket.BeginConnect(Server, null, null);
55                 bool success = conRes.AsyncWaitHandle.WaitOne(3000, true);
56 
57                 if (!success)
58                 {
59                     throw new ApplicationException("Failed to connect to server.");
60                 }
61 
62                 byte[] sendBuffer = Encoding.UTF8.GetBytes(command);
63 
64                 SocketError error;
65                 clientSocket.Send(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, out error);
66 
67                 const int size = 4096;
68 
69                 result = new StringBuilder(4096);
70                 var buffer = new byte[size];
71 
72                 int length;
73 
74                 do
75                 {
76                     length = clientSocket.Receive(buffer, 0, size, SocketFlags.None, out error);
77                     string response = Encoding.ASCII.GetString(buffer);
78                     response = response.Substring(0, length);
79                     result.Append(response);
80                 } while (length > 0);
81 
82                 return result.ToString();
83             }
84             catch (Exception ex)
85             {
86                 Debug.WriteLine(ex.Message);
87                 return "Failed to connect to server.";
88             }
89             finally
90             {
91                 if (clientSocket.IsBound)
92                     clientSocket.Shutdown(SocketShutdown.Both);
93 
94                 clientSocket.Close();
95             }
96         }
97     }
98 }