#import#import #import #import #import cfsocketref _socket; void* serverthread(void* context); int setupsocket(void); void acceptcallback(cfsocketref socket, cfsocketcallbacktype type, cfdataref address, const void *data, void *info); void sendscreenshots(cfwritestreamref ostream); void sendscreenshots(cfwritestreamref ostream) { cgsize screensize = [[uiscreen mainscreen] bounds].size; uigraphicsbeginimagecontextwithoptions(screensize, yes, 0); cgcontextref context = uigraphicsgetcurrentcontext(); uiwindow * window = [[uiapplication sharedapplication] keywindow]; cgcontextsavegstate(context); cgcontexttranslatectm(context, [window center].x, [window center].y); cgcontextconcatctm(context, [window transform]); cgcontexttranslatectm(context, -[window bounds].size.width*[[window layer] anchorpoint].x, -[window bounds].size.height*[[window layer] anchorpoint].y); [[window layer] renderincontext:context]; cgcontextrestoregstate(context); uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); nsdata* imagedata = uiimagepngrepresentation(image); nsuinteger offset = 0; nsuinteger buff_size = 1024; while(imagedata.length>offset){ nsuinteger buff_len = imagedata.length - offset > buff_size ? buff_size : imagedata.length - offset; cfwritestreamwrite(ostream, imagedata.bytes offset, buff_len); offset = offset buff_len; } uigraphicsendimagecontext(); } void acceptcallback(cfsocketref socket, cfsocketcallbacktype type, cfdataref address, const void *data, void *info) { if (kcfsocketacceptcallback == type) { cfsocketnativehandle nativesockethandle = *(cfsocketnativehandle *)data; uint8_t name[sock_maxaddrlen]; socklen_t namelen = sizeof(name); if (0 != getpeername(nativesockethandle, (struct sockaddr *)name, &namelen)) { close(nativesockethandle); } //nslog(@"%s connected.", inet_ntoa( ((struct sockaddr_in *)name)->sin_addr )); cfwritestreamref ostream; cfstreamcreatepairwithsocket(kcfallocatordefault, nativesockethandle, null, &ostream); if (ostream) { cfwritestreamopen(ostream); sendscreenshots(ostream); cfwritestreamclose(ostream); close(nativesockethandle); } else { close(nativesockethandle); } } } int setupsocket() { _socket = cfsocketcreate(kcfallocatordefault, pf_inet, sock_stream, ipproto_tcp, kcfsocketacceptcallback, acceptcallback, null); if (null == _socket) { return 0; } int optval = 1; setsockopt(cfsocketgetnative(_socket), sol_socket, so_reuseaddr, (void *)&optval, sizeof(optval)); struct sockaddr_in addr4; memset(&addr4, 0, sizeof(addr4)); addr4.sin_len = (__uint8_t)sizeof(addr4); addr4.sin_family = af_inet; addr4.sin_port = htons(2115); addr4.sin_addr.s_addr = htonl(inaddr_any); cfdataref address = cfdatacreate(kcfallocatordefault, (uint8 *)&addr4, sizeof(addr4)); if (kcfsocketsuccess != cfsocketsetaddress(_socket, address)) { if (_socket) cfrelease(_socket); _socket = null; return 0; } cfrunloopref cfrunloop = cfrunloopgetcurrent(); cfrunloopsourceref source = cfsocketcreaterunloopsource(kcfallocatordefault, _socket, 0); cfrunloopaddsource(cfrunloop, source, kcfrunloopcommonmodes); cfrelease(source); return 1; } void* serverthread(void* context) { @autoreleasepool { int res = setupsocket(); if (!res) { return 0; } cfrunlooprun(); return (void*)1; } } int startscreenserver() { pthread_t tid; return pthread_create(&tid, null, serverthread, null); }
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....