Ejemplo n.º 1
0
    public async void Relay(string message)
    {
        var package = new InternalComObject();

        package.Add  = false;
        package.Body = message;

        var buffer = new byte[BufferSize];
        var data   = JsonConvert.SerializeObject(package);

        byte[] encrypted = new byte[BufferSize];

        foreach (var other in others)
        {
            var client = new TcpClient();
            try
            {
                await client.ConnectAsync(other.Split(':')[0], int.Parse(other.Split(':')[1]));

                var sw = client.GetStream();

                var b = Encrypt(data);
                sw.Write(b, 0, b.Length);
                sw.Flush();
                sw.Dispose();
                client.Dispose();
            }
            catch
            {
                Console.WriteLine("Can't connect to other servers");
            }
        }
    }
Ejemplo n.º 2
0
    public async void StartCom()
    {
        var package = new ComObject();

        package.Address   = myAddress;
        package.Websocket = myWebsocket;
        package.New       = true;
        var buffer = new byte[BufferSize];

        var data = JsonConvert.SerializeObject(package);

        var client = new TcpClient();

        try
        {
            await client.ConnectAsync("localhost", 6000); //Proxy

            var sw = client.GetStream();
            var sr = client.GetStream();

            var b_data = Encrypt(data);
            sw.Write(b_data, 0, b_data.Length);
            sw.Flush();

            sr.Read(buffer, 0, BufferSize);
            var settings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            var connectTo = JsonConvert.DeserializeObject <List <string> >(Decrypt(buffer), settings);

            foreach (var s in connectTo)
            {
                var add = new TcpClient();
                await add.ConnectAsync(s.Split(':')[0], int.Parse(s.Split(':')[1])); //Other server

                var temp = add.GetStream();

                var InternalPackage = new InternalComObject();
                InternalPackage.Add  = true;
                InternalPackage.Body = myAddress;

                var b = Encrypt(JsonConvert.SerializeObject(InternalPackage, settings));
                temp.Write(b, 0, b.Length);
                temp.Flush();
                others.Add(s);
            }

            sr.Dispose();
            sw.Dispose();
            client.Dispose();
            ListenForOthers();
        }
        catch
        {
            Console.WriteLine("Can't see server");
            Environment.Exit(-1);
        }
    }