Пример #1
0
    public void SendAdjustFrame()
    {
        IsAdjusting = true;
        foreach (IPAddress address in ActiveAddresses)
        {
            Debug.Log("Adjusting with IP " + address.ToString());
            if (Received.ContainsKey(address))
            {
                continue;
            }
            TcpClient     client        = new TcpClient(address.ToString(), Port);
            IPAddress     clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            NetworkStream stream        = client.GetStream();

            String data  = string.Format("{0}|{1}", (int)CMD.AdjustFrame, VideoSynchroniser.Instance().CurrentTick);
            byte[] bytes = GenerateByteMessage(data);
            stream.Write(bytes, 0, bytes.Length);
            data = GetMessageFromNetworkStream(stream);
            Received.Add(clientAddress, int.Parse(data));
            AdjustedCnt++;
        }
        Debug.Log("AdjustCnt " + AdjustedCnt + " AllowedCnt " + ActiveAddresses.Count);
        Monitor.Enter(LockObj);
        Int32 minFrame = Int32.MaxValue;

        if (AdjustedCnt == ActiveAddresses.Count)
        {
            Debug.Log("Adjsting!!!");
            foreach (KeyValuePair <IPAddress, Int32> item in Sent)
            {
                minFrame = Math.Min(minFrame, item.Value);
            }
            foreach (KeyValuePair <IPAddress, Int32> item in Received)
            {
                minFrame = Math.Min(minFrame, item.Value);
            }
            if (minFrame != Int32.MaxValue)
            {
                VideoSynchroniser.Instance().AdjustedFrame = minFrame;
            }
        }
        Sent.Clear();
        Received.Clear();
        AdjustedCnt = 0;
        MyFrame     = -1;
        IsAdjusting = false;
        ActiveAddresses.Clear();
        foreach (IPAddress el in SecondaryActiveAddresses)
        {
            ActiveAddresses.Add(el);
        }
    }
Пример #2
0
 private void Update()
 {
     AccumulatedTime += Time.deltaTime;
     if (AccumulatedTime > TickDelay)
     {
         AccumulatedTime = 0;
         int receivedFrame = VideoSynchroniser.Instance().IncreaseCurrentTick((int)VPlayer.frame);
         if (receivedFrame != -1)
         {
             VPlayer.frame = receivedFrame;
         }
     }
 }
Пример #3
0
    private static void ReceiveHelloMessage(IAsyncResult res)
    {
        KeyValuePair <TCPServer, TcpClient> pair = (KeyValuePair <TCPServer, TcpClient>)res.AsyncState;
        TCPServer serv   = pair.Key;
        TcpClient client = pair.Value;

        IPAddress     address = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
        NetworkStream stream  = client.GetStream();

        Debug.Log("Received Hello from " + address.ToString());

        byte[] bytes = GenerateByteMessage("");
        stream.Write(bytes, 0, bytes.Length);

        string data = GetMessageFromNetworkStream(stream);

        string[] chunks = data.Split('|');
        VideoSynchroniser.Instance().CurrentTick = int.Parse(chunks[1]);

        client.Close();
    }
Пример #4
0
    public void SendKeepAlive()
    {
        try
        {
            Monitor.PulseAll(LockObj);
            Monitor.Exit(LockObj);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        foreach (IPAddress address in ActiveAddresses)
        {
            Debug.Log("Sending Keepalive to IP " + address.ToString());
            TcpClient     client = new TcpClient(address.ToString(), Port);
            NetworkStream stream = client.GetStream();
            String        data   = string.Format("{0}|{1}", (int)CMD.KeepAlive, VideoSynchroniser.Instance().CurrentTick);
            byte[]        bytes  = GenerateByteMessage(data);
            stream.Write(bytes, 0, bytes.Length);

            client.Close();
        }
    }
Пример #5
0
    private void Listen()
    {
        this.server = new TcpListener(Address, Port);
        this.server.Start();
        Debug.Log("Server Started");
        while (true)
        {
            Debug.Log("Waiting for connection");
            TcpClient client = server.AcceptTcpClient();
            Debug.Log("Connected!");
            IPAddress clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            if (!AllowedAddresses.Contains(clientAddress))
            {
                continue;
            }
            NetworkStream stream = client.GetStream();

            String data = GetMessageFromNetworkStream(stream);
            Debug.Log(data);
            CMD cmd = (CMD)(data[0] - '0');
            Debug.Log("dddddddddd " + cmd);
            switch (cmd)
            {
            case CMD.Hello:
                Debug.Log("Received Hello message from " + clientAddress.ToString());
                data = string.Format("{0}|{1}", (int)CMD.Hello, VideoSynchroniser.Instance().CurrentTick);
                byte[] bytes = GenerateByteMessage(data);
                stream.Write(bytes, 0, bytes.Length);

                if (IsAdjusting)
                {
                    SecondaryActiveAddresses.Add(clientAddress);
                }
                else
                {
                    ActiveAddresses.Add(clientAddress);
                }
                break;

            case CMD.KeepAlive:
                Debug.Log("Received KeepAlive message from " + clientAddress.ToString());
                if (IsAdjusting)
                {
                    if (!SecondaryActiveAddresses.Contains(clientAddress))
                    {
                        Debug.Log("Adding in SecondaryActiveAddresses");
                        SecondaryActiveAddresses.Add(clientAddress);
                    }
                }
                else
                {
                    if (!ActiveAddresses.Contains(clientAddress))
                    {
                        Debug.Log("Adding in ActiveAddresses");
                        ActiveAddresses.Add(clientAddress);
                    }
                }
                break;

            case CMD.AdjustFrame:
                Debug.Log("Received AdjustFrame message from: " + clientAddress.ToString());

                string[] chunks = data.Split('|');
                Received.Add(clientAddress, int.Parse(chunks[1]));
                while (MyFrame == -1)
                {
                    continue;
                }
                data  = MyFrame.ToString();
                bytes = System.Text.Encoding.ASCII.GetBytes(data);
                stream.Write(bytes, 0, bytes.Length);
                client.Close();
                AdjustedCnt++;
                break;
            }
            client.Close();
        }
    }