• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "netserver.h"
17 
18 #include "utils/log.h"
19 
20 #include <cstdlib>
21 
22 using namespace std;
23 
NetServer(napi_env env,napi_value thisVar)24 NetServer::NetServer(napi_env env, napi_value thisVar) : EventTarget(env, thisVar)
25 {
26     napi_get_uv_event_loop(env, &loop_);
27     tcpServer_ = { 0 };
28     tcpServer_.data = this;
29     serverClosed_ = false;
30     clients_ = nullptr;
31 }
32 
~NetServer()33 NetServer::~NetServer() {}
34 
Start(int port)35 int NetServer::Start(int port)
36 {
37     struct sockaddr_in addr;
38     int result = 0;
39 
40     uv_ip4_addr("0.0.0.0", port, &addr);
41 
42     result = uv_tcp_init(loop_, &tcpServer_);
43     if (result) {
44         this->Emit("error", nullptr);
45         return -1;
46     }
47 
48     result = uv_tcp_bind(&tcpServer_, (const struct sockaddr*)&addr, 0);
49     if (result) {
50         this->Emit("error", nullptr);
51         return -1;
52     }
53 
54     result = uv_listen((uv_stream_t*)&tcpServer_, SOMAXCONN, OnConnection);
55     if (result) {
56         this->Emit("error", nullptr);
57         return -1;
58     }
59 
60     Emit("started", nullptr);
61 
62     return 0;
63 }
64 
Stop()65 void NetServer::Stop()
66 {
67     Emit("closed", nullptr);
68     uint32_t thisRefCount = 0;
69     napi_reference_unref(env_, thisVarRef_, &thisRefCount);
70 }
71 
OnClose(uv_handle_t * peer)72 void NetServer::OnClose(uv_handle_t* peer)
73 {
74     if (peer == nullptr) {
75         HILOG_ERROR("peer is null");
76         return;
77     }
78 
79     NetServer* that = (NetServer*)peer->data;
80     that->Emit("disconnect", nullptr);
81     free(peer);
82 }
83 
OnConnection(uv_stream_t * server,int status)84 void NetServer::OnConnection(uv_stream_t* server, int status)
85 {
86     if (server == nullptr) {
87         HILOG_ERROR("server is null");
88         return;
89     }
90 
91     NetServer* that = (NetServer*)server->data;
92 
93     if (status != 0) {
94         that->Emit("error", nullptr);
95     }
96 
97     if (that->clients_ == nullptr) {
98         that->clients_ = new NetClient();
99     } else {
100         auto tmp = new NetClient();
101         tmp->next = that->clients_;
102         that->clients_ = tmp;
103     }
104 
105     uv_tcp_init(that->loop_, (uv_tcp_t*)&that->clients_->tcp);
106     that->clients_->tcp.data = server->data;
107     uv_accept(server, (uv_stream_t*)&that->clients_->tcp);
108     uv_read_start((uv_stream_t*)&that->clients_->tcp, EchoAlloc, AfterRead);
109 
110     that->Emit("connect", nullptr);
111 }
112 
OnServerClose(uv_handle_t * handle)113 void NetServer::OnServerClose(uv_handle_t* handle)
114 {
115     if (handle == nullptr) {
116         HILOG_ERROR("handle is null");
117         return;
118     }
119 
120     NetServer* that = (NetServer*)handle->data;
121 
122     for (NetClient* i = that->clients_; i != nullptr; i = i->next) {
123         uv_close((uv_handle_t*)&i->tcp, nullptr);
124     }
125 
126     uint32_t thisRefCount = 0;
127     napi_reference_unref(that->env_, that->thisVarRef_, &thisRefCount);
128 }
129 
AfterWrite(uv_write_t * req,int status)130 void NetServer::AfterWrite(uv_write_t* req, int status)
131 {
132     if (req == nullptr) {
133         HILOG_ERROR("req is null");
134         return;
135     }
136 
137     NetServer* that = (NetServer*)req->data;
138 
139     WriteReq* wr = (WriteReq*)req;
140 
141     free(wr->buf.base);
142     free(wr);
143 
144     if (status == 0) {
145         that->Emit("write", nullptr);
146         return;
147     }
148 
149     that->Emit("error", nullptr);
150 }
151 
AfterRead(uv_stream_t * handle,ssize_t nread,const uv_buf_t * buf)152 void NetServer::AfterRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
153 {
154     if (handle == nullptr) {
155         HILOG_ERROR("handle is null");
156         return;
157     }
158 
159     if (buf == nullptr) {
160         HILOG_ERROR("buf is null");
161         return;
162     }
163 
164     NetServer* that = (NetServer*)handle->data;
165     WriteReq* wr = nullptr;
166     uv_shutdown_t* sreq = nullptr;
167 
168     if (nread < 0) {
169         free(buf->base);
170         sreq = (uv_shutdown_t*)malloc(sizeof(*sreq));
171         if (sreq == nullptr) {
172             HILOG_ERROR("sreq is null");
173             return;
174         }
175         sreq->data = that;
176         uv_shutdown(sreq, handle, AfterShutdown);
177         return;
178     }
179 
180     if (nread == 0) {
181         free(buf->base);
182         return;
183     }
184 
185     if (!that->serverClosed_) {
186         for (int i = 0; i < nread; i++) {
187             if (buf->base[i] == 'Q') {
188                 if (i + 1 < nread && buf->base[i + 1] == 'S') {
189                     free(buf->base);
190                     uv_close((uv_handle_t*)handle, OnClose);
191                     return;
192                 } else {
193                     uv_close((uv_handle_t*)&that->tcpServer_, OnServerClose);
194                     that->serverClosed_ = 1;
195                     return;
196                 }
197             }
198         }
199     }
200 
201     that->Emit("read", nullptr);
202 
203     wr = (WriteReq*)malloc(sizeof(WriteReq));
204     if (wr == nullptr) {
205         HILOG_ERROR("wr is null");
206         free(buf->base);
207         return;
208     }
209 
210     wr->buf = uv_buf_init(buf->base, nread);
211 
212     wr->req.data = that;
213 
214     if (uv_write(&wr->req, handle, &wr->buf, 1, AfterWrite) != 0) {
215         that->Emit("error", nullptr);
216     }
217 }
218 
AfterShutdown(uv_shutdown_t * req,int status)219 void NetServer::AfterShutdown(uv_shutdown_t* req, int status)
220 {
221     if (req == nullptr) {
222         HILOG_ERROR("req is null");
223         return;
224     }
225 
226     uv_close((uv_handle_t*)req->handle, OnClose);
227     free(req);
228 }
229 
EchoAlloc(uv_handle_t * handle,size_t suggestedSize,uv_buf_t * buf)230 void NetServer::EchoAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
231 {
232     if (handle == nullptr) {
233         HILOG_ERROR("handle is null");
234         return;
235     }
236 
237     if (buf == nullptr) {
238         HILOG_ERROR("buf is null");
239         return;
240     }
241 
242     buf->base = (char*)malloc(suggestedSize);
243     if (buf->base != nullptr) {
244         HILOG_ERROR("buf->base is null");
245         return;
246     }
247 
248     buf->len = suggestedSize;
249 }
250