-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
99 lines (88 loc) · 3.27 KB
/
MainWindow.xaml.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
92
93
94
95
96
97
98
99
using LocalIM.Network;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LocalIM
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
InitListener();
new Background.BackgroundWorker(ViewModel);
}
MainViewModel ViewModel
{
get { return (MainViewModel)DataContext; }
}
#region network related
private void InitListener()
{
var ts = new System.Threading.ThreadStart(ListenerAction);
var t = new System.Threading.Thread(ts) { IsBackground = true };
t.Start();
}
public void DataReceived(Packet p)
{
if (p.SourceIP == Listener.Instance.LocalIP)
return; // ignore my self
if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header ,Headers.Init.WHO_IS_THERE))
{
//var pp = new Packet(Headers.Init.I_AM_HERE, txtMyUser.Text, Listener.Instance.LocalIP, new byte[] { 0 });
//var s = new MySender(ViewModel.UserName);
//s.BroadcastRaw(pp.ToRaw());
}
else
if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Init.I_AM_HERE))
{
ViewModel.CheckNewContact(p.UserIdText, p.SourceIP);
}
else
if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Message.MESSAGE))
{
var guidBytes = p.Data.Take(16).ToArray();
var messageBytes = p.Data.Skip(16).ToArray();
ViewModel.GotMessage(p.UserIdText, p.SourceIP, Encoding.Unicode.GetString(messageBytes), new Guid(guidBytes));
}
else
if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Message.MESSAGE_ACCEPTED))
{
var guidBytes = p.Data.Take(16).ToArray();
ViewModel.GotMessageConfirm(new Guid(guidBytes));
}
}
void ListenerAction()
{
var l = Listener.Instance;
while (System.Threading.Thread.CurrentThread.IsAlive)
{
var b = l.Receive();
if (b.Length > 0)
{
var p = new Packet(b);
Dispatcher.Invoke(() => { DataReceived(p); });
}
System.Threading.Thread.Sleep(50);
}
}
#endregion
}
}