66#include < chrono>
77#include < ctime>
88#include < mutex>
9-
10- #ifdef _WIN32
11- #include < windows.h>
12-
13- #endif
9+ #include < vector>
10+ #include < cstdarg>
1411
1512bool killswitch = false ;
1613std::string sockpath = " " ;
@@ -95,26 +92,33 @@ void serverOnDisconnect(void* data, OS::ClientId_t id) {
9592 );
9693}
9794
98- void serverOnMessage (void * data, OS::ClientId_t id, const std::vector<char >& msg) {
99- if (strcmp (msg.data (), longmessage) == 0 ) {
100- ClientData& cd = clientInfo.at (id);
101- uint64_t delta = duration_cast<nanoseconds>(high_resolution_clock::now () - cd.lastMessageTime ).count ();
102- cd.lastMessageTime = high_resolution_clock::now ();
103- cd.messageCount ++;
104- cd.messageTotalTime += delta;
105-
106-
107- if ((cd.messageCount % 1000 ) == 0 ) {
108- blog (" Server: Messages by %lld so far: %lld, Time: %lld ns, Average: %lld ns" ,
109- id, cd.messageCount , cd.messageTotalTime , uint64_t (double_t (cd.messageTotalTime ) / double_t (cd.messageCount )));
110- // cd.messageCount = cd.messageTotalTime = 0;
111- }
112- }
95+ IPC::Value callstuff (int64_t id, void * data, std::vector<IPC::Value> v) {
96+ auto & cd = clientInfo.at (id);
97+
98+ auto tp = std::chrono::high_resolution_clock::now ();
99+ cd.messageCount ++;
100+ cd.messageTotalTime += (tp - cd.lastMessageTime ).count ();
101+ cd.lastMessageTime = tp;
102+
103+ cd.replyCount ++;
104+ IPC::Value val;
105+ val.type = IPC::Type::UInt64;
106+ val.value .ui64 = v.at (0 ).value .ui64 ;
107+ auto tp2 = std::chrono::high_resolution_clock::now ();
108+ cd.replyTotalTime += (tp2 - tp).count ();
109+
110+ return val;
113111}
114112
115113int serverThread () {
116114 IPC::Server server;
117- server.SetMessageHandler (serverOnMessage, nullptr );
115+ IPC::Class cls (" Hello" );
116+ std::vector<IPC::Type> args; args.push_back (IPC::Type::UInt64);
117+ IPC::Function func (" Ping" , args);
118+ func.SetCallHandler (callstuff, nullptr );
119+ cls.RegisterFunction (std::make_shared<IPC::Function>(func));
120+ server.RegisterClass (cls);
121+
118122 server.SetConnectHandler (serverOnConnect, nullptr );
119123 server.SetDisconnectHandler (serverOnDisconnect, nullptr );
120124
@@ -132,33 +136,57 @@ int serverThread() {
132136 return 0 ;
133137}
134138
139+ struct ClientOnly {
140+ uint64_t counter = 0 ;
141+ uint64_t timedelta = 0 ;
142+ };
143+
144+ void incCtr (void * data, IPC::Value rval) {
145+ auto tp = std::chrono::high_resolution_clock::now ();
146+ ClientOnly* co = (ClientOnly*)data;
147+ co->counter ++;
148+ co->timedelta = (std::chrono::high_resolution_clock::now ().time_since_epoch ().count () - rval.value .ui64 );
149+ }
150+
135151int clientThread () {
136152 blog (" Client: Starting..." );
137153 IPC::Client client = { sockpath };
138154 blog (" Client: Started." );
139155
140156 std::vector<char > data (longmessage, longmessage+strlen (longmessage));
141157 auto bg = std::chrono::high_resolution_clock::now ();
142- client.RawWrite (data);
143- std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
158+ while (!client.Authenticate ()) {
159+ std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
160+ blog (" Client: Failed to authenticate, retrying... (Ctrl-C to quit)" );
161+ }
144162
145163 const size_t maxmsg = 100000 ;
146164 size_t idx = 0 ;
147165 size_t failidx = 0 ;
166+ ClientOnly co;
167+ std::vector<IPC::Value> args;
168+ args.push_back (IPC::Value (0ull ));
148169 while (idx < maxmsg) {
149- size_t wr = client. RawWrite (data );
150- if (wr == data. size ())
170+ args. at ( 0 ). value . ui64 = std::chrono::high_resolution_clock::now (). time_since_epoch (). count ( );
171+ if (client. Call ( " Hello " , " Ping " , args, incCtr, &co)) {
151172 idx++;
152- else {
173+ } else {
153174 failidx++;
154- blog (" Failed to send message %lld" , idx);
175+ if (failidx > 1000 )
176+ break ;
155177 }
156- if ((idx % 1000 ) == 0 )
157- blog (" Send! %lld" , idx);
158178 }
159179 size_t ns = (std::chrono::high_resolution_clock::now () - bg).count ();
160- blog (" Client: Sent %lld in %lld ns, average %lld ns." , maxmsg, ns, uint64_t (ns / double_t (maxmsg)));
161- blog (" Client: Failed sending %lld messages." , failidx);
180+
181+ while (co.counter < idx) {
182+ blog (" Client: Waiting for replies... (%lld out of %lld)" , co.counter , idx);
183+ std::this_thread::sleep_for (std::chrono::milliseconds (1000 ));
184+ }
185+
186+ blog (" Client: Sent %lld messages (%lld errors) in %lld ns, average %lld ns." ,
187+ maxmsg, failidx, ns, uint64_t (ns / double_t (maxmsg)));
188+ blog (" Client: Received %lld responses in %lld ns, average %lld ns." ,
189+ co.counter , co.timedelta , uint64_t (co.timedelta / double_t (co.counter )));
162190
163191 blog (" Client: Shutting down..." );
164192 std::cin.get ();
@@ -181,37 +209,6 @@ int main(int argc, char** argv) {
181209 std::thread worker;
182210 if (isServer) {
183211 worker = std::thread (serverThread);
184-
185- std::cin.get ();
186- #ifdef _WIN32
187- std::stringstream args;
188- args << ' "' << argv[0 ] << ' "' << " " ;
189- args << " client"
190- << " "
191- << argv[2 ];
192- std::string farg = args.str ();
193- std::vector<char > buf = std::vector<char >(farg.data (), farg.data () + farg.length () + 1 );
194-
195- blog (" Starting Client..." );
196-
197- STARTUPINFO si; memset (&si, 0 , sizeof (STARTUPINFO));
198- si.cb = sizeof (STARTUPINFO);
199-
200- PROCESS_INFORMATION pi; memset (&pi, 0 , sizeof (PROCESS_INFORMATION));
201- if (!CreateProcessA (
202- NULL ,
203- buf.data (),
204- NULL ,
205- NULL ,
206- false ,
207- CREATE_NEW_CONSOLE,
208- NULL ,
209- NULL ,
210- &si,
211- &pi)) {
212- blog (" Starting Client failed." );
213- }
214- #endif
215212 std::cin.get ();
216213 killswitch = true ;
217214 } else {
0 commit comments