‪Black Box
Static Public Attributes | Static Private Member Functions | Static Private Attributes
BBPresenseSensor.Program Class Reference

Static Public Attributes

static bool isItRunning = false
 

Static Private Member Functions

static void Main (string[] args)
 
static void StartListening ()
 ‪Listen for client connections More...
 
static void AcceptCallback (IAsyncResult ar)
 ‪Handler for new connections More...
 
static void ReceiveCallback (IAsyncResult ar)
 ‪Handler for received messages More...
 
static bool CheckState (IAsyncResult ar, out string err, out BBSensorObjects client)
 ‪Checks IAsyncResult for null value More...
 
static void CloseClient (BBSensorObjects client)
 ‪Closes a client socket and removes it from the client list More...
 
static void CloseAllSockets ()
 ‪Closes all client and server connections More...
 

Static Private Attributes

static readonly string BBconection = ConfigurationManager.ConnectionStrings["BB_Sensor_CONNECTION"].ConnectionString
 
static List< BBSensorObjects_sensors
 
static ManualResetEvent _connected = new ManualResetEvent(false)
 
static Socket _sensorServer = null
 

Detailed Description

Definition at line 49 of file Program.cs.

Member Function Documentation

◆ AcceptCallback()

static void BBPresenseSensor.Program.AcceptCallback ( IAsyncResult  ar)
staticprivate

‪Handler for new connections

Parameters
ar

Definition at line 126 of file Program.cs.

127  {
128  // Signal the main thread to continue accepting new connections
129  ‪_connected.Set();
130 
131  // Accept new client socket connection
132  Socket socket = ‪_sensorServer.EndAccept(ar);
133 
134  // Create a new client connection object and store the socket
136  {
137  Socket = socket
138  };
139 
140  // Store all clients
141  ‪_sensors.Add(client);
142 
143  // Begin receiving messages from new connection
144  try
145  {
146  client.‪Socket.BeginReceive(client.‪Buffer, 0, client.‪BufferSize, SocketFlags.None, new AsyncCallback(‪ReceiveCallback), client);
147  }
148  catch (SocketException)
149  {
150  // Client was forcibly closed on the client side
151  ‪CloseClient(client);
152  }
153  catch (Exception ex)
154  {
155  // Set operation status to true
156 
157  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
158  }
159  }
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪static void ReceiveCallback(IAsyncResult ar)
‪Handler for received messages
Definition: Program.cs:169
‪static void CloseClient(BBSensorObjects client)
‪Closes a client socket and removes it from the client list
Definition: Program.cs:274
‪static Socket _sensorServer
Definition: Program.cs:67
‪static List< BBSensorObjects > _sensors
Definition: Program.cs:57
‪static ManualResetEvent _connected
Definition: Program.cs:60

Referenced by BBPresenseSensor.Program.StartListening().

◆ CheckState()

static bool BBPresenseSensor.Program.CheckState ( IAsyncResult  ar,
out string  err,
out BBSensorObjects  client 
)
staticprivate

‪Checks IAsyncResult for null value

Parameters
ar
err
client
Returns

Definition at line 240 of file Program.cs.

241  {
242  // Initialize
243  client = null;
244  err = "";
245 
246  // Check ar
247  if (ar == null)
248  {
249  err = "Async result null";
250 
251  return false;
252  }
253 
254  // Check client
255  client = (‪BBSensorObjects)ar.AsyncState;
256  if (client == null)
257  {
258  err = "Client null";
259 
260  return false;
261  }
262 
263  return true;
264  }

Referenced by BBPresenseSensor.Program.ReceiveCallback().

◆ CloseAllSockets()

static void BBPresenseSensor.Program.CloseAllSockets ( )
staticprivate

‪Closes all client and server connections

Definition at line 298 of file Program.cs.

299  {
300  // Close all clients
301  foreach (‪BBSensorObjects connection in ‪_sensors)
302  {
303  connection.‪Close();
304  }
305 
306  // Close server
307  ‪_sensorServer.Close();
308  }
‪void Close()
‪Closes the connection
‪static Socket _sensorServer
Definition: Program.cs:67
‪static List< BBSensorObjects > _sensors
Definition: Program.cs:57

Referenced by BBPresenseSensor.Program.Main().

◆ CloseClient()

static void BBPresenseSensor.Program.CloseClient ( BBSensorObjects  client)
staticprivate

‪Closes a client socket and removes it from the client list

Parameters
client

Definition at line 274 of file Program.cs.

275  {
276  ‪BBSensorErrorLog.‪SaveErrorData("Client disconnected");
277  client.‪Close();
278  if (‪_sensors.Contains(client))
279  {
280  ‪_sensors.Remove(client);
281  }
282 
283  //Console.WriteLine(_sensors.Count().ToString());
284 
285  foreach (‪BBSensorObjects sensor in ‪_sensors)
286  {
287  Console.WriteLine(sensor);
288  }
289  }
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪void Close()
‪Closes the connection
‪static List< BBSensorObjects > _sensors
Definition: Program.cs:57

Referenced by BBPresenseSensor.Program.AcceptCallback(), and BBPresenseSensor.Program.ReceiveCallback().

◆ Main()

static void BBPresenseSensor.Program.Main ( string []  args)
staticprivate

Definition at line 73 of file Program.cs.

74  {
76  Console.Title = "BBPresenceSensor";
77 
78  ‪_sensors = new List<BBSensorObjects>();
80  Console.ReadLine();
82  }
‪static readonly string BBconection
Definition: Program.cs:54
‪static void StartListening()
‪Listen for client connections
Definition: Program.cs:91
‪static void parseConnectionData(string connection)
‪Parse connection data
‪static List< BBSensorObjects > _sensors
Definition: Program.cs:57
‪static void CloseAllSockets()
‪Closes all client and server connections
Definition: Program.cs:298

◆ ReceiveCallback()

static void BBPresenseSensor.Program.ReceiveCallback ( IAsyncResult  ar)
staticprivate

‪Handler for received messages

Parameters
ar

Definition at line 169 of file Program.cs.

170  {
171  int bytesRead;
172 
173  // Check for null values
174  if (!‪CheckState(ar, out string err, out ‪BBSensorObjects client))
175  {
177 
178  return;
179  }
180 
181  // Read message from the client socket
182  try
183  {
184  bytesRead = client.Socket.EndReceive(ar);
185  }
186  catch (SocketException)
187  {
188  // Client was forcedly closed on the client side
189  ‪CloseClient(client);
190 
191  return;
192  }
193  catch (Exception ex)
194  {
195  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
196 
197  return;
198  }
199 
200  // Check message
201  if (bytesRead > 0)
202  {
203  // Build message as it comes in
204  client.BuildIncomingMessage(bytesRead);
205 
206  // Print message to the console
207  client.PrintMessage();
208 
209  // Reset message
210  client.ClearIncomingMessage();
211  }
212 
213  // Listen for more incoming messages
214  try
215  {
216  client.Socket.BeginReceive(client.Buffer, 0, client.BufferSize, SocketFlags.None, new AsyncCallback(‪ReceiveCallback), client);
217  }
218  catch (SocketException)
219  {
220  // Client was forcedly closed on the client side
221  ‪CloseClient(client);
222  }
223  catch (Exception ex)
224  {
225  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
226  }
227  }
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪static void ReceiveCallback(IAsyncResult ar)
‪Handler for received messages
Definition: Program.cs:169
‪static void CloseClient(BBSensorObjects client)
‪Closes a client socket and removes it from the client list
Definition: Program.cs:274
‪static bool CheckState(IAsyncResult ar, out string err, out BBSensorObjects client)
‪Checks IAsyncResult for null value
Definition: Program.cs:240

Referenced by BBPresenseSensor.Program.AcceptCallback().

◆ StartListening()

static void BBPresenseSensor.Program.StartListening ( )
staticprivate

‪Listen for client connections

Definition at line 91 of file Program.cs.

92  {
93  try
94  {
96 
97  while (true)
98  {
99  // Set the event to non signaled state
100  ‪_connected.Reset();
101 
102  // Start an asynchronous socket to listen for connections
103  ‪_sensorServer.BeginAccept(new AsyncCallback(‪AcceptCallback), ‪_sensorServer);
104 
105  // Set operation status to true
107 
108  // Wait until a connection is made before continuing
109  ‪_connected.WaitOne();
110  }
111  }
112  catch (Exception ex)
113  {
114  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
115  }
116  }
‪static Socket CreateListener()
‪Create the connection needed for the TCP connection
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪static Socket _sensorServer
Definition: Program.cs:67
‪static ManualResetEvent _connected
Definition: Program.cs:60
‪static void AcceptCallback(IAsyncResult ar)
‪Handler for new connections
Definition: Program.cs:126

Referenced by BBPresenseSensor.Program.Main().

Field Documentation

◆ _connected

ManualResetEvent BBPresenseSensor.Program._connected = new ManualResetEvent(false)
staticprivate

◆ _sensors

List<BBSensorObjects> BBPresenseSensor.Program._sensors
staticprivate

◆ _sensorServer

Socket BBPresenseSensor.Program._sensorServer = null
staticprivate

◆ BBconection

readonly string BBPresenseSensor.Program.BBconection = ConfigurationManager.ConnectionStrings["BB_Sensor_CONNECTION"].ConnectionString
staticprivate

Definition at line 54 of file Program.cs.

Referenced by BBPresenseSensor.Program.Main().

◆ isItRunning

bool BBPresenseSensor.Program.isItRunning = false
static

Definition at line 64 of file Program.cs.


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