1'use strict';
2import * as event from 'events'
3import * as TCP from 'TCP'
4
5class TCPClient extends event.EventEmitter{
6    constructor(options) {
7        super();
8        if (!options || !options.host || !options.port) {
9            throw new Error("invalid params");
10        }
11
12        this.options = {
13            host: options.host,
14            port: options.port
15        }
16
17        this.success = options.success || function(){};
18        this.fail = options.fail || function(){};
19        this._connect();
20    }
21
22    _connect() {
23        this.connected = false;
24        var cb = function(ret) {
25            if (ret < 0) {
26                this.fail();
27                this.emit('error', 'tcp connect error');
28                return;
29            }
30            this.tcpClientInstance = ret;
31            this.connected = true;
32            this.success();
33            this.emit('connect');
34            this._onListening();
35        }
36
37        if(TCP.createSocket(this.options, cb.bind(this)) < 0){
38            this.fail();
39            this.emit('error', 'tcp connect error');
40        }
41    }
42
43    send(options) {
44        if (!options.message) {
45            throw new Error("tcp send message is empty");
46        }
47
48        if(this.connected === false) {
49            throw new Error("tcp not connected");
50        }
51        TCP.send(this.tcpClientInstance, options.message, function(ret) {
52            if (ret < 0) {
53                this.emit('error', 'tcp send error');
54                if(options.fail) options.fail();
55                return;
56            }
57            this.emit('send', 'tcp send success');
58            if(options.success) options.success();
59        }.bind(this));
60    };
61
62    _onListening() {
63        if (!this.tcpClientInstance) {
64            throw new Error("tcpserver not init");
65        }
66        TCP.recv(this.tcpClientInstance, function(datalen, data) {
67            if (datalen === -2) {
68                this.connected = false;
69                this.emit('error', 'tcp receive message error');
70                return;
71            }
72            if (datalen === -1) {
73                this.connected = false;
74                this.emit('disconnect');
75                return;
76            }
77            if (datalen > 0) {
78                this.emit('message', data);
79            }
80        }.bind(this));
81    };
82
83    close() {
84        if (!this.tcpClientInstance) {
85            throw new Error("tcpserver not init");
86        }
87        var ret = TCP.close(this.tcpClientInstance);
88        if (ret != 0) {
89            this.emit('error', 'tcp socket close error');
90            return;
91        }
92        this.emit('close', 'tcp socket close success');
93    };
94
95    reconnect() {
96        if (this.tcpClientInstance) {
97            TCP.close(this.tcpClientInstance);
98        }
99        this._connect();
100    };
101}
102
103export function createClient(options) {
104    return new TCPClient(options);
105}
106