-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.cs
More file actions
143 lines (128 loc) · 4.97 KB
/
updater.cs
File metadata and controls
143 lines (128 loc) · 4.97 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace IotUpdater
{
public class updater
{
TcpListener listener;
public enum FLASH_Options
{
FLASH = 0,
SPIFFS = 100,
AUTH = 200,
}
public bool update(string remoteAddr, int remotePort, int localPort, string password, byte[] file_data, FLASH_Options command = FLASH_Options.FLASH)
{
Console.WriteLine("Starting Java server");
IPAddress address = IPAddress.Any;
listener = new TcpListener(address, localPort);
listener.Server.NoDelay = true;
listener.Start();
Console.WriteLine($"Server started. Listening to TCP clients at 0.0.0.0:{localPort}");
long content_size = file_data.Length;
string file_md5 = file_data.ToMD5Hash().ToLower();
Console.WriteLine($"Upload size {content_size}");
string message = string.Format("0 {1} {2} {3}\n", command, localPort, content_size, file_md5);
byte[] message_bytes = Encoding.ASCII.GetBytes(message);
Console.WriteLine("Sending invitation to " + remoteAddr);
UdpClient sock2 = new UdpClient();
var ep = new IPEndPoint(IPAddress.Parse(remoteAddr), remotePort);
sock2.Send(message_bytes, message_bytes.Length, ep);
var t = sock2.ReceiveAsync();
t.Wait(10000);
var res = t.Result;
if (res == null)
{
Console.WriteLine("No Response");
{
//client.Close();
listener.Stop();
return false;
}
}
var res_text = Encoding.ASCII.GetString(res.Buffer);
if (res_text != "OK")
{
Console.WriteLine("AUTH requeired and not implemented");
{
//client.Close();
listener.Stop();
return false;
}
}
sock2.Close();
Console.WriteLine("Waiting for device ...");
listener.Server.SendTimeout = 10000;
listener.Server.ReceiveTimeout = 10000;
DateTime startTime = DateTime.Now;
TcpClient client = null;
Console.WriteLine("Awaiting Connection");
while ((DateTime.Now - startTime).TotalSeconds < 10)
{
if (listener.Pending())
{
client = listener.AcceptTcpClient();
client.NoDelay = true;
break;
}
else
Thread.Sleep(10);
}
if (client == null)
{
Console.WriteLine("No response from device");
{
listener.Stop();
return false;
}
}
Console.WriteLine("Got Connection");
using (MemoryStream fs = new MemoryStream(file_data))
{
int offset = 0;
byte[] chunk = new byte[1460];
int chunk_size = 0;
int read_count = 0;
string resp = "";
while (content_size > offset)
{
chunk_size = fs.Read(chunk, 0, 1460);
offset += chunk_size;
if (client.Available > 0)
{
resp = Encoding.ASCII.GetString(chunk, 0, read_count);
Console.Write(resp);
}
Console.WriteLine($"Written {offset} out of {content_size} ({(float)(offset)/(float)(content_size)*100})");
client.Client.Send(chunk, 0, chunk_size, SocketFlags.None);
client.ReceiveTimeout = 10000;
}
client.ReceiveTimeout = 60000;
read_count = client.Client.Receive(chunk);
resp = Encoding.ASCII.GetString(chunk, 0, read_count);
while (!resp.Contains("O"))
{
if (resp.Contains("E"))
{
client.Close();
listener.Stop();
return false;
}
read_count = client.Client.Receive(chunk);
resp = Encoding.ASCII.GetString(chunk, 0, read_count);
Console.Write(resp);
//return;
}
Console.WriteLine("\r\nAll done, Kill me now");
client.Close();
listener.Stop();
return true;
}
}
}
}