OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 sockets_by_port_[allocated_port] = os_socket; | 120 sockets_by_port_[allocated_port] = os_socket; |
121 sockets_by_fd_[socketfd] = os_socket; | 121 sockets_by_fd_[socketfd] = os_socket; |
122 | 122 |
123 // We set as a side-effect the port on the dart socket_object. | 123 // We set as a side-effect the port on the dart socket_object. |
124 Socket::SetSocketIdNativeField(socket_object, socketfd); | 124 Socket::SetSocketIdNativeField(socket_object, socketfd); |
125 | 125 |
126 return Dart_True(); | 126 return Dart_True(); |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) { | |
131 ASSERT(!mutex_->TryLock()); | |
132 ASSERT(os_socket != NULL); | |
133 ASSERT(os_socket->ref_count > 0); | |
134 os_socket->ref_count--; | |
135 if (os_socket->ref_count > 0) { | |
136 return false; | |
137 } | |
138 // We free the OS socket by removing it from two datastructures. | |
139 sockets_by_fd_.erase(os_socket->socketfd); | |
140 | |
141 OSSocket *prev = NULL; | |
142 OSSocket *current = sockets_by_port_[os_socket->port]; | |
143 while (current != os_socket) { | |
144 ASSERT(current != NULL); | |
145 prev = current; | |
146 current = current->next; | |
147 } | |
148 | |
149 if ((prev == NULL) && (current->next == NULL)) { | |
150 // Remove last element from the list. | |
151 sockets_by_port_.erase(os_socket->port); | |
152 } else if (prev == NULL) { | |
153 // Remove first element of the list. | |
154 sockets_by_port_[os_socket->port] = current->next; | |
155 } else { | |
156 // Remove element from the list which is not the first one. | |
157 prev->next = os_socket->next; | |
158 } | |
159 | |
160 ASSERT(os_socket->ref_count == 0); | |
161 delete os_socket; | |
162 return true; | |
163 } | |
164 | |
165 | |
166 void ListeningSocketRegistry::CloseAllSafe() { | |
167 MutexLocker ml(mutex_); | |
168 SocketsIterator it = sockets_by_fd_.begin(); | |
169 while (it != sockets_by_fd_.end()) { | |
170 CloseOneSafe(it->second); | |
171 it++; | |
172 } | |
173 } | |
174 | |
175 | |
176 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { | 130 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { |
177 ASSERT(!mutex_->TryLock()); | 131 ASSERT(!mutex_->TryLock()); |
| 132 |
178 SocketsIterator it = sockets_by_fd_.find(socketfd); | 133 SocketsIterator it = sockets_by_fd_.find(socketfd); |
179 if (it != sockets_by_fd_.end()) { | 134 if (it != sockets_by_fd_.end()) { |
180 return CloseOneSafe(it->second); | 135 OSSocket *os_socket = it->second; |
| 136 |
| 137 ASSERT(os_socket->ref_count > 0); |
| 138 os_socket->ref_count--; |
| 139 if (os_socket->ref_count == 0) { |
| 140 // We free the OS socket by removing it from two datastructures. |
| 141 sockets_by_fd_.erase(socketfd); |
| 142 |
| 143 OSSocket *prev = NULL; |
| 144 OSSocket *current = sockets_by_port_[os_socket->port]; |
| 145 while (current != os_socket) { |
| 146 ASSERT(current != NULL); |
| 147 prev = current; |
| 148 current = current->next; |
| 149 } |
| 150 |
| 151 if ((prev == NULL) && (current->next == NULL)) { |
| 152 // Remove last element from the list. |
| 153 sockets_by_port_.erase(os_socket->port); |
| 154 } else if (prev == NULL) { |
| 155 // Remove first element of the list. |
| 156 sockets_by_port_[os_socket->port] = current->next; |
| 157 } else { |
| 158 // Remove element from the list which is not the first one. |
| 159 prev->next = os_socket->next; |
| 160 } |
| 161 |
| 162 delete os_socket; |
| 163 return true; |
| 164 } |
| 165 return false; |
181 } else { | 166 } else { |
182 // It should be impossible for the event handler to close something that | 167 // It should be impossible for the event handler to close something that |
183 // hasn't been created before. | 168 // hasn't been created before. |
184 UNREACHABLE(); | 169 UNREACHABLE(); |
185 return false; | 170 return false; |
186 } | 171 } |
187 } | 172 } |
188 | 173 |
189 | 174 |
190 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 175 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 if (Dart_IsError(err)) { | 883 if (Dart_IsError(err)) { |
899 Dart_PropagateError(err); | 884 Dart_PropagateError(err); |
900 } | 885 } |
901 return socket; | 886 return socket; |
902 } | 887 } |
903 | 888 |
904 } // namespace bin | 889 } // namespace bin |
905 } // namespace dart | 890 } // namespace dart |
906 | 891 |
907 #endif // !defined(DART_IO_DISABLED) | 892 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |