Example #1
0
        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--;
                }
            }
        }
Example #2
0
 /// <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;
     }
 }
Example #3
0
        public byte[] GetNextData()
        {
            PackageOut pkg = packages.Dequeue();

            return(Encode(pkg.ToByteArray()));
        }
Example #4
0
 public static void Send(string gm)
 {
     NetJZWL.PackageOut packageOut = NetworkManager.instance.NewPackagetOut(MessageId.GM_CMD);
     packageOut.PushString(gm);
     NetworkManager.instance.Send(packageOut);
 }