public void Send(PackageOut pkgOut) { if (m_status != SocketStatus.Connected) { return; } try { byte[] data = pkgOut.ToByteArray(); if (NetworkManager.instance.IsDebug) { NetworkManager.instance.Error("pkg send now code " + pkgOut.code + " process " + Thread.CurrentThread.ManagedThreadId.ToString()); } lock (this) { sendCout++; } m_socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(this.SendCallback), m_socket); } catch (Exception ex) { NetworkManager.instance.Error("pkg send error: " + ex.Message); lock (this) { sendCout--; } } }
/// <summary> /// 连接制定ip和端口 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void Connect(string ip, int port) { m_connectStartTime = TimeUtils.CurLocalTimeMilliSecond(); m_status = SocketStatus.Connecting; m_active = false; m_netReader.Clear(); m_netSender.Clear(); m_canTargetEvent = true; PackageOut.ResetIndex(); lock (this) { sendCout = 0; } try { IPAddress ipAddress = IPAddress.Parse(ip); IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, port); m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, SEND_TIME_OUT); m_socket.BeginConnect(ipEndpoint, new AsyncCallback(M_ConnectCallBack), m_socket); } catch (Exception) { Debug.LogError("服务器关闭,请稍后重试"); m_socket.Close(); m_status = SocketStatus.Closed; } }
public byte[] GetNextData() { PackageOut pkg = packages.Dequeue(); return(Encode(pkg.ToByteArray())); }
public static void Send(string gm) { NetJZWL.PackageOut packageOut = NetworkManager.instance.NewPackagetOut(MessageId.GM_CMD); packageOut.PushString(gm); NetworkManager.instance.Send(packageOut); }