示例#1
0
        private void HandleRecvQueue()
        {
            recvQueue.Switch();
            while (!recvQueue.Empty())
            {
                var recvBufferRaw = recvQueue.Pop();
                int ret           = kcpObject.Input(recvBufferRaw, recvBufferRaw.Length);

                //收到的不是一个正确的KCP包
                if (ret < 0)
                {
                    string str = System.Text.Encoding.UTF8.GetString(recvBufferRaw, 0, recvBufferRaw.Length);
                    DLog.LogFormat("收到了错误的kcp包: {0}", str);
                    return;
                }

                needKcpUpdateFlag = true;

                for (int size = kcpObject.PeekSize(); size > 0; size = kcpObject.PeekSize())
                {
                    var recvBuffer = new byte[size];
                    int treclen    = kcpObject.Recv(recvBuffer, recvBuffer.Length);
                    if (treclen > 0)
                    {
                        PushRecData(recvBuffer, treclen);
                    }
                }
            }
        }
示例#2
0
 public void BeginReceive()
 {
     try
     {
         SocketError errorCode = SocketError.Success;
         var         ar        = socket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, out errorCode, recCallback, null);
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("TCP Rec Error.{0}", erro);
     }
 }
示例#3
0
 override public bool Send(byte[] pBuffer, int pSize)
 {
     try
     {
         var ar = mSocket.BeginSendTo(pBuffer, 0, pSize, SocketFlags.None, mTargetPoint, sendCallBack, pBuffer);
         return(true);
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("UDP Send Error.{0}", erro);
     }
     return(false);
 }
示例#4
0
 private void HandleKcpSend(byte[] buff, int size)
 {
     if (testServer == null || tarpoint == null)
     {
         return;
     }
     try
     {
         var ar = testServer.BeginSendTo(buff, 0, size, SocketFlags.None, tarpoint, SendAsyncCallback, buff);
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("KCP Send Error.{0}", erro);
     }
 }
示例#5
0
 private void HandleKcpSend(byte[] buff, int size)
 {
     if (mSocket == null)
     {
         return;
     }
     try
     {
         var ar = mSocket.BeginSendTo(buff, 0, size, SocketFlags.None, mTargetPoint, sendCallBack, buff);
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("KCP Send Error.{0}", erro);
     }
 }
示例#6
0
 public void BeginSend(byte[] buffer, int offset, int size)
 {
     try
     {
         SocketError errorCode = SocketError.Success;
         var         ar        = socket.BeginSend(buffer, offset, size, SocketFlags.None, out errorCode, sendCallback, buffer);
         if (errorCode != SocketError.Success)
         {
             DLog.LogErrorFormat("TCP Send Error.{0}", errorCode);
         }
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("TCP Send Error.{0}", erro);
     }
 }
示例#7
0
 override public bool Send(byte[] pBuffer, int pSize)
 {
     if (mSocket == null)
     {
         return(false);
     }
     try
     {
         SocketError errorCode = SocketError.Success;
         var         ar        = mSocket.BeginSend(pBuffer, 0, pSize, SocketFlags.None, out errorCode, sendCallBack, pBuffer);
         if (errorCode != SocketError.Success)
         {
             DLog.LogErrorFormat("TCP Send Error.{0}", errorCode);
         }
         return(errorCode == SocketError.Success);
     }
     catch (System.Exception erro)
     {
         DLog.LogFormat("TCP Send Error.{0}", erro);
     }
     return(false);
 }
示例#8
0
            private void ReadNetByte()
            {
                FileStream ttempfile = null;

                try
                {
                    long thaveindex = 0;
                    if (File.Exists(TempFile))
                    {
                        if (!mIsClear)
                        {
                            ttempfile  = System.IO.File.OpenWrite(TempFile);
                            thaveindex = ttempfile.Length;
                            ttempfile.Seek(thaveindex, SeekOrigin.Current);
                        }
                        else
                        {
                            File.Delete(TempFile);
                            thaveindex = 0;
                        }
                    }

                    mReqest         = (HttpWebRequest)HttpWebRequest.Create(SourceURL);
                    mReqest.Timeout = 5000;

                    if (SourceURL.Contains("https://"))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    }

                    if (thaveindex > 0)
                    {
                        mReqest.AddRange((int)thaveindex);
                    }

                    int tindex = 0;
                    while (tindex++ < 4 && mThreadRuning)
                    {
                        try
                        {
                            mResponse = mReqest.GetResponse();
                            break;
                        }
                        catch (System.Exception _error)
                        {
                            DLog.LogFormat("ReadNetByte 获取Response失败,尝试次数{0},error = {1}", tindex, _error);
                        }
                    }

                    if (mResponse == null)
                    {
                        throw new System.NullReferenceException("ReadNetByte 获取Response失败.");
                    }

                    mHttpStream   = mResponse.GetResponseStream();
                    ContentLength = mResponse.ContentLength;

                    if (ttempfile == null)
                    {
                        ttempfile = System.IO.File.Create(TempFile);
                    }

                    int    tcount    = 0;
                    int    tlen      = 1024;
                    byte[] tbuffer   = new byte[tlen];
                    int    tReadSize = 0;
                    tReadSize = mHttpStream.Read(tbuffer, 0, tlen);
                    while (tReadSize > 0 && mThreadRuning)
                    {
                        DownLoadedLength += tReadSize;
                        ttempfile.Write(tbuffer, 0, tReadSize);
                        tReadSize = mHttpStream.Read(tbuffer, 0, tlen);

                        if (++tcount >= 512)
                        {
                            ttempfile.Flush();
                            tcount = 0;
                        }
                    }
                }
                catch (System.Exception _error)
                {
                    Error = _error.ToString();
                }

                if (ttempfile != null)
                {
                    ttempfile.Close();
                }

                if (DownLoadedLength == ContentLength)
                {
                    if (File.Exists(TempFile))
                    {
                        if (File.Exists(CompleteFile))
                        {
                            File.Delete(CompleteFile);
                        }
                        File.Move(TempFile, CompleteFile);
                    }
                }
                else
                {
                    if (Error == null)
                    {
                        Error = "文件未能完成下载.Stream 被中断.";
                    }
                }

                CloseHttpClient();
                mIsDone = true;
            }