• 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 "app_spawn_socket.h"
17 
18 #include "app_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 // arg "AppSpawn" cannot be defined as string object since REGISTER_SYSTEM_ABILITY will
23 // firstly start without init this string object, which leads to error.
24 
AppSpawnSocket()25 AppSpawnSocket::AppSpawnSocket() : clientSocket_(std::make_unique<AppSpawn::ClientSocket>("AppSpawn"))
26 {}
27 
~AppSpawnSocket()28 AppSpawnSocket::~AppSpawnSocket()
29 {}
30 
OpenAppSpawnConnection()31 ErrCode AppSpawnSocket::OpenAppSpawnConnection()
32 {
33     APP_LOGD("ready to open connection");
34     if (clientSocket_) {
35         if (clientSocket_->CreateClient() != ERR_OK) {
36             APP_LOGE("failed to create socketClient");
37             return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT;
38         }
39         if (clientSocket_->ConnectSocket() != ERR_OK) {
40             APP_LOGE("failed to connect socket");
41             return ERR_APPEXECFWK_CONNECT_APPSPAWN_FAILED;
42         }
43         APP_LOGD("connection has been opened");
44         return ERR_OK;
45     }
46     APP_LOGE("failed to open connection without socket");
47     return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET;
48 }
49 
CloseAppSpawnConnection()50 void AppSpawnSocket::CloseAppSpawnConnection()
51 {
52     if (clientSocket_) {
53         clientSocket_->CloseClient();
54     }
55 }
56 
WriteMessage(const void * buf,const int32_t len)57 ErrCode AppSpawnSocket::WriteMessage(const void *buf, const int32_t len)
58 {
59     APP_LOGD("ready to write message");
60     if (len <= 0) {
61         APP_LOGE("failed to write message due to invalid length of message");
62         return ERR_INVALID_VALUE;
63     }
64     if (buf == nullptr) {
65         APP_LOGE("failed to write message due to null buf");
66         return ERR_INVALID_VALUE;
67     }
68     if (clientSocket_) {
69         if (clientSocket_->WriteSocketMessage(buf, len) != len) {
70             APP_LOGE("failed to write message due to invalid write length");
71             return ERR_APPEXECFWK_SOCKET_WRITE_FAILED;
72         }
73         APP_LOGD("write message success");
74         return ERR_OK;
75     }
76 
77     APP_LOGE("failed to write message without socket");
78     return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET;
79 }
80 
ReadMessage(void * buf,const int32_t len)81 ErrCode AppSpawnSocket::ReadMessage(void *buf, const int32_t len)
82 {
83     APP_LOGD("ready to read message");
84     if (len <= 0) {
85         APP_LOGE("failed to read message due to invalid length of cache");
86         return ERR_INVALID_VALUE;
87     }
88     if (buf == nullptr) {
89         APP_LOGE("failed to read message due to null buf");
90         return ERR_INVALID_VALUE;
91     }
92     if (clientSocket_) {
93         if (clientSocket_->ReadSocketMessage(buf, len) != len) {
94             APP_LOGE("failed to read message due to invalid read length");
95             return ERR_APPEXECFWK_SOCKET_READ_FAILED;
96         }
97         APP_LOGD("read message success");
98         return ERR_OK;
99     }
100     APP_LOGE("failed to read message without socket");
101     return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT;
102 }
103 
SetClientSocket(const std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket)104 void AppSpawnSocket::SetClientSocket(const std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket)
105 {
106     clientSocket_ = clientSocket;
107 }
108 }  // namespace AppExecFwk
109 }  // namespace OHOS
110