public static fireAll ( string eventname ) : void | ||
eventname | string | |
return | void |
public void _onConnectionState(ConnectState state) { KBEngine.Event.deregisterIn(this); bool success = (state.error == "" && valid()); if (success) { Dbg.DEBUG_MSG(string.Format("NetworkInterfaceBase::_onConnectionState(), connect to {0} is success!", state.socket.RemoteEndPoint.ToString())); _packetReceiver = createPacketReceiver(); _packetReceiver.startRecv(); connected = true; } else { reset(); Dbg.ERROR_MSG(string.Format("NetworkInterfaceBase::_onConnectionState(), connect error! ip: {0}:{1}, err: {2}", state.connectIP, state.connectPort, state.error)); } Event.fireAll("onConnectionState", new object[] { success }); if (state.connectCB != null) { state.connectCB(state.connectIP, state.connectPort, success, state.userData); } }
public bool connect(string ip, int port) { int count = 0; Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))"); if (rx.IsMatch(ip)) { } else { IPHostEntry ipHost = Dns.GetHostEntry(ip); ip = ipHost.AddressList[0].ToString(); } __RETRY: reset(); TimeoutObject.Reset(); // Security.PrefetchSocketPolicy(ip, 843); socket_ = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket_.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, MemoryStream.BUFFER_MAX); try { IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ip), port); socket_.BeginConnect(endpoint, new AsyncCallback(connectCB), socket_); if (TimeoutObject.WaitOne(10000)) { } else { reset(); } } catch (Exception e) { Dbg.WARNING_MSG(e.ToString()); if (count < 3) { Dbg.WARNING_MSG("connect(" + ip + ":" + port + ") is error, try=" + (count++) + "!"); goto __RETRY; } return(false); } if (!valid()) { Event.fireAll("onConnectStatus", new object[] { false }); return(false); } Event.fireAll("onConnectStatus", new object[] { true }); return(true); }
public void close() { if (_socket != null) { _socket.Close(0); _socket = null; Event.fireAll("onDisconnected", new object[] {}); } _socket = null; }
public void close() { if (socket_ != null && socket_.Connected) { socket_.Close(0); socket_ = null; Event.fireAll("onDisableConnect", new object[] {}); } socket_ = null; }
public virtual void close() { if (_socket != null) { _socket.Close(0); _socket = null; Event.fireAll(EventOutTypes.onDisconnected); } _socket = null; connected = false; }
public void close() { if (_socket != null) { _socket.Close(0); _socket = null; Event.fireAll("onDisableConnect", new object[] {}); } if (_udpSocket != null) { _udpSocket.Close(0); _udpSocket = null; } _socket = null; _udpSocket = null; }
public void send(byte[] datas) { if (socket_ == null || socket_.Connected == false) { throw new ArgumentException("invalid socket!"); } if (datas == null || datas.Length == 0) { throw new ArgumentException("invalid datas!"); } try { socket_.Send(datas); } catch (SocketException err) { if (err.ErrorCode == 10054 || err.ErrorCode == 10053) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::send(): disable connect!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; Event.fireAll("onDisableConnect", new object[] {}); } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::send(): socket error(" + err.ErrorCode + ")!")); } } }
public void _onConnectStatus(string error) { KBEngine.Event.deregisterIn(this); bool success = (error == ""); if (success) { Dbg.INFO_MSG(string.Format("NetworkInterface::_onConnectStatus(), connected to {0}", sock().RemoteEndPoint.ToString())); _packetReceiver = new PacketReceiver(this); _packetReceiver.startRecv(); } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::_onConnectStatus(), connect is error! ip: {0}:{1}, err: {2}", _connectIP, _connectPort, error)); } Event.fireAll("onConnectStatus", new object[] { success }); if (_connectCB != null) { _connectCB(_connectIP, _connectPort, success, _userData); } }
public void recv() { if (socket_ == null || socket_.Connected == false) { throw new ArgumentException("invalid socket!"); } if (socket_.Poll(100000, SelectMode.SelectRead)) { if (socket_ == null || socket_.Connected == false) { Dbg.WARNING_MSG("invalid socket!"); return; } int successReceiveBytes = 0; try { successReceiveBytes = socket_.Receive(_datas, MemoryStream.BUFFER_MAX, 0); } catch (SocketException err) { if (err.ErrorCode == 10054 || err.ErrorCode == 10053) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): disable connect!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::recv(): socket error(" + err.ErrorCode + ")!")); } Event.fireAll("onDisableConnect", new object[] {}); return; } if (successReceiveBytes > 0) { // Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): size={0}!", successReceiveBytes)); } else if (successReceiveBytes == 0) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): disable connect!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; Event.fireAll("onDisableConnect", new object[] {}); } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::recv(): socket error!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; Event.fireAll("onDisableConnect", new object[] {}); return; } msgReader.process(_datas, (MessageLength)successReceiveBytes); } }