private void BtnStart_Click(object sender, RoutedEventArgs e) { IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Parse("192.168.178.26"), 1920); CommunicationProvider provider = new CommunicationProvider(ProtocolType.Tcp); provider.Socket.Connect(clientEndPoint); DeepSeaServer = new DeepSeaServer(provider); if (!DeepSeaServer.Send(new ConnectionRequestPacket() { options = 0 })) { provider.Socket.Close(); return; } receivedBytes = provider.Socket.Receive(buffer); if (DeepSea.GetPacketType(buffer) != PacketType.ClientDefinition) { provider.Socket.Close(); return; } ClientDefinitionPacket clientDefinitionPacket = DeepSea.GetPacket <ClientDefinitionPacket>(buffer, receivedBytes); ClientScreenSize.X = clientDefinitionPacket.width; ClientScreenSize.Y = clientDefinitionPacket.height; Point targetDefinition = ClientScreenSize; if (!DeepSeaServer.Send(new TargetDefinitionPacket() { width = Convert.ToUInt16(targetDefinition.X), height = Convert.ToUInt16(targetDefinition.Y) })) { provider.Socket.Close(); return; } receivedBytes = provider.Socket.Receive(buffer); if (DeepSea.GetPacketType(buffer) != PacketType.StreamRequest) { provider.Socket.Close(); return; } //TODO: Start streaming the video Graphics graphics = Graphics.FromImage(imageBuffer); Socket streamSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); StreamPacket streamPacket = new StreamPacket(); streamPacket.imageBytes = buffer; MemoryStream memoryStream = new MemoryStream(buffer); byte packetTypeByte = Convert.ToByte(PacketType.Stream); IPEndPoint clientUdpEndPoint = new IPEndPoint(clientEndPoint.Address, 1080); while (true) { graphics.CopyFromScreen(0, 0, 0, 0, imageBuffer.Size); imageBuffer.Save(memoryStream, ImageFormat.Jpeg); System.Buffer.BlockCopy(buffer, 0, buffer, 1, buffer.Length - 1); buffer[0] = packetTypeByte; streamSocket.SendTo(buffer, clientUdpEndPoint); } }
State HandleConnection(ref Socket serverSocket) { switch (currentState) { case State.Initializing: UpdateStatusText("Initializing"); IWindowManager windowManager = GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); windowManager.DefaultDisplay.GetRealSize(ScreenSize); return(State.WaitingForHost); case State.WaitingForHost: UpdateStatusText("Waiting for host"); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1920); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(ipEndPoint); serverSocket.Listen(100); serverSocket = serverSocket.Accept(); return(State.WaitingForConnectionRequest); case State.WaitingForConnectionRequest: UpdateStatusText("Waiting for connection request"); receivedBytes = serverSocket.Receive(buffer); if (DeepSea.GetPacketType(buffer) == PacketType.ConnectionRequest) { return(State.SendingClientDefinition); } serverSocket.Close(); return(State.WaitingForHost); case State.SendingClientDefinition: UpdateStatusText("Sending client information"); if (DeepSeaClient.Send(new ClientDefinitionPacket() { width = Convert.ToUInt16(ScreenSize.X), height = Convert.ToUInt16(ScreenSize.Y) })) { return(State.WaitingForTargetDefinition); } serverSocket.Close(); return(State.WaitingForHost); case State.WaitingForTargetDefinition: UpdateStatusText("Waiting for target definition"); receivedBytes = serverSocket.Receive(buffer); if (DeepSea.GetPacketType(buffer) == PacketType.TargetDefinition) { TargetDefinitionPacket targetDefinitionPacket = DeepSea.GetPacket <TargetDefinitionPacket>(buffer, receivedBytes); TargetScreenSize.X = targetDefinitionPacket.width; TargetScreenSize.Y = targetDefinitionPacket.height; return(State.SendingStreamRequest); } serverSocket.Close(); return(State.WaitingForHost); case State.SendingStreamRequest: UpdateStatusText("Sending stream request"); if (DeepSeaClient.Send(new StreamRequestPacket() { options = 0 })) { return(State.WaitingForStream); } serverSocket.Close(); return(State.WaitingForHost); case State.WaitingForStream: UpdateStatusText("Waiting for stream"); receivedBytes = serverSocket.Receive(buffer); if (DeepSea.GetPacketType(buffer) == PacketType.Stream) { return(State.WaitingForStream); } serverSocket.Close(); return(State.WaitingForHost); default: return(State.WaitingForHost); } }