<commit_before>//
//  903WVideoStream.swift
//  WIFIAV
//
//  Created by <NAME> on 3/13/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import AVFoundation

protocol NetworkVideoStream: class {
    var delegate: NetworkVideoStreamDelegate? { get set }
    
    var socket: Socket { get set }
    var buffer: NaluBuffer { get set }
    var converter: VideoStreamConverter & NaluBufferDelegate { get set }
    
    init(socket: Socket, buffer: NaluBuffer, converter: VideoStreamConverter & NaluBufferDelegate)
    
    func process(_ data: Data)
}

protocol NetworkVideoStreamDelegate: class {
    func handle(sample: CMSampleBuffer, from stream: NetworkVideoStream)
}

class W903NetworkVideoStream: NetworkVideoStream {
    weak var delegate: NetworkVideoStreamDelegate?
    var socket: Socket
    var buffer: NaluBuffer
    var converter: VideoStreamConverter & NaluBufferDelegate
    
    required init(socket: Socket = try! UDPSocket(port: 3102, queue: DispatchQueue.main),
                  buffer: NaluBuffer = RawH264NaluBuffer(length: 1024 * 50),
                  converter: VideoStreamConverter & NaluBufferDelegate = ElementaryVideoStreamConverter()) {
        self.socket = socket
        self.buffer = buffer
        self.converter = converter
        
        socket.delegate = self
        buffer.delegate = converter
        converter.delegate = self
        
        socket.listen()
    }
    
    deinit {
        socket.shutdown()
    }
    
    enum ReceivingState {
        case idle
        case gotAnnouncement
        case gotAllInfo
        case gotStreamSettings
    }
    
    var state = ReceivingState.idle
    
    func process(_ data: Data) {
         data.withUnsafeBytes { (bytesPtr: UnsafePointer<UInt8>) -> Void in
             let bufferPointer = UnsafeRawBufferPointer(start: bytesPtr, count: data.count)
             
             switch state {
             case .idle where Announcement.isRecognized(in: bufferPointer):
                try? socket.send(AllInfoRequest.bytes)
                state = .gotAnnouncement
             
             case .gotAnnouncement where AllInfoResponse.isRecognized(in: bufferPointer):
                try? socket.send(Acknowledgement(received: 0, next: 1).bytes)
                try? socket.send(StreamSettingsRequest.bytes)
                state = .gotAllInfo
             
             case .gotAllInfo where StreamSettingsResponse.isRecognized(in: bufferPointer):
                try? socket.send(Acknowledgement(received: 1, next: 2).bytes)
                state = .gotStreamSettings
             
             case .gotStreamSettings where VideoData.isRecognized(in: bufferPointer):
                let videoData = try! VideoData(bufferPointer)
                try? socket.send(Acknowledgement(received: videoData.sequence, next: videoData.sequence &+ 1).bytes)
                buffer.append(videoData.bytes)
             
             case _ where Announcement.isRecognized(in: bufferPointer):
                state = .idle
                break
             
             default:
                print("Default case")
             }
         }
    }
}

extension W903NetworkVideoStream: SocketDelegate {
    func didReceive(data: Data, from address: sockaddr_in, on socket: Socket) {
        process(data)
    }
}
<commit_msg>Explicitly crash on send exceptions<commit_after>//
//  903WVideoStream.swift
//  WIFIAV
//
//  Created by <NAME> on 3/13/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import AVFoundation

protocol NetworkVideoStream: class {
    var delegate: NetworkVideoStreamDelegate? { get set }
    
    var socket: Socket { get set }
    var buffer: NaluBuffer { get set }
    var converter: VideoStreamConverter & NaluBufferDelegate { get set }
    
    init(socket: Socket, buffer: NaluBuffer, converter: VideoStreamConverter & NaluBufferDelegate)
    
    func process(_ data: Data)
}

protocol NetworkVideoStreamDelegate: class {
    func handle(sample: CMSampleBuffer, from stream: NetworkVideoStream)
}

class W903NetworkVideoStream: NetworkVideoStream {
    weak var delegate: NetworkVideoStreamDelegate?
    var socket: Socket
    var buffer: NaluBuffer
    var converter: VideoStreamConverter & NaluBufferDelegate
    
    required init(socket: Socket = try! UDPSocket(port: 3102, queue: DispatchQueue.main),
                  buffer: NaluBuffer = RawH264NaluBuffer(length: 1024 * 50),
                  converter: VideoStreamConverter & NaluBufferDelegate = ElementaryVideoStreamConverter()) {
        self.socket = socket
        self.buffer = buffer
        self.converter = converter
        
        socket.delegate = self
        buffer.delegate = converter
        converter.delegate = self
        
        socket.listen()
    }
    
    deinit {
        socket.shutdown()
    }
    
    enum ReceivingState {
        case idle
        case gotAnnouncement
        case gotAllInfo
        case gotStreamSettings
    }
    
    var state = ReceivingState.idle
    
    func process(_ data: Data) {
         data.withUnsafeBytes { (bytesPtr: UnsafePointer<UInt8>) -> Void in
             let bufferPointer = UnsafeRawBufferPointer(start: bytesPtr, count: data.count)
             
             switch state {
             case .idle where Announcement.isRecognized(in: bufferPointer):
                try! socket.send(AllInfoRequest.bytes)
                state = .gotAnnouncement
             
             case .gotAnnouncement where AllInfoResponse.isRecognized(in: bufferPointer):
                try! socket.send(Acknowledgement(received: 0, next: 1).bytes)
                try! socket.send(StreamSettingsRequest.bytes)
                state = .gotAllInfo
             
             case .gotAllInfo where StreamSettingsResponse.isRecognized(in: bufferPointer):
                try! socket.send(Acknowledgement(received: 1, next: 2).bytes)
                state = .gotStreamSettings
             
             case .gotStreamSettings where VideoData.isRecognized(in: bufferPointer):
                let videoData = try! VideoData(bufferPointer)
                try! socket.send(Acknowledgement(received: videoData.sequence, next: videoData.sequence &+ 1).bytes)
                buffer.append(videoData.bytes)
             
             case _ where Announcement.isRecognized(in: bufferPointer):
                state = .idle
                break
             
             default:
                print("Default case")
             }
         }
    }
}

extension W903NetworkVideoStream: SocketDelegate {
    func didReceive(data: Data, from address: sockaddr_in, on socket: Socket) {
        process(data)
    }
}

{'fraction_non_alphanumeric': 0.07621040047818291, 'fraction_numerical': 0.01105797967722654, 'mean_word_length': 2.1555869872701554, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869005', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1736, 'n_words': 436}