Add SignalR event handler for file transfer (for when WebRTC fails).

This commit is contained in:
Jared Goodwin 2020-05-29 17:41:29 -07:00
parent 8d38f62975
commit a352582311
4 changed files with 155 additions and 152 deletions

View File

@ -88,10 +88,6 @@ namespace Remotely.ScreenCast.Core.Services
{
fileStream.Close();
partialTransfers.Remove(messageId, out _);
if (EnvironmentHelper.IsWindows)
{
Process.Start("explorer.exe", baseDir);
}
}
}
catch (Exception ex)
@ -101,7 +97,6 @@ namespace Remotely.ScreenCast.Core.Services
finally
{
writeLock.Release();
if (endOfFile)
{
await Task.Run(ShowTransferComplete);

View File

@ -183,7 +183,15 @@ namespace Remotely.Server.Services
{
return RCDeviceHubContext.Clients.Client(ScreenCasterID).SendAsync("ClipboardTransfer", transferText, typeText, Context.ConnectionId);
}
public async Task SendFile(byte[] buffer, string fileName, string messageId, bool endOfFile, bool startOfFile)
{
await RCDeviceHubContext.Clients.Client(ScreenCasterID).SendAsync("ReceiveFile",
buffer,
fileName,
messageId,
endOfFile,
startOfFile);
}
public Task SendFrameReceived(int bytesReceived)
{
return RCDeviceHubContext.Clients.Client(ScreenCasterID).SendAsync("FrameReceived", bytesReceived, Context.ConnectionId);

File diff suppressed because one or more lines are too long

View File

@ -1,147 +1,147 @@
import { MainRc } from "./Main.js";
import {
CtrlAltDelDto,
KeyDownDto,
KeyPressDto,
KeyUpDto,
MouseDownDto,
MouseMoveDto,
MouseUpDto,
MouseWheelDto,
QualityChangeDto,
SelectScreenDto,
TapDto,
AutoQualityAdjustDto,
ToggleAudioDto,
ToggleBlockInputDto,
ClipboardTransferDto,
FileDto,
WindowsSessionsDto,
import { MainRc } from "./Main.js";
import {
CtrlAltDelDto,
KeyDownDto,
KeyPressDto,
KeyUpDto,
MouseDownDto,
MouseMoveDto,
MouseUpDto,
MouseWheelDto,
QualityChangeDto,
SelectScreenDto,
TapDto,
AutoQualityAdjustDto,
ToggleAudioDto,
ToggleBlockInputDto,
ClipboardTransferDto,
FileDto,
WindowsSessionsDto,
GenericDto
} from "./RtcDtos.js";
import { CreateGUID, When } from "../Utilities.js";
import { FileTransferProgress } from "./UI.js";
import { BinaryDtoType } from "../Enums/BinaryDtoType.js";
export class MessageSender {
GetWindowsSessions() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new WindowsSessionsDto()),
() => MainRc.RCHubConnection.GetWindowsSessions());
}
ChangeWindowsSession(sessionId: number) {
MainRc.RCHubConnection.ChangeWindowsSession(sessionId);
}
SendSelectScreen(displayName: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new SelectScreenDto(displayName)),
() => MainRc.RCHubConnection.SendSelectScreen(displayName));
}
SendMouseMove(percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseMoveDto(percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseMove(percentX, percentY));
}
SendMouseDown(button: number, percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseDownDto(button, percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseDown(button, percentX, percentY));
}
SendMouseUp(button: number, percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseUpDto(button, percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseUp(button, percentX, percentY));
}
SendTap(percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new TapDto(percentX, percentY)),
() => MainRc.RCHubConnection.SendTap(percentX, percentY));
}
SendMouseWheel(deltaX: number, deltaY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseWheelDto(deltaX, deltaY)),
() => MainRc.RCHubConnection.SendMouseWheel(deltaX, deltaY));
}
SendKeyDown(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyDownDto(key)),
() => MainRc.RCHubConnection.SendKeyDown(key));
}
SendKeyUp(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyUpDto(key)),
() => MainRc.RCHubConnection.SendKeyUp(key));
}
SendKeyPress(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyPressDto(key)),
() => MainRc.RCHubConnection.SendKeyPress(key));
}
SendSetKeyStatesUp() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new GenericDto(BinaryDtoType.SetKeyStatesUp)),
() => MainRc.RCHubConnection.SendSetKeyStatesUp());
}
SendCtrlAltDel() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new CtrlAltDelDto()),
() => MainRc.RCHubConnection.SendCtrlAltDel());
}
async SendFile(buffer: Uint8Array, fileName: string) {
var messageId = CreateGUID();
this.SendToAgent(() => MainRc.RtcSession.SendDto(new FileDto(null, fileName, messageId, false, true)),
() => MainRc.RCHubConnection.SendFile(null, fileName, messageId, false, true));
for (var i = 0; i < buffer.byteLength; i += 50_000) {
await this.SendToAgentAsync(async () => {
MainRc.RtcSession.SendDto(new FileDto(buffer.slice(i, i + 50_000), fileName, messageId, false, false));
await When(() => MainRc.RtcSession.DataChannel.bufferedAmount == 0, 10);
}, async () => {
await MainRc.RCHubConnection.SendFile(buffer.slice(i, i + 50_000), fileName, messageId, false, false);
});
if (i > 0) {
FileTransferProgress.value = i / buffer.byteLength;
}
}
this.SendToAgent(() => MainRc.RtcSession.SendDto(new FileDto(null, fileName, messageId, true, false)),
() => MainRc.RCHubConnection.SendFile(null, fileName, messageId, true, false));
}
SendQualityChange(qualityLevel: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new QualityChangeDto(qualityLevel)),
() => MainRc.RCHubConnection.SendQualityChange(qualityLevel));
}
SendAutoQualityAdjust(isOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new AutoQualityAdjustDto(isOn)),
() => MainRc.RCHubConnection.SendAutoQualityAdjust(isOn));
}
SendToggleAudio(toggleOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ToggleAudioDto(toggleOn)),
() => MainRc.RCHubConnection.SendToggleAudio(toggleOn));
};
SendToggleBlockInput(toggleOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ToggleBlockInputDto(toggleOn)),
() => MainRc.RCHubConnection.SendToggleBlockInput(toggleOn));
}
SendClipboardTransfer(text: string, typeText: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ClipboardTransferDto(text, typeText)),
() => MainRc.RCHubConnection.SendClipboardTransfer(text, typeText));
}
private IsWebRtcAvailable() {
return MainRc.RtcSession.DataChannel && MainRc.RtcSession.DataChannel.readyState == "open";
}
private SendToAgent(rtcSend: () => void, websocketSend: () => void) {
if (MainRc.RtcSession.DataChannel && MainRc.RtcSession.DataChannel.readyState == "open") {
rtcSend();
}
else if (MainRc.RCHubConnection.Connection.connectionStarted) {
websocketSend();
}
}
private async SendToAgentAsync(rtcSend: () => Promise<any>, websocketSend: () => Promise<any>) {
if (this.IsWebRtcAvailable()) {
await rtcSend();
}
else {
await websocketSend();
}
}
} from "./RtcDtos.js";
import { CreateGUID, When } from "../Utilities.js";
import { FileTransferProgress } from "./UI.js";
import { BinaryDtoType } from "../Enums/BinaryDtoType.js";
export class MessageSender {
GetWindowsSessions() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new WindowsSessionsDto()),
() => MainRc.RCHubConnection.GetWindowsSessions());
}
ChangeWindowsSession(sessionId: number) {
MainRc.RCHubConnection.ChangeWindowsSession(sessionId);
}
SendSelectScreen(displayName: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new SelectScreenDto(displayName)),
() => MainRc.RCHubConnection.SendSelectScreen(displayName));
}
SendMouseMove(percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseMoveDto(percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseMove(percentX, percentY));
}
SendMouseDown(button: number, percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseDownDto(button, percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseDown(button, percentX, percentY));
}
SendMouseUp(button: number, percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseUpDto(button, percentX, percentY)),
() => MainRc.RCHubConnection.SendMouseUp(button, percentX, percentY));
}
SendTap(percentX: number, percentY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new TapDto(percentX, percentY)),
() => MainRc.RCHubConnection.SendTap(percentX, percentY));
}
SendMouseWheel(deltaX: number, deltaY: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new MouseWheelDto(deltaX, deltaY)),
() => MainRc.RCHubConnection.SendMouseWheel(deltaX, deltaY));
}
SendKeyDown(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyDownDto(key)),
() => MainRc.RCHubConnection.SendKeyDown(key));
}
SendKeyUp(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyUpDto(key)),
() => MainRc.RCHubConnection.SendKeyUp(key));
}
SendKeyPress(key: string) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new KeyPressDto(key)),
() => MainRc.RCHubConnection.SendKeyPress(key));
}
SendSetKeyStatesUp() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new GenericDto(BinaryDtoType.SetKeyStatesUp)),
() => MainRc.RCHubConnection.SendSetKeyStatesUp());
}
SendCtrlAltDel() {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new CtrlAltDelDto()),
() => MainRc.RCHubConnection.SendCtrlAltDel());
}
async SendFile(buffer: Uint8Array, fileName: string) {
var messageId = CreateGUID();
this.SendToAgent(() => MainRc.RtcSession.SendDto(new FileDto(null, fileName, messageId, false, true)),
() => MainRc.RCHubConnection.SendFile(null, fileName, messageId, false, true));
for (var i = 0; i < buffer.byteLength; i += 50_000) {
await this.SendToAgentAsync(async () => {
MainRc.RtcSession.SendDto(new FileDto(buffer.slice(i, i + 50_000), fileName, messageId, false, false));
await When(() => MainRc.RtcSession.DataChannel.bufferedAmount == 0, 10);
}, async () => {
await MainRc.RCHubConnection.SendFile(buffer.slice(i, i + 50_000), fileName, messageId, false, false);
});
if (i > 0) {
FileTransferProgress.value = i / buffer.byteLength;
}
}
this.SendToAgent(() => MainRc.RtcSession.SendDto(new FileDto(null, fileName, messageId, true, false)),
() => MainRc.RCHubConnection.SendFile(null, fileName, messageId, true, false));
}
SendQualityChange(qualityLevel: number) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new QualityChangeDto(qualityLevel)),
() => MainRc.RCHubConnection.SendQualityChange(qualityLevel));
}
SendAutoQualityAdjust(isOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new AutoQualityAdjustDto(isOn)),
() => MainRc.RCHubConnection.SendAutoQualityAdjust(isOn));
}
SendToggleAudio(toggleOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ToggleAudioDto(toggleOn)),
() => MainRc.RCHubConnection.SendToggleAudio(toggleOn));
};
SendToggleBlockInput(toggleOn: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ToggleBlockInputDto(toggleOn)),
() => MainRc.RCHubConnection.SendToggleBlockInput(toggleOn));
}
SendClipboardTransfer(text: string, typeText: boolean) {
this.SendToAgent(() => MainRc.RtcSession.SendDto(new ClipboardTransferDto(text, typeText)),
() => MainRc.RCHubConnection.SendClipboardTransfer(text, typeText));
}
private IsWebRtcAvailable() {
return MainRc.RtcSession.DataChannel && MainRc.RtcSession.DataChannel.readyState == "open";
}
private SendToAgent(rtcSend: () => void, websocketSend: () => void) {
if (MainRc.RtcSession.DataChannel && MainRc.RtcSession.DataChannel.readyState == "open") {
rtcSend();
}
else if (MainRc.RCHubConnection.Connection.connectionStarted) {
websocketSend();
}
}
private async SendToAgentAsync(rtcSend: () => Promise<any>, websocketSend: () => Promise<any>) {
if (this.IsWebRtcAvailable()) {
await rtcSend();
}
else {
await websocketSend();
}
}
}