-
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
[Feature] 근거리 통신 모듈에 URL을 이용한 통신 메서드 추가 #103
Changes from all commits
b8b9980
4cc4ec9
18b5eba
3ae769e
6db839f
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import Foundation | ||
|
||
public protocol NearbyNetworkInterface { | ||
var delegate: NearbyNetworkDelegate? { get set } | ||
var connectionDelegate: NearbyNetworkConnectionDelegate? { get set } | ||
|
||
/// 주변 기기를 검색합니다. | ||
func startSearching() | ||
|
@@ -33,15 +33,15 @@ public protocol NearbyNetworkInterface { | |
/// 연결된 기기들에게 데이터를 송신합니다. | ||
/// - Parameter data: 송신할 데이터 | ||
func send(data: Data) | ||
} | ||
|
||
public protocol NearbyNetworkDelegate: AnyObject { | ||
/// 데이터를 수신했을 때 실행됩니다. | ||
/// 연결된 기기들에게 파일을 송신합니다. | ||
/// - Parameters: | ||
/// - data: 수신된 데이터 | ||
/// - connection: 데이터를 송신한 기기 | ||
func nearbyNetwork(_ sender: NearbyNetworkInterface, didReceive data: Data, from connection: NetworkConnection) | ||
/// - fileURL: 파일의 URL | ||
/// - info: 파일에 대한 정보 | ||
func send(fileURL: URL, info: DataInformationDTO) async | ||
} | ||
|
||
public protocol NearbyNetworkConnectionDelegate: AnyObject { | ||
/// 주변 기기에게 연결 요청을 받았을 때 실행됩니다. | ||
/// - Parameters: | ||
/// - connectionHandler: 연결 요청 처리 Handler | ||
|
@@ -55,3 +55,19 @@ public protocol NearbyNetworkDelegate: AnyObject { | |
/// 주변 기기와의 연결에 실패했을 때 실행됩니다. | ||
func nearbyNetworkCannotConnect(_ sender: NearbyNetworkInterface) | ||
} | ||
|
||
public protocol NearbyNetworkReceiptDelegate: AnyObject { | ||
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. connection, receipt 적절한 기준으로 잘 나눈거같아용 |
||
/// 데이터를 수신했을 때 실행됩니다. | ||
/// - Parameters: | ||
/// - data: 수신된 데이터 | ||
func nearbyNetwork(_ sender: NearbyNetworkInterface, didReceive data: Data) | ||
|
||
/// 파일을 수신했을 때 실행됩니다. | ||
/// - Parameters: | ||
/// - URL: 수신한 파일의 URL | ||
/// - info: 파일에 대한 정보 | ||
func nearbyNetwork( | ||
_ sender: NearbyNetworkInterface, | ||
didReceiveURL URL: URL, | ||
info: DataInformationDTO) | ||
Comment on lines
+69
to
+72
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. style: 닫히는 괄호가 밑으로 내려와야 하는 것 같습니다! 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. 요 부분은 저희 같이 이야기 나눈대로 현상유지하도록 하겠습니다 :) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// MCSession+.swift | ||
// NearbyNetwork | ||
// | ||
// Created by 이동현 on 11/20/24. | ||
// | ||
|
||
import MultipeerConnectivity | ||
|
||
// MARK: - Swift Concurrency로 wrapping | ||
extension MCSession { | ||
func sendResource( | ||
at resourceURL: URL, | ||
withName resourceName: String, | ||
toPeer peer: MCPeerID | ||
) async throws { | ||
typealias Continuation = CheckedContinuation<Void, Error> | ||
|
||
try await withCheckedThrowingContinuation { (continuation: Continuation) in | ||
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. 이것을 통해 swift Concurrency처럼 사용할 수 있게 해주는 군요..! 이부분은 공부를 해봐야할 것 같습니다..!ㅜㅜ 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. 꺆 !!!!!!!!!!!!!! 쏘 어렵 ... 내일 설명해주기 ....................................... |
||
self.sendResource( | ||
at: resourceURL, | ||
withName: resourceName, | ||
toPeer: peer) { error in | ||
if let error { | ||
continuation.resume(throwing: error) | ||
} else { | ||
continuation.resume() | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// MCSessionState+.swift | ||
// NearbyNetwork | ||
// | ||
// Created by 이동현 on 11/20/24. | ||
// | ||
|
||
import MultipeerConnectivity | ||
|
||
extension MCSessionState { | ||
var description: String { | ||
switch self { | ||
case .notConnected: | ||
return "연결 끊김" | ||
case .connecting: | ||
return "연결 중" | ||
case .connected: | ||
return "연결 됨" | ||
@unknown default: | ||
return "알 수 없음" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ import MultipeerConnectivity | |
import OSLog | ||
|
||
public final class NearbyNetworkService: NSObject { | ||
public weak var delegate: NearbyNetworkDelegate? | ||
public weak var connectionDelegate: NearbyNetworkConnectionDelegate? | ||
public weak var receiptDelegate: NearbyNetworkReceiptDelegate? | ||
private let peerID: MCPeerID | ||
private let session: MCSession | ||
private var serviceAdvertiser: MCNearbyServiceAdvertiser | ||
|
@@ -20,7 +21,7 @@ public final class NearbyNetworkService: NSObject { | |
private var foundPeers: [MCPeerID: NetworkConnection] = [:] { | ||
didSet { | ||
let foundPeers: [NetworkConnection] = foundPeers.values.map { $0 } | ||
delegate?.nearbyNetwork(self, didFind: foundPeers) | ||
connectionDelegate?.nearbyNetwork(self, didFind: foundPeers) | ||
} | ||
} | ||
private let logger = Logger() | ||
|
@@ -77,9 +78,8 @@ extension NearbyNetworkService: NearbyNetworkInterface { | |
.first { $0.value == connection }? | ||
.key | ||
// TODO: Error 수정 | ||
guard let peerID else { | ||
throw NSError() | ||
} | ||
guard let peerID else { throw NSError() } | ||
|
||
serviceBrowser.invitePeer( | ||
peerID, | ||
to: session, | ||
|
@@ -97,6 +97,30 @@ extension NearbyNetworkService: NearbyNetworkInterface { | |
logger.log(level: .error, "데이터 전송 실패") | ||
} | ||
} | ||
|
||
public func send(fileURL: URL, info: DataSource.DataInformationDTO) async { | ||
let infoJsonData = try? JSONEncoder().encode(info) | ||
|
||
guard | ||
let infoJsonData, | ||
let infoJsonString = String(data: infoJsonData, encoding: .utf8) | ||
else { return } | ||
|
||
await withTaskGroup(of: Void.self) { taskGroup in | ||
session.connectedPeers.forEach { peer in | ||
taskGroup.addTask { | ||
do { | ||
try await self.session.sendResource( | ||
at: fileURL, | ||
withName: infoJsonString, | ||
toPeer: peer) | ||
} catch { | ||
self.logger.log(level: .error, "\(peer)에게 file 데이터 전송 실패") | ||
} | ||
} | ||
} | ||
} | ||
Comment on lines
+109
to
+122
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. 이부분도 공부해보겠습니다! 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. 다시봐도 어렵네유 |
||
} | ||
} | ||
|
||
// MARK: - MCSessionDelegate | ||
|
@@ -131,7 +155,7 @@ extension NearbyNetworkService: MCSessionDelegate { | |
logger.log(level: .error, "\(peerID.displayName)와 연결되어 있지 않음") | ||
return | ||
} | ||
delegate?.nearbyNetwork(self, didReceive: data, from: connection) | ||
receiptDelegate?.nearbyNetwork(self, didReceive: data) | ||
} | ||
|
||
public func session( | ||
|
@@ -159,7 +183,16 @@ extension NearbyNetworkService: MCSessionDelegate { | |
at localURL: URL?, | ||
withError error: (any Error)? | ||
) { | ||
logger.log(level: .error, "\(peerID.displayName)로부터 데이터 수신을 완료함") | ||
guard | ||
let localURL, | ||
let jsonData = resourceName.data(using: .utf8), | ||
let dto = try? JSONDecoder().decode(DataInformationDTO.self, from: jsonData) | ||
else{ return } | ||
|
||
receiptDelegate?.nearbyNetwork( | ||
self, | ||
didReceiveURL: localURL, | ||
info: dto) | ||
} | ||
} | ||
|
||
|
@@ -171,7 +204,7 @@ extension NearbyNetworkService: MCNearbyServiceAdvertiserDelegate { | |
withContext context: Data?, | ||
invitationHandler: @escaping (Bool, MCSession?) -> Void | ||
) { | ||
delegate?.nearbyNetwork(self, didReceive: { [weak self] isAccepted in | ||
connectionDelegate?.nearbyNetwork(self, didReceive: { [weak self] isAccepted in | ||
invitationHandler(isAccepted, self?.session) | ||
}) | ||
} | ||
|
@@ -181,7 +214,7 @@ extension NearbyNetworkService: MCNearbyServiceAdvertiserDelegate { | |
didNotStartAdvertisingPeer error: any Error | ||
) { | ||
logger.log(level: .error, "Advertising 실패 \(error.localizedDescription)") | ||
delegate?.nearbyNetworkCannotConnect(self) | ||
connectionDelegate?.nearbyNetworkCannotConnect(self) | ||
} | ||
} | ||
|
||
|
@@ -203,19 +236,3 @@ extension NearbyNetworkService: MCNearbyServiceBrowserDelegate { | |
foundPeers[peerID] = nil | ||
} | ||
} | ||
|
||
// MARK: - MCSessionState | ||
extension MCSessionState { | ||
var description: String { | ||
switch self { | ||
case .notConnected: | ||
return "연결 끊김" | ||
case .connecting: | ||
return "연결 중" | ||
case .connected: | ||
return "연결 됨" | ||
@unknown default: | ||
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.
그러니까 근거리통신 연결 로직을 NearbyNetworkConnectionDelegate가,
근거리통신 송수신 로직을 NearbyNetworkReceiptDelegate가 담당하게 되는게 맞나요 ??
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.
똑똑한 조이 맞습니다!!