-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleksandr
committed
Jun 11, 2015
1 parent
68be9cb
commit 78f7ce5
Showing
14 changed files
with
742 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>Client</RootNamespace> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v120</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v120</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(BOOST_ROOT);</IncludePath> | ||
<LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(BOOST_ROOT)\lib32-msvc-12.0;</LibraryPath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(BOOST_ROOT);</IncludePath> | ||
<LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(BOOST_ROOT)\lib32-msvc-12.0;</LibraryPath> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="client.cpp" /> | ||
<ClCompile Include="..\Common\logger.cpp" /> | ||
<ClCompile Include="main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="client.h" /> | ||
<ClInclude Include="..\Common\logger.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Filter Include="Source Files"> | ||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="Header Files"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | ||
</Filter> | ||
<Filter Include="Resource Files"> | ||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
</Filter> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="main.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="client.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="..\Common\logger.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="client.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
<ClInclude Include="..\Common\logger.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <string> | ||
#include <iostream> | ||
|
||
#include <boost/filesystem/path.hpp> | ||
#include <boost/log/trivial.hpp> | ||
|
||
#include "client.h" | ||
|
||
|
||
Client::Client(IoService& t_ioService, TcpResolverIterator t_endpointIterator, | ||
std::string const& t_path) | ||
: m_ioService(t_ioService), m_socket(t_ioService), | ||
m_endpointIterator(t_endpointIterator), m_path(t_path) | ||
{ | ||
doConnect(); | ||
openFile(m_path); | ||
} | ||
|
||
|
||
void Client::openFile(std::string const& t_path) | ||
{ | ||
m_sourceFile.open(t_path, std::ios_base::binary | std::ios_base::ate); | ||
if (m_sourceFile.fail()) | ||
throw std::fstream::failure("Failed while opening file " + t_path); | ||
|
||
m_sourceFile.seekg(0, m_sourceFile.end); | ||
auto fileSize = m_sourceFile.tellg(); | ||
m_sourceFile.seekg(0, m_sourceFile.beg); | ||
|
||
std::ostream requestStream(&m_request); | ||
boost::filesystem::path p(t_path); | ||
requestStream << p.filename().string() << "\n" << fileSize << "\n\n"; | ||
BOOST_LOG_TRIVIAL(trace) << "Request size: " << m_request.size(); | ||
} | ||
|
||
|
||
void Client::doConnect() | ||
{ | ||
boost::asio::async_connect(m_socket, m_endpointIterator, | ||
[this](boost::system::error_code ec, TcpResolverIterator) | ||
{ | ||
if (!ec) { | ||
writeBuffer(m_request); | ||
} else { | ||
std::cout << "Coudn't connect to host. Please run server " | ||
"or check network connection.\n"; | ||
BOOST_LOG_TRIVIAL(error) << "Error: " << ec.message(); | ||
} | ||
}); | ||
} | ||
|
||
|
||
void Client::doWriteFile(const boost::system::error_code& t_ec) | ||
{ | ||
if (!t_ec) { | ||
if (m_sourceFile) { | ||
m_sourceFile.read(m_buf.data(), m_buf.size()); | ||
if (m_sourceFile.fail() && !m_sourceFile.eof()) { | ||
auto msg = "Failed while reading file"; | ||
BOOST_LOG_TRIVIAL(error) << msg; | ||
throw std::fstream::failure(msg); | ||
} | ||
std::stringstream ss; | ||
ss << "Send " << m_sourceFile.gcount() << " bytes, total: " | ||
<< m_sourceFile.tellg() << " bytes"; | ||
BOOST_LOG_TRIVIAL(trace) << ss.str(); | ||
std::cout << ss.str() << std::endl; | ||
|
||
auto buf = boost::asio::buffer(m_buf.data(), static_cast<size_t>(m_sourceFile.gcount())); | ||
writeBuffer(buf); | ||
} | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Error: " << t_ec.message(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <fstream> | ||
|
||
#include <boost/asio.hpp> | ||
|
||
|
||
class Client | ||
{ | ||
public: | ||
using IoService = boost::asio::io_service; | ||
using TcpResolver = boost::asio::ip::tcp::resolver; | ||
using TcpResolverIterator = TcpResolver::iterator; | ||
using TcpSocket = boost::asio::ip::tcp::socket; | ||
|
||
Client(IoService& t_ioService, TcpResolverIterator t_endpointIterator, | ||
std::string const& t_path); | ||
|
||
private: | ||
void openFile(std::string const& t_path); | ||
void doConnect(); | ||
void doWriteFile(const boost::system::error_code& t_ec); | ||
template<class Buffer> | ||
void writeBuffer(Buffer& t_buffer); | ||
|
||
|
||
TcpResolver m_ioService; | ||
TcpSocket m_socket; | ||
TcpResolverIterator m_endpointIterator; | ||
enum { MessageSize = 1024 }; | ||
std::array<char, MessageSize> m_buf; | ||
boost::asio::streambuf m_request; | ||
std::ifstream m_sourceFile; | ||
std::string m_path; | ||
}; | ||
|
||
|
||
template<class Buffer> | ||
void Client::writeBuffer(Buffer& t_buffer) | ||
{ | ||
boost::asio::async_write(m_socket, | ||
t_buffer, | ||
[this](boost::system::error_code ec, size_t /*length*/) | ||
{ | ||
doWriteFile(ec); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
|
||
#include <boost/asio/io_service.hpp> | ||
|
||
#include "client.h" | ||
#include "../Common/logger.h" | ||
|
||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
if (argc != 4) { | ||
std::cerr << "Usage: client <address> <port> <filePath>\n"; | ||
return 1; | ||
} | ||
|
||
Logger::instance().setOptions("client_%3N.log", 1 * 1024 * 1024, 10 * 1024 * 1024); | ||
|
||
auto address = argv[1]; | ||
auto port = argv[2]; | ||
auto filePath = argv[3]; | ||
|
||
try { | ||
boost::asio::io_service ioService; | ||
|
||
boost::asio::ip::tcp::resolver resolver(ioService); | ||
auto endpointIterator = resolver.resolve({ address, port }); | ||
Client client(ioService, endpointIterator, filePath); | ||
|
||
ioService.run(); | ||
} catch (std::fstream::failure& e) { | ||
std::cerr << e.what() << "\n"; | ||
} catch (std::exception& e) { | ||
std::cerr << "Exception: " << e.what() << "\n"; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <boost/log/utility/setup.hpp> | ||
#include <boost/log/utility/setup/file.hpp> | ||
|
||
#include "logger.h" | ||
|
||
|
||
Logger& Logger::instance() | ||
{ | ||
static Logger logger; | ||
return logger; | ||
} | ||
|
||
|
||
void Logger::setOptions(std::string const& t_fileName, unsigned t_rotationSize, | ||
unsigned t_maxSize) | ||
{ | ||
boost::log::add_file_log( | ||
boost::log::keywords::file_name = t_fileName, | ||
boost::log::keywords::rotation_size = t_rotationSize, | ||
boost::log::keywords::max_size = t_maxSize, | ||
boost::log::keywords::time_based_rotation | ||
= boost::log::sinks::file::rotation_at_time_point(0, 0, 0), | ||
boost::log::keywords::format = "[%TimeStamp%]: %Message%", | ||
boost::log::keywords::auto_flush = true | ||
); | ||
|
||
boost::log::add_common_attributes(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
|
||
class Logger | ||
{ | ||
Logger() {} | ||
public: | ||
static Logger& instance(); | ||
static void setOptions(std::string const& t_fileName, unsigned t_rotationSize, | ||
unsigned t_maxSize); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 2013 | ||
VisualStudioVersion = 12.0.40418.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Client", "Client\Client.vcxproj", "{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}" | ||
EndProject | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Server", "Server\Server.vcxproj", "{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Win32 = Debug|Win32 | ||
Release|Win32 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Debug|Win32.ActiveCfg = Debug|Win32 | ||
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Debug|Win32.Build.0 = Debug|Win32 | ||
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Release|Win32.ActiveCfg = Release|Win32 | ||
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Release|Win32.Build.0 = Release|Win32 | ||
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Debug|Win32.ActiveCfg = Debug|Win32 | ||
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Debug|Win32.Build.0 = Debug|Win32 | ||
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Release|Win32.ActiveCfg = Release|Win32 | ||
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Release|Win32.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Requirements | ||
- MS Visual Studio 2015 with update 5 | ||
- boost 1.58.0 | ||
|
||
# Building for Windows | ||
- Install boost.asio | ||
- Add `BOOST_ROOT` with boost's directory path to environment variables | ||
- Open solution and build | ||
|
||
# Running | ||
- start server to accept files | ||
``` | ||
server <port> <workDirectory> | ||
``` | ||
- start client to transfer file | ||
``` | ||
client <address> <port> <filePath> | ||
``` | ||
|
Oops, something went wrong.