Ejemplo n.º 1
0
 public void NotifyBase(NotifyType notifyType, Socket socket, BaseInfo receiveInfo)
 {
 }
Ejemplo n.º 2
0
        private void OnDataReceived()
        {
            try
            {
                while (_IsEndClientListener == false)
                {
                    byte[] receiveData = new byte[_ClientSocket.ReceiveBufferSize];
                    int    iRx         = _ClientSocket.Receive(receiveData);

                    MemoryStream ms = new MemoryStream(receiveData, false);
                    BinaryReader br = new BinaryReader(ms);

                    while (iRx > 0)
                    {
                        if (_CompleteBuffer == null)
                        {
                            HeaderInfo headerInfo = (HeaderInfo)BaseInfo.CreateInstance(br);

                            if (headerInfo == null)
                            {
                                break;
                            }

                            if (headerInfo._Header != HeaderInfo.Marker)
                            {
                                break;
                            }

                            _CompleteCount = headerInfo._BodySize;
                            _ReceiveCount  = iRx - headerInfo.GetSize();

                            if (_CompleteCount > _ReceiveCount)
                            {
                                _CompleteBuffer = new byte[_CompleteCount + 1];

                                for (int i = 0; i < _ReceiveCount; i++)
                                {
                                    _CompleteBuffer[i] = br.ReadByte();
                                }

                                break;
                            }

                            iRx -= headerInfo.GetSize();
                            iRx -= _CompleteCount;
                        }
                        else
                        {
                            int readCount = _CompleteCount - _ReceiveCount;

                            if (readCount > iRx)
                            {
                                readCount = iRx;
                            }

                            for (int i = 0; i < readCount; i++)
                            {
                                _CompleteBuffer[_ReceiveCount + i] = br.ReadByte();
                            }

                            _ReceiveCount += readCount;

                            if (_CompleteCount > _ReceiveCount)
                            {
                                break;
                            }

                            iRx -= readCount;
                        }

                        if (_CompleteBuffer != null)
                        {
                            MemoryStream memoryStream = new MemoryStream(_CompleteBuffer, false);
                            BinaryReader binaryReader = new BinaryReader(memoryStream);

                            HandleEvent(binaryReader);

                            binaryReader.Close();
                            memoryStream.Close();
                        }
                        else
                        {
                            HandleEvent(br);
                        }
                    }
                    br.Close();
                    ms.Close();
                }
            }
            catch (SocketException e)
            {
                _NotifyHandler(NotifyType.Notify_Socket, null, null);
            }
        }