-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfile_server.cpp
More file actions
101 lines (84 loc) · 1.9 KB
/
file_server.cpp
File metadata and controls
101 lines (84 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// test_rpc_server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef _MSC_VER
#pragma warning(disable: 4251) // needs to have dll-interface
#pragma warning(disable: 4267) // conversion from 'size_t' to 'long', possible loss of data
#pragma warning(disable: 4244) // conversion from '__w64 const unsigned int' to 'unsigned int', possible loss of data
#pragma comment(lib, "Ws2_32.lib")
#endif
#include <stdio.h>
#include <nark/rpc/server.hpp>
#include <nark/inet/SocketStream.hpp>
#include <iostream>
using namespace std;
using namespace nark;
using namespace nark::rpc;
#include "../ifile.h"
BEGIN_RPC_IMP_INTERFACE(FileObjImp, FileObj)
FileObjImp()
{
fp = 0;
}
~FileObjImp()
{
if (fp)
{
fclose(fp);
}
}
rpc_ret_t open(const std::string& fname, const std::string& mode)
{
fp = fopen(fname.c_str(), mode.c_str());
if (0 == fp)
return errno;
return 0;
}
rpc_ret_t read(vector<char>* buffer, uint32_t length)
{
buffer->resize(length);
size_t nRead = fread(&*buffer->begin(), 1, length, fp);
buffer->resize(nRead);
return 0;
}
rpc_ret_t write(const vector<char>& buffer, uint32_t* length)
{
*length = fwrite(&*buffer.begin(), 1, buffer.size(), fp);
return 0;
}
rpc_ret_t close()
{
fclose(fp);
fp = 0;
return 0;
}
private:
FILE* fp;
END_RPC_IMP_INTERFACE()
int main0(int argc, char* argv[])
{
try {
SocketAcceptor acceptor("0.0.0.0:8001");
rpc_server<PortableDataInput, PortableDataOutput> server(&acceptor);
// FileObjImp will auto created by client call
RPC_SERVER_AUTO_CREATE(server, FileObjImp);
server.start();
}
catch (const std::exception& exp)
{
printf("exception: what=%s\n", exp.what());
}
return 0;
}
int main(int argc, char* argv[])
{
#if defined(_WIN32) || defined(_WIN64)
WSADATA information;
WSAStartup(MAKEWORD(2, 2), &information);
int ret = main0(argc, argv);
WSACleanup();
return ret;
#else
return main0(argc, argv);
#endif
}