/// <summary> /// RPC服务提供者 /// </summary> /// <param name="serviceTypes">null 自动注册全部服务</param> /// <param name="port"></param> public ServiceProvider(Type[] serviceTypes, int port = 39654) { _serviceTypes = serviceTypes; _port = 39654; _RServer = new RServer(); _RServer.OnMsg += _RServer_OnMsg; _RServer.OnError += _RServer_OnError; }
public static void requestNewConnectionToOpenPort(string ipAddress, NewConnectionEstablished NewConnectionEstablishedCallback, ErrorEncounted ErrorEncountedCallback) { RServer tS = new RServer(ipAddress, COMMUNICATION_PORT.ToString()); tS.setOnNewDeviceConnectedCallback( new RServer.NewDeviceConnected( (RServer _sender, CommunicationNetworkStream _stream) => { _stream.WriteString("NEW_CONNECTION"); } ) ); tS.setOnDataReceivedCallback( new RServer.DataReceived( (RServer _sender, CommunicationNetworkStream stream, String data) => { if (data.Contains("WAIT_FOR_CONNECTION")) { ErrorEncountedCallback(_sender, stream, data); } // Error En count Callback} //SPLIT DATA TO GET PORT & AUTH ID string[] pp = data.Split(':'); if (pp[0].Contains("MOVE_TO_PORT")) { //INSTRUCT TO MOVE TO NEW PORT string port = pp[1]; string authId = pp[2]; NetworkServer server = new NetworkServer(); server.authId = authId; server.nickName = ipToNickName(_sender.ServerIP); server.serverPort = new RServer(_sender.ServerIP, port); server.serverPort.setOnNewDeviceConnectedCallback( new RServer.NewDeviceConnected( (RServer __sender, CommunicationNetworkStream _stream) => { //SENT THE AUTH ID OF THE CLIENT _stream.WriteString(((NetworkServer)serverList[$"{__sender.ServerIP}:{__sender.ServerPort.ToString()}"]).authId); NetworkServer serverObj = (NetworkServer)serverList[$"{__sender.ServerIP}:{__sender.ServerPort.ToString()}"]; serverList.Remove($"{__sender.ServerIP}:{__sender.ServerPort.ToString()}"); //REMOVE THE SERVER OBJ FROM LIST NewConnectionEstablishedCallback(serverObj, _stream); //GIVE THE CALLBACK } ) ); serverList.Add($"{_sender.ServerIP}:{port}", server); server.serverPort.Connect(true); } _sender.removeAllNewDeviceConnectedCallback(); _sender.removeAllDataReceivedCallback(); _sender.StopListening(); //CLOSE THE CONNECTION TO OPEN PORT 5660 } )); tS.Connect(true); }
/// <summary> /// Adds a new router with priority <paramref name="priority"/> to the given server. /// </summary> /// <param name="server">The server to add the router to.</param> /// <param name="priority">The priority of the router.</param> /// <returns>The new router added to the server.</returns> public static Router AddRouter(this RServer server, int priority = 0) { var router = new Router() { Priority = priority, }; server.AddHandler(router); return(router); }
public static void closeConnection(RServer connection) { if (connection.IsRunning) { connection.Stream.WriteString(ExchangeType.CLOSE_CONNECTION_REQUEST.ToString()); connection.removeAllNewDeviceConnectedCallback(); connection.removeAllDataReceivedCallback(); connection.StopListening(); } }
/// <summary> /// RPC服务提供者 /// </summary> /// <param name="serviceTypes">null 自动注册全部服务</param> /// <param name="port"></param> public ServiceProvider(Type[] serviceTypes, int port = 39654) { _serviceTypes = serviceTypes; _port = 39654; _RServer = new RServer(port: _port); _RServer.OnMsg += _RServer_OnMsg; _RServer.OnError += _RServer_OnError; ExceptionCollector.OnErr += ExceptionCollector_OnErr; }
public static string getOthIdOfClient(RServer client) { foreach (DictionaryEntry item in ConnectedClients) { NetworkClient obj = ((NetworkClient)item.Value); if (obj.clientPort.ServerIP == client.ServerIP && obj.clientPort.ServerPort == client.ServerPort) { return(obj.othId); } } return(null); }
private void newConnectionEstablishedCallback(NetworkServer serverObj, CommunicationNetworkStream stream) { RServer connection = serverObj.serverPort; double _readOut = 0; //SET UP TIMER Stopwatch totalTimer = new Stopwatch(); //TOTAL TIME totalTimer.Start(); //START TIMER Stopwatch fileTimer = new Stopwatch(); //FILE TIMER fileTimer.Start(); //START TIMER foreach (TransferDetails info in fileDetails) { double calling = 1; fileTimer.Restart(); //START TIMER FileTransfer ft = new FileTransfer(info.SourcePath, info.DestinationPath, connection.Stream, new FileTransfer.FileTransferingProgress((string fileName, double progressRatio) => { try { info.progressRatio = progressRatio; ActualSizeTransfered = _readOut + info.FileLength * progressRatio; totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC if ((calling % skeepCalls) == 0) { TransferProgressCallback(this, info, progressRatio, fileTimer); //PASS THE CALLBACK } calling++; } catch (Exception) { } }), new FileTransfer.FileTransferingCompleted((string fileName) => { try { _readOut += info.FileLength; ActualSizeTransfered = _readOut; fileTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC TransferProgressCallback(this, info, 1.0d, fileTimer); //FORCED CALLBACK TransferCompletedCallback(this, info, fileTimer); //PASS THE CALLBACK } catch (Exception) { } })) .Initialize(); FileTransferingStartedCallback(this, info); //TRANSFER STARTED CALLBACK ft.StartTransfering(true, MyGlobal.BUFFER_SIZE); } totalTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC AllFileTransferingCompletedCallback(this, connection); //ALL FILES TRANSFERED NetworkServer.closeConnection(connection); //CLOSE CONNECTION }
/// <summary> /// RPC服务提供者 /// </summary> /// <param name="serviceTypes">null 注册全部rpc服务</param> /// <param name="port"></param> /// <param name="bufferSize"></param> /// <param name="count"></param> public ServiceProvider(Type[] serviceTypes, int port = 39654, int bufferSize = 10 * 1024, int count = 10000) { _serviceTypes = serviceTypes; _port = port; _noticeCollection = new NoticeCollection(); _RServer = new RServer(_port, bufferSize, count); _RServer.OnMsg += _RServer_OnMsgAsync; _RServer.OnError += _RServer_OnError; ExceptionCollector.OnErr += ExceptionCollector_OnErr; }
private void closeConnection(RServer server) { try { ConnectedClients.Remove(getOthIdOfClient(server)); //UNREGISTER CLIENT unRegisterPort(server.ServerPort); //UNREGISTER CLIENT PORT try { server.DetachClient(); } catch (Exception) { } try { server.StopListening(); } catch (Exception) { } } catch (Exception) { } }
public static void Main(string[] args) { List <string> hosts = File.Exists(HostsFile) ? File.ReadAllLines(HostsFile).ToList() : new List <string>(); var server = new RServer(new RServerOptions().WithHosts(hosts)); server.AddLocalFiles() .WithFileProvider(new LocalFileProvider("www")) .WithMimeService(new FileMimeService("mime.map")); server.Start(); while (Console.ReadLine() != ".exit") { ; } }
public Fivem(string Code) { EpHTTP http = new EpHTTP(); this.RServer = http.GetServer(Code); if (this.RServer != null) { this.RServer.isOnline = true; } else { this.RServer = new RServer() { isOnline = false }; } }
private void InitiateOpenServer() { fileManagement = new FileManagement(); fileManagement.read(); RServer openServer = new RServer(fileManagement.fileData.myIpAddress, OPEN_COMMUNICATION_PORT.ToString()); openServer.setOnNewDeviceConnectedCallback( new RServer.NewDeviceConnected( (RServer _client, CommunicationNetworkStream _stream) => { //CHECK IF THIS CLIENT IS ACTUALLY WANTS TO HAVE A NEW CONNECTION string __data = _stream.ReadString(true); string[] __pp = __data.Split(':'); string requestCode = __pp[0]; switch (requestCode) { case "NEW_CONNECTION": allcoateNewConnection(_client, _stream); break; case "NEW_OR_REDIRECT_CONNECTION": //NEW_OR_REDIRECT_CONNECTION:othId if (ConnectedClients.ContainsKey(__pp[1])) { //THE CLIENT HAS AN ALREADY ALLOCATED PORT //INSTRUCT TO MOVE NetworkClient cL = ((NetworkClient)ConnectedClients[__pp[1]]); _stream.WriteString($"MOVE_TO_PORT:{cL.clientPort.ServerPort}:{cL.othId}"); Debug.WriteLine($"NEW_OR_REDIRECT_CONNECTION: redirected to port.{cL.clientPort.ServerPort}:{cL.othId}"); } else { allcoateNewConnection(_client, _stream); MessageBox.Show("NEW_OR_REDIRECT_CONNECTION: new port allocated"); } break; } } ) ); openServer.Initialize(true); }
public static void InitServer(TestContext context) { server = new RServer(new RServerOptions() .WithHost(Host) .WithHost(HostHttps)); server.AddLocalFiles() .WithFileProvider(new LocalFileProvider("www")) .WithMimeService(new DictionaryMimeService() { { ".txt", "text/plain" }, { ".html", "text/html" }, { ".pdf", "application/pdf" } }); server.Start(); client = new HttpClient(); client.BaseAddress = new Uri(Host); wc = new WebClient(); }
public Fivem(string ipport) { EpHTTP http = new EpHTTP(ipport); Info Info = http.GetInfo(); List <User> Players = http.GetPlayers(); RServer = new RServer() { isOnline = false }; if (Info != null && Players != null) { RServer = new RServer() { isOnline = true, Players = Players, Info = Info }; } }
private void newConnectionEstablishedCallback(NetworkServer serverObj, CommunicationNetworkStream __stream) { RServer connection = serverObj.serverPort; double _readOut = 0; int noOfFilesReceived = 0; //lock(_readOut){ } //SET UP TIMER Stopwatch totalTimer = new Stopwatch(); //TOTAL TIME totalTimer.Start(); //START TIMER Stopwatch fileTimer = new Stopwatch(); //FILE TIMER fileTimer.Start(); //START TIMER //HANDLE THE DATA RECEIVED RESPONSES connection.setOnDataReceivedCallback( new RServer.DataReceived((RServer sender, CommunicationNetworkStream stream, string data) => { switch (CommandRecognization.Recognize(data)) { case ExchangeType.FILE_TRANSFER: double calling = 1; fileTimer.Restart(); //START TIMER TransferDetails CURRENT_FILE = fileDetails[noOfFilesReceived]; //CURRENT FILE FileReceiver fR = new FileReceiver(stream).Initialize(); //TRANSFER OBJ //PROGRESS CALLBACK fR.FileReceivingProgressCallback = new FileReceiver.FileReceivingProgress( (string FileName, double progressRatio) => { try { CURRENT_FILE.progressRatio = progressRatio; ActualSizeTransfered = _readOut + CURRENT_FILE.FileLength * progressRatio; totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { if ((calling % skeepCalls) == 0) { DownloadingProgressCallback(this, CURRENT_FILE, progressRatio, fileTimer); //PASS THE CALLBACK } } catch (Exception) { } calling++; } catch (Exception) { } }); //COMPLETION CALLBACK fR.FileReceivingCompletedCallback = new FileReceiver.FileReceivingCompleted( (string FileName) => { //DOWNLOAD COMPLETED try { _readOut += CURRENT_FILE.FileLength; ActualSizeTransfered = _readOut; noOfFilesReceived++; fileTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { DownloadingProgressCallback(this, CURRENT_FILE, 1.0d, fileTimer); //FORCED CALLBACK } catch (Exception) { } try { DownloadingCompletedCallback(this, CURRENT_FILE, fileTimer); //PASS THE CALLBACK } catch (Exception) { } } catch (Exception) { } }); try { //NEW RECEIVING IS STARTED FileDownloadingStartedCallback(this, CURRENT_FILE); //RECEIVING STARTED CALLBACK } catch (Exception) { } fR.StartReceiving(true); //START RECEIVING if (fileDetails.Count <= noOfFilesReceived) { //ALL DOWNLOADS COMPLETED totalTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { AllFileDownloadingCompletedCallback(this, sender); //ALL FILES RECEIVED } catch (Exception) { } NetworkServer.closeConnection(connection); //CLOSE CONNECTION } break; default: break; } })); //MADE ALL REQUESTS foreach (TransferDetails info in fileDetails) { new FileReceiverRequest(info.SourcePath, info.DestinationPath, connection.Stream, MyGlobal.BUFFER_SIZE).Request(); } }
private void allcoateNewConnection(RServer openServer, CommunicationNetworkStream _stream) { ChatBoxForm chatWindow = null;; //CHAT WINDOW //CHECK IF THEIR IS PORT LEFT TO ALLOCATE if (ConnectedClients.Count < ALLOCABLE_PORTS.Length) { NetworkClient client = new NetworkClient(); client.othId = Guid.NewGuid().ToString(); //ALLOCATE ID //START THE SERVER client.clientPort = new RServer(fileManagement.fileData.myIpAddress, allocateNewPort().ToString()); client.clientPort.setOnNewDeviceConnectedCallback( new RServer.NewDeviceConnected( (RServer sender, CommunicationNetworkStream stream) => { String cId = stream.ReadString(true); //WAIT TO GET ID Debug.WriteLine($"CONNECTION: {((NetworkClient)ConnectedClients[cId]).clientPort.ServerPort}:{cId}"); if (((NetworkClient)ConnectedClients[cId]).clientPort.ServerPort != sender.ServerPort) { sender.DetachClient(); } } ) ); client.clientPort.setOnDataReceivedCallback( new RServer.DataReceived( (RServer sender, CommunicationNetworkStream stream, string Data) => { switch (CommandRecognization.Recognize(Data)) { case ExchangeType.CLOSE_CONNECTION_REQUEST: closeConnection(sender); //CLOSE CONNECTION MessageBox.Show("CLOSE_CONNECTION_REQUEST"); break; case ExchangeType.FS_LIST_DRIVE_REQUEST: FileSystem.SentDriveList(stream); break; case ExchangeType.FS_LIST_DIRECTORY_REQUEST: FileSystem.SentDirectoryList(stream); break; case ExchangeType.FS_LIST_FILE_REQUEST: FileSystem.SentFileList(stream); break; case ExchangeType.FS_DELETE_REQUEST: FileSystem.ExecuteDelete(stream); break; case ExchangeType.FS_RENAME_REQUEST: FileSystem.ExecuteRename(stream); break; case ExchangeType.PROCESS_LIST_ALL_REQUEST: ProcessSystem.SentAllProcess(stream); break; case ExchangeType.PROCESS_KILL_REQUEST: ProcessSystem.ExecuteKillProcess(stream); break; case ExchangeType.PROCESS_START_REQUEST: ProcessSystem.ExecuteProcess(stream); break; case ExchangeType.FILE_TRANSFER: FileReceiver fr = new FileReceiver(stream).Initialize(); fr.StartReceiving(); break; case ExchangeType.FILE_TRANSFER_REQUEST: FileTransferRequest ft = new FileTransferRequest(stream).Initialize(); ft.StartTransfering(); Thread.Sleep(1000); break; case ExchangeType.NULL: //EXTENDED MODE int len = Data.IndexOf(':'); string command = Data; if (len > 0) { command = Data.Substring(0, len); } string arg = ""; if (len > 0) { arg = Data.Substring(len + 1); } switch (command) { case "CREATE_DIR": try { System.IO.Directory.CreateDirectory(arg); } catch (Exception) { } break; case "CHAT_START": chatWindow = new ChatBoxForm(); Invoke(new UpdateData(() => { chatWindow.client = sender; chatWindow.connectedIp = arg; chatWindow.Show(); })); break; case "CHAT_STOP": Invoke(new UpdateData(() => { if (chatWindow != null) { chatWindow.Close(); chatWindow.Dispose(); chatWindow = null; } })); closeConnection(sender); //CLOSE CONNECTION break; case "CHAT_MESSAGE": Invoke(new UpdateData(() => { if (chatWindow != null) { chatWindow.newMessage(arg); } })); break; default: break; } break; } } ) ); client.clientPort.Initialize(true); //TELL THE CLIENT TO MOVE TO A NEW PORT //INSTRUCT TO MOVE _stream.WriteString($"MOVE_TO_PORT:{client.clientPort.ServerPort}:{client.othId}"); Debug.WriteLine($"NEW_CONNECTION: new port allocated.{client.clientPort.ServerPort}:{client.othId}"); ConnectedClients.Add(client.othId, client); //REGISTER CLIENT } else { _stream.WriteString("WAIT_FOR_CONNECTION"); } //DISCONNECT THE CLIENT FORM THIS PORT openServer.DetachClient(); }