-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
118 lines (112 loc) · 4.14 KB
/
Program.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Data.SQLite;
using System.Net;
using System.Net.Sockets;
using FileSyncServer.Config;
using FileSyncServer.Startup;
using Newtonsoft.Json;
using Serilog;
using Microsoft.Data.Sqlite;
using RocksDbSharp;
namespace FileSyncServer;
internal class Program
{
private static async Task<int> Main(string[] args)
{
try
{
Console.WriteLine($"Running in: {AppDomain.CurrentDomain.BaseDirectory}");
//INITIALIZING LOGGING
Console.WriteLine("STARTING UP");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LogFiles", "Log.txt"),
rollingInterval: RollingInterval.Month,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}")
.CreateLogger();
Console.WriteLine("LOADING CONFIG");
Log.Information("LOADING CONFIG");
//LOADING CONFIG
JsonSerializer jsonSerializer = new JsonSerializer();
Settings settings;
try
{
settings = jsonSerializer.Deserialize<Settings>(new JsonTextReader(File.OpenText("config.json")));
if (settings is null)
{
Log.Error("Couldn't load config");
return -1;
}
}
catch (Exception e)
{
Log.Error("Couldn't load config");
Console.WriteLine(e);
throw;
}
Log.Information("CONFIG LOADED SUCCESSFULLY");
Log.Information("LOADING ROCKSDB");
RocksDb rocksDb;
//INITIALIZING DB
try
{
var options = new DbOptions()
.SetCreateIfMissing(true);
rocksDb = RocksDb.Open(options, "rocks.db");
}
catch (Exception e)
{
Log.Error("Couldn't load/create database");
Console.WriteLine(e);
throw;
}
Log.Information("ROCKSDB LOADED SUCCESSFULLY");
Console.WriteLine("CONFIG LOADED");
//Establishing listener
IPHostEntry ipHostInfo;
IPAddress ipAddress;
try
{
ipHostInfo = await Dns.GetHostEntryAsync(settings.HostName);
ipAddress = ipHostInfo.AddressList[0];
}
catch (Exception e)
{
try
{
Console.WriteLine("Trying parsing");
Log.Information("Trying parsing");
ipAddress = IPAddress.Parse(settings.HostName);
Console.WriteLine("Success !");
Log.Information("Success !");
}
catch (Exception exception)
{
Console.WriteLine(exception);
Log.Error("WRONG HOST IP");
throw;
}
}
IPEndPoint ipEndPoint = new(ipAddress, settings.Port);
var tcpListener = new TcpListener(ipEndPoint);
tcpListener.Start();
Console.WriteLine($"CONNECTION EXPOSED: {ipEndPoint.Address}:{ipEndPoint.Port}");
Log.Information("CONNECTION EXPOSED: {address}:{port}",ipEndPoint.Address,ipEndPoint.Port);
//Waiting for a new client
while (true)
{
Socket client = tcpListener.AcceptSocket();
//INITIATE NEW CLIENT CONNECTION
ClientConnection newClientConnection = new ClientConnection(client,rocksDb);
Console.WriteLine($"CLIENT CONNECTED: {((IPEndPoint)client.LocalEndPoint).Address.ToString()}");
Log.Information("CLIENT CONNECTED {addrfamily}", ((IPEndPoint)client.LocalEndPoint).Address.ToString());
}
return 0;
}
catch (Exception e)
{
Log.Error(e.ToString());
return 100;
}
return 0;
}
}