Swiftgram/TelegramCore/Serialization.swift
2016-09-05 23:20:51 +03:00

79 lines
2.9 KiB
Swift

import Foundation
#if os(macOS)
import MtProtoKitMac
#else
import MtProtoKitDynamic
#endif
public class BoxedMessage: NSObject {
public let body: Any
public init(_ body: Any) {
self.body = body
}
override public var description: String {
get {
return "\(self.body)"
}
}
}
public class Serialization: NSObject, MTSerialization {
public func currentLayer() -> UInt {
return 53
}
public func parseMessage(_ data: Data!) -> Any! {
if let body = Api.parse(Buffer(data: data)) {
return BoxedMessage(body)
}
return nil
}
public func exportAuthorization(_ datacenterId: Int32, data: AutoreleasingUnsafeMutablePointer<NSData?>) -> MTExportAuthorizationResponseParser!
{
let functionContext = Api.functions.auth.exportAuthorization(dcId: datacenterId)
data.pointee = functionContext.1.makeData() as NSData
return { data -> MTExportedAuthorizationData! in
if let exported = functionContext.2(Buffer(data: data)) {
switch exported {
case let .exportedAuthorization(id, bytes):
return MTExportedAuthorizationData(authorizationBytes: bytes.makeData(), authorizationId: id)
}
} else {
return nil
}
}
}
public func importAuthorization(_ authId: Int32, bytes: Data!) -> Data! {
return Api.functions.auth.importAuthorization(id: authId, bytes: Buffer(data: bytes)).1.makeData()
}
public func requestDatacenterAddressList(_ datacenterId: Int32, data: AutoreleasingUnsafeMutablePointer<NSData?>) -> MTRequestDatacenterAddressListParser! {
let (_, buffer, parse) = Api.functions.help.getConfig()
data.pointee = buffer.makeData() as NSData
return { response -> MTDatacenterAddressListData! in
if let config = parse(Buffer(data: response)) {
switch config {
case let .config(_, _, _, _, dcOptions, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _):
var addressList = [MTDatacenterAddress]()
for option in dcOptions {
switch option {
case let .dcOption(flags, id, ipAddress, port) where id == datacenterId:
let preferForMedia = (flags & (1 << 1)) != 0
addressList.append(MTDatacenterAddress(ip: ipAddress, port: UInt16(port), preferForMedia: preferForMedia, restrictToTcp: false))
break
default:
break
}
}
return MTDatacenterAddressListData(addressList: addressList)
}
}
return nil
}
}
}