/// <summary> /// Send file /// </summary> /// <param name="file">path to file</param> /// <param name="receiveAction">Action identifier after receiving a file</param> public static void Send(string file, string receiveAction) { if (file == null || file == string.Empty || !File.Exists(file)) { throw new Exception("Non valid file"); } if (ports.Count >= (instance.portRangeTo - instance.portRangeFrom)) { throw new Exception("Not available port"); } int port = instance.portRangeFrom; while (ports.Contains(port) && port < instance.portRangeTo) { port++; } ports.Add(port); var prepareMsg = new FileSharePrepare() { crc = GetCRC(file), receiveAction = receiveAction, extension = Path.GetExtension(file), port = port }; //msg to clients, to be prepared if (NetworkClient.active) { NetworkManager.singleton.client.Send(fileSharePrepare, prepareMsg); NetworkManager.singleton.client.Send(getClientsSendFile, new NetConn() { port = port, file = file }); } else if (NetworkServer.active) { NetworkServer.SendToAll(fileSharePrepare, prepareMsg); foreach (var conn in NetworkServer.connections) { if (conn != null) { InvokeThread(conn.address, port, file); } } } }
private void ClientPrepare(NetworkMessage netMsg) { FileSharePrepare msg = netMsg.ReadMessage <FileSharePrepare>(); if (files.ContainsKey(msg.crc)) { if (onReceiveActions.ContainsKey(msg.receiveAction)) { onReceiveActions[msg.receiveAction](files[msg.crc].file); } } else { StartCoroutine(WaitForReceivedFile(msg.crc)); new Thread(() => Host(msg.extension, msg.receiveAction, msg.port)).Start(); } }
private void ServerPrepare(NetworkMessage netMsg) { FileSharePrepare msg = netMsg.ReadMessage <FileSharePrepare>(); NetworkServer.SendToAll(netMsg.msgType, msg); }