11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef SRC_STREAM_WRAP_H_
231cb0ef41Sopenharmony_ci#define SRC_STREAM_WRAP_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include "stream_base.h"
281cb0ef41Sopenharmony_ci#include "handle_wrap.h"
291cb0ef41Sopenharmony_ci#include "v8.h"
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cinamespace node {
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciclass Environment;
341cb0ef41Sopenharmony_ciclass ExternalReferenceRegistry;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciclass LibuvStreamWrap : public HandleWrap, public StreamBase {
371cb0ef41Sopenharmony_ci public:
381cb0ef41Sopenharmony_ci  static void Initialize(v8::Local<v8::Object> target,
391cb0ef41Sopenharmony_ci                         v8::Local<v8::Value> unused,
401cb0ef41Sopenharmony_ci                         v8::Local<v8::Context> context,
411cb0ef41Sopenharmony_ci                         void* priv);
421cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
431cb0ef41Sopenharmony_ci  int GetFD() override;
441cb0ef41Sopenharmony_ci  bool IsAlive() override;
451cb0ef41Sopenharmony_ci  bool IsClosing() override;
461cb0ef41Sopenharmony_ci  bool IsIPCPipe() override;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // JavaScript functions
491cb0ef41Sopenharmony_ci  int ReadStart() override;
501cb0ef41Sopenharmony_ci  int ReadStop() override;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // Resource implementation
531cb0ef41Sopenharmony_ci  int DoShutdown(ShutdownWrap* req_wrap) override;
541cb0ef41Sopenharmony_ci  int DoTryWrite(uv_buf_t** bufs, size_t* count) override;
551cb0ef41Sopenharmony_ci  int DoWrite(WriteWrap* w,
561cb0ef41Sopenharmony_ci              uv_buf_t* bufs,
571cb0ef41Sopenharmony_ci              size_t count,
581cb0ef41Sopenharmony_ci              uv_stream_t* send_handle) override;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  inline uv_stream_t* stream() const {
611cb0ef41Sopenharmony_ci    return stream_;
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  inline bool is_named_pipe() const {
651cb0ef41Sopenharmony_ci    return stream()->type == UV_NAMED_PIPE;
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  inline bool is_named_pipe_ipc() const {
691cb0ef41Sopenharmony_ci    return is_named_pipe() &&
701cb0ef41Sopenharmony_ci           reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0;
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  inline bool is_tcp() const {
741cb0ef41Sopenharmony_ci    return stream()->type == UV_TCP;
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  ShutdownWrap* CreateShutdownWrap(v8::Local<v8::Object> object) override;
781cb0ef41Sopenharmony_ci  WriteWrap* CreateWriteWrap(v8::Local<v8::Object> object) override;
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  static LibuvStreamWrap* From(Environment* env, v8::Local<v8::Object> object);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci protected:
831cb0ef41Sopenharmony_ci  LibuvStreamWrap(Environment* env,
841cb0ef41Sopenharmony_ci                  v8::Local<v8::Object> object,
851cb0ef41Sopenharmony_ci                  uv_stream_t* stream,
861cb0ef41Sopenharmony_ci                  AsyncWrap::ProviderType provider);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  AsyncWrap* GetAsyncWrap() override;
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
911cb0ef41Sopenharmony_ci      Environment* env);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci protected:
941cb0ef41Sopenharmony_ci  inline void set_fd(int fd) {
951cb0ef41Sopenharmony_ci#ifdef _WIN32
961cb0ef41Sopenharmony_ci    fd_ = fd;
971cb0ef41Sopenharmony_ci#endif
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci private:
1021cb0ef41Sopenharmony_ci  static void GetWriteQueueSize(
1031cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& info);
1041cb0ef41Sopenharmony_ci  static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  // Callbacks for libuv
1071cb0ef41Sopenharmony_ci  void OnUvAlloc(size_t suggested_size, uv_buf_t* buf);
1081cb0ef41Sopenharmony_ci  v8::Maybe<void> OnUvRead(ssize_t nread, const uv_buf_t* buf);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  static void AfterUvWrite(uv_write_t* req, int status);
1111cb0ef41Sopenharmony_ci  static void AfterUvShutdown(uv_shutdown_t* req, int status);
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  uv_stream_t* const stream_;
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci#ifdef _WIN32
1161cb0ef41Sopenharmony_ci  // We don't always have an FD that we could look up on the stream_
1171cb0ef41Sopenharmony_ci  // object itself on Windows. However, for some cases, we open handles
1181cb0ef41Sopenharmony_ci  // using FDs; In that case, we can store and provide the value.
1191cb0ef41Sopenharmony_ci  // This became necessary because it allows to detect situations
1201cb0ef41Sopenharmony_ci  // where multiple handles refer to the same stdio FDs (in particular,
1211cb0ef41Sopenharmony_ci  // a possible IPC channel and a regular process.std??? stream).
1221cb0ef41Sopenharmony_ci  int fd_ = -1;
1231cb0ef41Sopenharmony_ci#endif
1241cb0ef41Sopenharmony_ci};
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci}  // namespace node
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci#endif  // SRC_STREAM_WRAP_H_
132