예제 #1
0
 public void NotifyBase(NotifyType notifyType, Socket socket, BaseInfo receiveInfo)
 {
 }
예제 #2
0
        public void OnDataReceived(IAsyncResult asyn)//For Server Mode
        {
            KeyValuePair aKeyValuePair = (KeyValuePair)asyn.AsyncState;

            Socket socket = aKeyValuePair._Socket;

            Byte[] buffer = aKeyValuePair._DataBuffer;

            //_NotifyHandler(NotifyType.Notify_notify, socket, null);

            try
            {
                int iRx = socket.EndReceive(asyn);

                MemoryStream ms = new MemoryStream(buffer, 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, socket);

                        binaryReader.Close();
                        memoryStream.Close();
                    }
                    else
                    {
                        HandleEvent(br, socket);
                    }
                }
                br.Close();
                ms.Close();

                WaitForData(socket);
            }
            catch (SocketException e)
            {
                _NotifyHandler(NotifyType.Notify_Socket, socket, null);
            }
        }