1 /*
2  * Collie - An asynchronous event-driven network framework using Dlang development
3  *
4  * Copyright (C) 2015-2016  Shanghai Putao Technology Co., Ltd 
5  *
6  * Developer: putao's Dlang team
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 module app;
12 
13 import core.thread;
14 
15 import std.datetime;
16 import std.stdio;
17 import std.functional;
18 import std.exception;
19 import std.string;
20 
21 import yu.eventloop;
22 import yu.asyncsocket;
23 import yu.asyncsocket.client.client;
24 
25 import core.thread;
26 import core.sync.semaphore;
27 import yu.memory.allocator;
28 
29 @trusted class MyClient : BaseClient {
30     this() {
31         super(yNew!EventLoop());
32     }
33 
34     ~this() {
35         EventLoop loop = eventLoop;
36         if (loop)
37             yDel(loop);
38         if (sem)
39             yDel(sem);
40         if (th)
41             yDel(th);
42     }
43 
44     void runInThread() {
45         if (th !is null || isAlive)
46             return;
47         th = yNew!Thread(() { eventLoop.run(); });
48         th.start();
49     }
50 
51     void syncConnet(Address addr) {
52         if (sem is null) {
53             sem = yNew!Semaphore(0);
54         }
55         _sync = true;
56         scope (failure)
57             _sync = false;
58         connect(addr, &onCreate);
59         sem.wait();
60     }
61 
62     Thread th;
63 protected:
64     override void onActive() nothrow {
65         collectException({
66             if (_sync)
67                 sem.notify();
68             writeln("connect suess!");
69         }());
70     }
71 
72     override void onFailure() nothrow {
73         collectException({
74             if (_sync)
75                 sem.notify();
76             writeln("connect failure!");
77         }());
78     }
79 
80     override void onClose() nothrow {
81         collectException(writeln("connect close!"));
82     }
83 
84     override void onRead(in ubyte[] data) nothrow {
85         collectException(writeln("read data : ", cast(string) data));
86     }
87 
88     override void onTimeout() nothrow {
89         collectException({
90             if (isAlive) {
91                 writeln("time out do beat!");
92                 string data = Clock.currTime().toSimpleString();
93                 write(cast(ubyte[]) data, null);
94             }
95         }());
96     }
97 
98     void onCreate(TCPClient client) {
99         // set client;
100         client.setKeepAlive(1200, 2);
101         writeln("create a tcp client!");
102     }
103 
104 private:
105 
106     Semaphore sem;
107     bool _sync = false;
108 }
109 
110 void main() {
111     import std.experimental.allocator.mallocator;
112 
113     //yuAlloctor = allocatorObject(Mallocator.instance);
114 
115     MyClient client = yNew!MyClient();
116     client.runInThread();
117     client.setTimeout(60);
118     client.tryCount(3);
119     client.syncConnet(yNew!InternetAddress("127.0.0.1", cast(ushort) 8094));
120     //client.connect(new InternetAddress("127.0.0.1",8094));
121     //client.eventLoop.run();
122     if (!client.isAlive) {
123         return;
124     }
125     while (true) {
126         writeln("write to send server: ");
127         string data = readln();
128         if (startsWith(data, "\\q")) {
129             client.close();
130             client.eventLoop.stop();
131             client.th.join();
132             yDel(client);
133             return;
134         }
135         client.write(cast(ubyte[]) data, null);
136     }
137 }