Exemple #1
0
        public void AddSegment(byte[] segment)
        {
            HeaderInfo headerInfo = PacketDeserializer.ParseHeader(segment);

            if (headerInfo.CourrentSegmentID >= 1000)
            {
                if (segmentCollecters.Count > 0)
                {
                    segmentCollecters.Clear();
                }

                if (LastCollecter == null)
                {
                    LastCollecter = new SegmentCollecter(headerInfo.SegmentCount, headerInfo.CourrentSegmentID);
                    LastCollecter.OnDataInvoke += PacketReciver_OnDataInvoke;
                }
                LastCollecter.AddSegment(segment, PacketHeaderSize.HeaderSize);
                return;
            }

            if (headerInfo.CourrentSegmentID == 1)
            {
                LastCollecter = null;
            }

            if (segmentCollecters.ContainsKey(headerInfo.CourrentSegmentID))
            {
                segmentCollecters[headerInfo.CourrentSegmentID].AddSegment(segment, PacketHeaderSize.HeaderSize);
            }
            else
            {
                SegmentCollecter segmentCollecter = new SegmentCollecter(headerInfo.SegmentCount, headerInfo.CourrentSegmentID);
                segmentCollecter.OnDataInvoke += PacketReciver_OnDataInvoke;

                segmentCollecter.AddSegment(segment, PacketHeaderSize.HeaderSize);
                segmentCollecters.Add(headerInfo.CourrentSegmentID, segmentCollecter);
            }
        }
        public int ReadStream()
        {
            if (TcpClient.Connected)
            {
                if (0 < TcpClient.Available)
                {
                    ReadNetworkStreamToReadBuffer(0, PacketHeaderSize.HeaderSize);
                    HeaderInfo headerInfo = PacketDeserializer.ParseHeader(ReadBuffer);
                    ReadNetworkStreamToReadBuffer(PacketHeaderSize.HeaderSize, headerInfo.SegmentLength);

                    return(PacketHeaderSize.HeaderSize + headerInfo.SegmentLength);
                }
                else
                {
                    return(-2);
                }
            }
            else
            {
                Dispose();
                return(-1);
            }
        }
Exemple #3
0
        private void InitSocket()
        {
            while (!StopTask)
            {
                if (tcpClient.Connected)
                {
                    if (0 < tcpClient.Available)
                    {
                        ReadNetworkStreamToReadBuffer(0, PacketHeaderSize.HeaderSize);
                        HeaderInfo headerInfo = PacketDeserializer.ParseHeader(ReadBuffer);
                        ReadNetworkStreamToReadBuffer(PacketHeaderSize.HeaderSize, headerInfo.SegmentLength);

                        OnReceiveData(ReadBuffer, PacketHeaderSize.HeaderSize + headerInfo.SegmentLength);
                    }
                }
                else
                {
                    StopTask = true;
                }
            }
            tcpClient.Close();
            networkStream.Close();
            networkStream.Dispose();
        }