-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] NearbyNetwork 데이터 송수신 기능 리팩터링 #152
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ import OSLog | |
public final class RefactoredNearbyNetworkService { | ||
public var connectionDelegate: NearbyNetworkConnectionDelegate? = nil | ||
public var foundPeerHandler: ((_ networkConnections: [RefactoredNetworkConnection]) -> Void)? | ||
public let refactoredReciptDataPublisher: AnyPublisher<DataInformationDTO, Never> | ||
private let refactoredReciptDataSubject: PassthroughSubject<DataInformationDTO, Never> | ||
private let serviceName: String | ||
private let serviceType: String | ||
private let peerID: UUID | ||
|
@@ -42,6 +44,8 @@ public final class RefactoredNearbyNetworkService { | |
nearbyNetworkConnections = [:] | ||
jsonEncoder = JSONEncoder() | ||
jsonDecoder = JSONDecoder() | ||
refactoredReciptDataSubject = PassthroughSubject<DataInformationDTO, Never>() | ||
refactoredReciptDataPublisher = refactoredReciptDataSubject.eraseToAnyPublisher() | ||
self.serviceName = serviceName | ||
self.serviceType = serviceType | ||
nearbyNetworkBrowser.delegate = self | ||
|
@@ -99,7 +103,42 @@ public final class RefactoredNearbyNetworkService { | |
} | ||
|
||
private func handleReceivedData(data: Data?, connection: NWConnection) { | ||
self.logger.log(level: .error, "\(connection.debugDescription): 데이터 수신") | ||
guard | ||
let data, | ||
let dataDTO = try? jsonDecoder.decode(DataInformationDTO.self, from: data) | ||
else { return } | ||
|
||
refactoredReciptDataSubject.send(dataDTO) | ||
self.logger.log(level: .debug, "\(connection.debugDescription): 데이터 수신") | ||
} | ||
|
||
private func send(data: DataInformationDTO, connection: NWConnection) async -> Bool { | ||
typealias Continuation = CheckedContinuation<Bool, Never> | ||
var tryCount = 0 | ||
|
||
let encodedData = try? jsonEncoder.encode(data) | ||
let message = NWProtocolFramer.Message(nearbyNetworkMessageType: .data) | ||
let context = NWConnection.ContentContext(identifier: "Data", metadata: [message]) | ||
|
||
while tryCount < 3 { | ||
let result = await withCheckedContinuation { (continuation: Continuation) in | ||
connection.send( | ||
content: encodedData, | ||
contentContext: context, | ||
completion: .contentProcessed({ error in | ||
if let error { | ||
continuation.resume(returning: false) | ||
} else { | ||
continuation.resume(returning: true) | ||
} | ||
})) | ||
} | ||
|
||
if result { return true } | ||
tryCount += 1 | ||
} | ||
Comment on lines
+123
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. withCheckedContinuation으로 클로저 내부의 error에 따른 처리를 해 주셨군요..?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 Continuation을 사용한 이유는 send의 결과 값을 반환하기 위해 불가피하게 wrapping 하였습니다! |
||
|
||
return false | ||
} | ||
} | ||
|
||
|
@@ -192,16 +231,36 @@ extension RefactoredNearbyNetworkService: NearbyNetworkInterface { | |
} | ||
|
||
public func send(data: Data) { | ||
|
||
// TODO: - will be deprecated | ||
} | ||
|
||
public func send(fileURL: URL, info: DataInformationDTO) async { | ||
// TODO: - will be deprecated | ||
} | ||
|
||
public func send(data: DataInformationDTO) async -> Bool { | ||
let result: Bool = await withTaskGroup(of: Bool.self, returning: Bool.self) { taskGroup in | ||
for connection in nearbyNetworkConnections.values { | ||
taskGroup.addTask { | ||
return await self.send(data: data, connection: connection) | ||
} | ||
} | ||
for await childResult in taskGroup { | ||
if !childResult { return false } | ||
} | ||
return true | ||
} | ||
return result | ||
} | ||
|
||
public func send(fileURL: URL, info: DataInformationDTO, to connection: NetworkConnection) async { | ||
// TODO: - will be deprecated | ||
} | ||
|
||
public func send(data: DataInformationDTO, to connection: RefactoredNetworkConnection) async -> Bool { | ||
guard let connection = nearbyNetworkConnections[connection] else { return false } | ||
return await send(data: data, connection: connection) | ||
} | ||
} | ||
|
||
// MARK: - NearbyNetworkBrowserDelegate | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
갑자기 이거 보고 생각났는데..
저는 딴이랑 페프할 때 한줄이면
return
생략했거덩요?별거 아니니까 그냥 무시시 고고??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
허얼 ~ 1줄이여도 노 생략인게 저희 컨벤션으루 기억합니다만 !!??!!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엇.. 저희 명시적으로 작성하려구 한 줄 일때도 return 생략하지 않기로 했던 것으로 기억합니다!