// Creates a proxy with interface of remote instance public void createSendChannel(string address) { EndpointAddress baseAddress = new EndpointAddress(address); WSHttpBinding binding = new WSHttpBinding(); factory = new ChannelFactory <IMessagePassing>(binding, address); channel = factory.CreateChannel(); }
// Uploads file to Receiver instance public bool postFile(string fileName, string storagePath, long blockSize, string toAddress, string toPath) { if (toAddress == lastUrl) { return(postFileTry(fileName, storagePath, blockSize, channel, toPath)); } else { close(); try { IMessagePassing newChannel = createNewSendChannel(toAddress); lastUrl = toAddress; return(postFileTry(fileName, storagePath, blockSize, newChannel, toPath)); } catch (Exception ex) { Console.WriteLine("\n Exception Raised!!"); Console.WriteLine("\n Port already in use"); Console.WriteLine(ex.Message); return(false); } } }
// Helper function which tries to send the file block through the specified channel private bool postFileTry(string fileName, string storagePath, long blockSize, IMessagePassing channel, string toPath) { FileStream fs = null; long bytesRemaining; try { string path = Path.Combine(storagePath, fileName); fs = File.OpenRead(path); bytesRemaining = fs.Length; channel.openFileForWrite(toPath, fileName); while (true) { long bytesToRead = Math.Min(blockSize, bytesRemaining); byte[] blk = new byte[bytesToRead]; long numBytesRead = fs.Read(blk, 0, (int)bytesToRead); bytesRemaining -= numBytesRead; channel.writeFileBlock(blk); if (bytesRemaining <= 0) { break; } } channel.closeFile(); fs.Close(); } catch (Exception ex) { lastError = ex.Message; return(false); } return(true); }