-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.cs
91 lines (80 loc) · 2.3 KB
/
Client.cs
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
using System;
using System.Net.Sockets;
using System.Threading;
using System.Windows;
using System.ComponentModel;
namespace DesktopApp
{
//Client class
public class Client
{
private int _port;
protected readonly TcpClient TcpClient;
protected readonly NetworkStream Stream;
protected bool IsRunning = false;
protected Client(int port)
{
_port = port;
try
{
TcpClient = new TcpClient("127.0.0.1", port);
Stream = TcpClient.GetStream();
IsRunning = true;
}
catch (Exception)
{
MessageBox.Show("Connection Error", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
//connect to the FlightGear app
public class FlightGearClient : Client, INotifyPropertyChanged
{
protected double SendSpeed;
public event PropertyChangedEventHandler PropertyChanged;
private int _numLine; //the line number that currently being sent.
public int _NumLine
{
get => _numLine;
set
{
//m.WaitOne();
_numLine = value;
//m.ReleaseMutex();
NotifyPropertyChanged("_Num_line");
}
}
protected static readonly Mutex M = new Mutex();
//Constructor
protected FlightGearClient() : base(5400)
{
SendSpeed = 10;
_NumLine = 0;
}
//Constructor
protected FlightGearClient(int port) : base(port)
{
SendSpeed = 10;
_NumLine = 0;
}
//Destructor
~FlightGearClient()
{
if (!IsRunning) return;
// Close socket.
Stream.Close();
TcpClient.Close();
}
//Sets the sending speed.
public void SetSpeed(double d)
{
M.WaitOne();
SendSpeed = d;
M.ReleaseMutex();
}
protected void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
}