//连接 public void connect(string ip, int port) { //连接前先关闭 if (tcpClient != null && tcpClient.Client != null) { tcpClient.Client.Close(); tcpClient.Close(); tcpClient = null; } if (!connected()) { try { setNetStatus(NetParams.LINKING); this.ip = ip; this.port = port; IPAddress address = IPAddress.Parse(ip); var iPEndPoint = new IPEndPoint(address, port); Log.info(this, "-connect() " + "第" + tryNum + "次连接服务器 [" + iPEndPoint + "] [Start]"); tcpClient = new TcpClient(); tcpClient.NoDelay = true; tcpClient.SendTimeout = 3000; //3秒 tcpClient.ReceiveTimeout = 3000; //3秒 tcpClient.BeginConnect(address, port, ConnectCallBack, this); } catch (Exception ex) { NetLog.error(this, "-connect() 连接服务器异常:" + ex); //失败重连 retryConnect(); } } }
private void send(byte[] data) { if (connected()) { /*lock (dataList) * { * //添加数据到发送队列 * dataList.AddLast(data); * if (dataList.Count == 1) * { * //当前即为第一个数据则发送 * sendAnsy(data); * } * }*/ try { Log.info(this, "-Send 成功发送数据 " + data.Length + "bytes: " + " ServerTimestamp:" + ServerTime.Instance.Timestamp); tcpClient.Client.Send(data); } catch (Exception ex) { NetLog.error(this, "-send() 发送数据出错," + ex); close(); } } else { NetLog.error(this, "-send() 网络未连接,关闭网络请重新连接"); close(); } }
//连接回调 //重连 public void retryConnect() { isReceive = false; if (tryNum < tryableNum) { tryNum++; connect(ip, port); } else { NetLog.error(this, "-retryConnect() 请求网络连接失败"); close(); } }
private void SendCallBack(IAsyncResult asyncresult) { var data = asyncresult.AsyncState as byte[]; try { int sendNum = tcpClient.Client.EndSend(asyncresult); sendNext(); } catch (Exception ex) { NetLog.error(this, "-SendCallBack() 发送数据失败" + ex.Message); close(); } }
//异步发送数据 private void sendAnsy(byte[] data) { if (connected()) { try { tcpClient.Client.BeginSend(data, 0, data.Length, SocketFlags.None, SendCallBack, data); } catch (Exception ex) { NetLog.error(this, "-sendAnsy() 发送数据出错," + ex); close(); } } else { NetLog.error(this, "-sendAnsy() 网络未连接,关闭网络请重新连接"); close(); } }
private void ConnectCallBack(IAsyncResult asyncresult) { var net = asyncresult.AsyncState as NetSocket; try { if (net.tcpClient.Client != null && net.tcpClient.Client.Connected) { //连接成功 net.tcpClient.EndConnect(asyncresult); NetLog.info(net, "-ConnectCallBack()" + "第" + net.tryNum + "次连接服务器 [" + net.ip + "] [OK]"); net.startReceive(); net.tryNum = 0; net.msgLen = 0; //重连时,先将缓存的数据清空 lock (dataList) { dataList.Clear(); } net.setNetStatus(NetParams.LINK_OK); //首先发送 var netdata = new MemoryStream(); send(netdata, 1, 0); } else { NetLog.info(net, "-ConnectCallBack()" + "第" + net.tryNum + "次连接服务器 [" + net.ip + "] [Fail]"); net.retryConnect(); } } catch (Exception ex) { NetLog.error(net, "-ConnectCallBack() 连接服务器异常:" + ex); net.retryConnect(); } }
/*public void send(byte framID, byte wayID) * { * byte[] data = new byte[4] { 0, 2, framID, wayID }; * send(data); * * }*/ //帧更新执行 public void Update() { lock (netStatusList) { //检查连接状态变动状态 if (netStatusList.Count > 0) { if (statusCallback != null) { //状态通知 statusCallback(netStatusList.First.Value); netStatusList.RemoveFirst(); } } } if (connected()) { try { //有数据要接收 while (tcpClient.Client.Available >= 2) //仍然有数据可以处理 { //NetLog.warin(this, "收到数据"); if (msgLen == 0) { tcpClient.Client.Receive(headBuffer); msgLen = ByteUtil.Byte2Int(headBuffer); //NetLog.warin(this, "receive data head length: " + this.msgLen); bodyBuffer = new byte[msgLen]; } else { if (tcpClient.Client.Available >= msgLen) //数据完整 { tcpClient.Client.Receive(bodyBuffer); //NetLog.warin(this, "Receive bodyBuffer success"); var netData = new NetData(bodyBuffer); AppNet.main.receiveNetMsg(netData); bodyBuffer = null; msgLen = 0; } else { break; //数据不完整-退出 下次update再处理 } } } } catch (Exception ex) { NetLog.error(this, "-Update() Socket接收数据出错," + ex); close(); } } else { if (netStatus.Equals(NetParams.LINK_OK)) { close(); } } }