public void SendToClient(byte[] data, IPEndPoint destinationIP)
    {
        UdpServerConnection udpServerConnection = udpConnection as UdpServerConnection;

        if (udpServerConnection != null)
        {
            udpServerConnection.Send(data, destinationIP);
        }
    }
    public void Broadcast(byte[] data)
    {
        UdpServerConnection udpServerConnection = udpConnection as UdpServerConnection;

        if (udpServerConnection != null)
        {
            using (var iterator = UdpConnectionManager.Instance.ClientsIPs.GetEnumerator())
                while (iterator.MoveNext())
                {
                    udpServerConnection.Send(data, iterator.Current);
                }
        }
    }
예제 #3
0
        public IConnection ReadData()
        {
            var endPoint = new IPEndPoint(IPAddress.Any, 0);

            byte[] data = Socket.Receive(ref endPoint);
            UdpServerConnection connection;

            if (Connections.TryGetValue(endPoint, out connection))
            {
                connection.HandleData(data);
                return(null);
            }
            else
            {
                connection = new UdpServerConnection(this, new IPEndPoint(endPoint.Address, endPoint.Port));
                Connections[new IPEndPoint(endPoint.Address, endPoint.Port)] = connection;
                // Skip data, detection packet
                //connection.HandleData(data);
                return(connection);
            }
        }
예제 #4
0
        public static List <IConnection> CreateConnections(string uniqueId, string channelName, string url)
        {
            var result = new List <IConnection>();

            switch (channelName)
            {
            case "SessionUpdate":
                result.Add(new WsConnection(uniqueId, channelName)
                {
                    Url = url
                });
                result.Add(new RestConnection()
                {
                    Url = url, UniqueId = uniqueId, ChannelName = channelName
                });
                break;

            case "ParameterUpdate":
                result.Add(new WsConnection(uniqueId, channelName)
                {
                    Url = url
                });
                result.Add(new RestConnection()
                {
                    Url = url, UniqueId = uniqueId, ChannelName = channelName
                });
                break;

            case "CommandsExecution":
            case "Master":
#if _UDP
                _serverConnection = new UdpServerConnection()
                {
                    Url = url, ChannelName = channelName, UniqueId = uniqueId
                };

                result.Add(_serverConnection);
#endif
                result.Add(new WsConnection(uniqueId, channelName)
                {
                    Url = url
                });
                result.Add(new RestConnection()
                {
                    Url = url, UniqueId = uniqueId, ChannelName = channelName
                });
                break;

            case "ExecuteCommander":
            case "Slave":
#if _UDP
                if (_serverConnection != null)
                {
                    result.Add(new UdpServerProxyConnection(_serverConnection)
                    {
                        Url = url, UniqueId = uniqueId, ChannelName = channelName
                    });
                }
#endif
                result.Add(new WsConnection(uniqueId, channelName)
                {
                    Url = url
                });
                result.Add(new RestConnection()
                {
                    Url = url, UniqueId = uniqueId, ChannelName = channelName
                });
                break;

            default:
                MsgLogger.WriteError($"ConnectionFactory - CreateConnections", $"cannot create instance, channelName = {channelName}");
                break;
            }

            return(result);
        }
예제 #5
0
        public MainWindow()
        {
            InitializeComponent();

            int max_fps = 40;

            var tcpClient        = new TcpServerConnection();
            var udpClient        = new UdpServerConnection();
            var canvas           = new CanvasControlVM();
            var drawingTools     = new DrawingToolsVM(this, canvas, max_fps);
            var serverConnection = new ServerConnectionVM(tcpClient);

            KeyDown += (s, e) =>
            {
                if (Keyboard.Modifiers != ModifierKeys.Control)
                {
                    return;
                }

                if (e.Key == Key.X)
                {
                    canvas.Clear();
                }
                if (e.Key == Key.S)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog {
                        FileName = "unnamed.png"
                    };
                    if (saveFileDialog.ShowDialog() == true)
                    {
                        canvas.Picture.Save(saveFileDialog.FileName, ImageFormat.Png);
                    }
                }
                if (e.Key == Key.T)
                {
                    drawingTools.OpenToolWindow();
                }
            };

            serverConnection.PropertyChanged += (s, e) =>
            {
                if (serverConnection.IsServerConnected)
                {
                    udpClient.Address = serverConnection.Address;
                }
                else
                {
                    udpClient.Stop();
                }
            };
            serverConnection.RoomChanged += () => udpClient.Start(serverConnection.RoomID);
            drawingTools.LineDrawn       += () =>
            {
                canvas.DrawLine(drawingTools.DrawnLine);
                udpClient.SendLine(drawingTools.DrawnLine);
            };

            udpClient.LineReceived += () => Dispatcher.Invoke(() => canvas.DrawLine(udpClient.ReceivedLine.Retrieve(x => x)));
            udpClient.Disconnected += () => Dispatcher.Invoke(() => tcpClient.Disconnect());

            var visSwitch = new VisibilitySwitchView();

            visSwitch.DisplayedContent.Content = serverConnection.View;

            WPFContent.Content         = drawingTools.View;
            ConnectionControls.Content = visSwitch;
        }