/// <summary> /// 连接成功,建立接受线程 /// </summary> /// <param name="iar"></param> private void _onConnect_Sucess(IAsyncResult iar) { try { Socket client = (Socket)iar.AsyncState; client.EndConnect(iar); receiveThread = new Thread(new ThreadStart(_onReceiveSocket)); receiveThread.IsBackground = true; receiveThread.Start(); _isConnected = true; SGNFDebug.Log("InfoServer connected!"); SGNFDebug.Log("Requesting ScenarioServer list..."); byte[] rawData = SocketUtil.ISSerial(new ISSocketModel() { Command = (int)SocketUtil.InternalCommand.SSINFO, }); Instance.SendMsgBase(rawData); } catch (Exception _e) { Close(); SGNFDebug.ExceptionCaught(_e); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="_currIP"></param> /// <param name="_currPort"></param> internal void Connect(string _currIP, int _currPort) { if (!IsConnceted) { try { //创建套接字 clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //解析IP地址 IPAddress ipAddress = IPAddress.Parse(_currIP); IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, _currPort); //异步连接 IAsyncResult result = clientSocket.BeginConnect(ipEndpoint, new AsyncCallback(_onConnect_Sucess), clientSocket); bool success = result.AsyncWaitHandle.WaitOne(5000, true); if (!success) { //超时 Close(); } } catch (Exception _e) { //失败 Close(); SGNFDebug.ExceptionCaught(_e); } } }
/// <summary> /// 发送消息结果回调,可判断当前网络状态 /// </summary> /// <param name="asyncSend"></param> private void _onSendMsg(IAsyncResult asyncSend) { try { Socket client = (Socket)asyncSend.AsyncState; client.EndSend(asyncSend); } catch (Exception e) { SGNFDebug.ExceptionCaught(e); } }
/// <summary> /// 接受网络数据,将接受到的放入消息队列 /// </summary> private void _onReceiveSocket() { while (true) { if (!clientSocket.Connected) { _isConnected = false; break; } try { int receiveLength = clientSocket.Receive(_tmpReceiveBuff); if (receiveLength > 0) { //将收到的数据添加到缓存器中 _databuffer.AddBuffer(_tmpReceiveBuff, receiveLength); SGNFDebug.HEXLog("rcv", _tmpReceiveBuff, receiveLength); //取出一条完整数据 while (_databuffer.GetData(out _socketData)) { ISSocketModel DeData = SocketUtil.ISDeSerial(_socketData); //如果数据属于内部协议 if (Enum.IsDefined(typeof(SocketUtil.InternalCommand), DeData.Command)) { if (DeData.Command == (int)SocketUtil.InternalCommand.NULL) { SGNFDebug.Log("Got NULL from server"); } if (DeData.Command == (int)SocketUtil.InternalCommand.PING) { //Client.RcvPingStr = DeData.Message[0]; } if (DeData.Command == (int)SocketUtil.InternalCommand.SSINFO) { int num = Convert.ToInt32(DeData.Message[0]); if (num > 0) { NetManager.Instance.allSSInfo.Clear(); for (int i = 0; i < num; i++) { NetManager.Instance.allSSInfo.Add(new SocketUtil.SSInfo() { Tag = DeData.Message[i * 3 + 1], IP = DeData.Message[i * 3 + 2], Port = Convert.ToInt32(DeData.Message[i * 3 + 3]), }); } } SGNFDebug.ListLog("Got " + num + " row of SS from server", NetManager.Instance.allSSInfo); } } else { //锁死消息中心消息队列,并添加数据 lock (MessageCenter.Instance.ISMessageDataQueue) { MessageCenter.Instance.ISMessageDataQueue.Enqueue(DeData); } } } } } catch (Exception e) { clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); SGNFDebug.ExceptionCaught(e); break; } } }
/// <summary> /// 接受网络数据,将接受到的放入消息队列 /// </summary> private void _onReceiveSocket() { while (true) { if (!clientSocket.Connected) { _isConnected = false; break; } try { int receiveLength = clientSocket.Receive(_tmpReceiveBuff); if (receiveLength > 0) { //将收到的数据添加到缓存器中 _databuffer.AddBuffer(_tmpReceiveBuff, receiveLength); SGNFDebug.HEXLog("rcv", _tmpReceiveBuff, receiveLength); //取出一条完整数据 while (_databuffer.GetData(out _socketData)) { SSSocketModel DeData = SocketUtil.SSDeSerial(_socketData); //如果数据属于内部协议 if (Enum.IsDefined(typeof(SocketUtil.InternalCommand), DeData.Command)) { if (DeData.Command == (int)SocketUtil.InternalCommand.NULL) { SGNFDebug.Log("Got NULL from server"); } if (DeData.Command == (int)SocketUtil.InternalCommand.PING) { //Client.RcvPingStr = DeData.Message[0]; } if (DeData.Command == (int)SocketUtil.InternalCommand.TICK) { if (DeData.Message.Count >= 1) { NetManager.Instance.tick = DeData.Message[0]; SGNFDebug.Log("Got Tick!Which is " + DeData.Message[0]); } else { SGNFDebug.ExceptionCaught(new Exception("Recieved nothing from TICK package!"));; } } } else { lock (SSRecorder.Instance.RecieveDataBuffer) { SSRecorder.Instance.RecieveDataBuffer.Add(DeData.CurrentTick, DeData); } //锁死消息中心消息队列,并添加数据 lock (MessageCenter.Instance.SSMessageDataQueue) { MessageCenter.Instance.SSMessageDataQueue.Enqueue(DeData); } } } } } catch (Exception e) { clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); SGNFDebug.ExceptionCaught(e); break; } } }