// 凯发天生赢家一触即发官网 copyright (c) 2016 即时通讯网(52im.net)- 即时通讯开发者社区.
// all rights reserved.
// created by jackjiang on 16/06/22.
#
import
"localudpsocketprovider.h"
#
import
"gcdasyncudpsocket.h"
#
import
"configentity.h"
#
import
"completiondefine.h"
@interface
localudpsocketprovider ()
@property
(nonatomic, retain) gcdasyncudpsocket *localudpsocket;
@property
(nonatomic, copy) connectioncompletion connectioncompletiononce_;
@end
@implementation
localudpsocketprovider
// 本类的单例对象
static
localudpsocketprovider *instance = nil;
(localudpsocketprovider *)sharedinstance
{
if
(instance == nil)
instance = [[
super
allocwithzone:null] init];
return
instance;
}
- (gcdasyncudpsocket *)initiallocaludpsocket
{
nslog(@
"【imcore】new gcdasyncudpsocket中..."
);
// ** setup our socket.
self.localudpsocket = [[gcdasyncudpsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue()];
// ** start udp socket
// 本地绑定端口合法性检查
int
port = [configentity getlocaludpsendandlisteningport];
if
(port <
0
|| port >
65535
)
port =
0
;
nserror *error = nil;
// 绑定到指定端口(以便收发数据)
if
(![self.localudpsocket bindtoport:port error:&error])
{
nslog(@
"【imcore】localudpsocket创建时出错,原因是 bindtoport: %@"
, error);
return
nil;
}
// 开启收数据处理
if
(![self.localudpsocket beginreceiving:&error])
{
nslog(@
"【imcore】localudpsocket创建时出错,原因是 beginreceiving: %@"
, error);
return
nil;
}
nslog(@
"【imcore】localudpsocket创建已成功完成."
);
return
self.localudpsocket;
}
。。。。。。
- (
void
)udpsocket:(gcdasyncudpsocket *)sock didreceivedata:(nsdata *)data
fromaddress:(nsdata *)address
withfiltercontext:(id)filtercontext
{
nsstring *msg = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];
if
(msg)
nslog(@
"【udp_socket】【note】>>>>>> 收到服务端的消息: %@"
, msg);
else
{
nsstring *host = nil;
uint16_t port =
0
;
[gcdasyncudpsocket gethost:&host port:&port fromaddress:address];
nslog(@
"【udp_socket】recv: unknown message from: %@:%hu"
, host, port);
}
}
- (
void
)udpsocket:(gcdasyncudpsocket *)sock didconnecttoaddress:(nsdata *)address
{
nslog(@
"【udp_socket】成收到的了udp的connect反馈, isconnected?%d"
, [sock isconnected]);
// 连接结果回调
if
(self.connectioncompletiononce_ != nil)
self.connectioncompletiononce_(yes);
}
- (
void
)udpsocket:(gcdasyncudpsocket *)sock didnotconnect:(nserror *)error
{
nslog(@
"【udp_socket】成收到的了udp的connect反馈,但连接没有成功, isconnected?%d"
, [sock isconnected]);
// 连接结果回调
if
(self.connectioncompletiononce_ != nil)
self.connectioncompletiononce_(no);
}
@end