-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionObject.cs
More file actions
116 lines (105 loc) · 3.42 KB
/
Copy pathConnectionObject.cs
File metadata and controls
116 lines (105 loc) · 3.42 KB
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
using System;
using System.Configuration;
using System.Net.Sockets;
using System.Threading;
namespace RESTfulFish
{
public class ConnectionObject : IDisposable
{
public static TcpClient tc;
public static NetworkStream tcS;
private static EndianBinaryWriter bw;
private static EndianBinaryReader br;
/*
* Some house keeping
*/
~ConnectionObject()
{
tc.GetStream().Close();
tc.Close();
}
/*
* Create the server connection
*/
public ConnectionObject()
{
}
public static void Connect()
{
string FishbowlServer = ConfigurationManager.AppSettings.Get("FishbowlServer");
int FishbowlPort = Int32.Parse(ConfigurationManager.AppSettings.Get("FishbowlPort"));
try
{
tc = new TcpClient(FishbowlServer, FishbowlPort);
tc.NoDelay = true;
tc.ReceiveBufferSize = 8192;
tc.SendBufferSize = 8192;
tc.ReceiveTimeout = 10000;
tc.SendTimeout = 1000;
}
catch (Exception e)
{
Console.WriteLine("ConnectionObject.Connect(): {0}", e);
Console.ReadLine();
System.Environment.Exit(1);
}
tcS = tc.GetStream();
bw = new EndianBinaryWriter(new BigEndianBitConverter(), tcS);
br = new EndianBinaryReader(new BigEndianBitConverter(), tcS);
}
/*
* Send the JSON/XML request string
*/
public static String sendCommand(string command)
{
try
{
bw = new EndianBinaryWriter(new BigEndianBitConverter(), tcS);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytes = encoding.GetBytes(command);
bw.Write(bytes.Length);
bw.Write(bytes);
bw.Flush();
Thread.Sleep(1000);
br = new EndianBinaryReader(new BigEndianBitConverter(), tcS);
int i = br.ReadInt32();
byte[] bytess = new byte[i];
br.Read(bytess, 0, i);
String response = encoding.GetString(bytess, 0, i);
return response;
}
catch (Exception e)
{
return FishbowlLegacy.GenericXMLError(e.ToString());
}
}
/*
* Check when Fishbowl server starts
*/
public static bool IsFishbowlPortOpen()
{
string FishbowlServer = ConfigurationManager.AppSettings.Get("FishbowlServer");
int FishbowlPort = Int32.Parse(ConfigurationManager.AppSettings.Get("FishbowlPort"));
int timeout = 5;
try
{
using (var client = new TcpClient())
{
var result = client.BeginConnect(FishbowlServer, FishbowlPort, null, null);
var success = result.AsyncWaitHandle.WaitOne(timeout);
client.EndConnect(result);
return success;
}
}
catch
{
return false;
}
}
public void Dispose()
{
tc.GetStream().Close();
tc.Close();
}
}
}