public void OnRecevie(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { this.remain_bytes = transffered; int src_position = offset; while (this.remain_bytes > 0) { bool completed = false; if (this.current_position < Defines.HEADERSIZE) { this.positionToRead = Defines.HEADERSIZE; completed = Read(buffer, ref src_position, offset, transffered); if (completed == false) { return; } this.messageSize = GetBodySize(); if (this.messageSize == 0) { break; } this.positionToRead = this.messageSize + Defines.HEADERSIZE; } completed = Read(buffer, ref src_position, offset, transffered); if (completed) { callback(this.messageBuffer); ClearBuffer(); } } }
public void on_receive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { this.remain_bytes = transffered; int src_position = offset; while (this.remain_bytes > 0) { bool completed = false; if (this.current_position < Defines.HEADERSIZE) { this.position_to_read = Defines.HEADERSIZE; completed = read_until(buffer, ref src_position, offset, transffered); if (!completed) { return; } this.message_size = get_body_size(); this.position_to_read = this.message_size + Defines.HEADERSIZE; } completed = read_until(buffer, ref src_position, offset, transffered); if (completed) { callback(new Const <byte[]>(this.message_buffer)); clear_buffer(); } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때 마다 호출된다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="transffered"></param> public void OnReceive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { // 이번 receive로 읽어오게 될 바이트 수. remainBytes = transffered; // 원본 버퍼의 포지션값. // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 그 처리를 위한 변수이다. int srcPosition = offset; // 남은 데이터가 있다면 계속 반복한다. while (remainBytes > 0) { bool completed = false; PacketHeader header = new PacketHeader(); // 헤더만큼 못읽은 경우 헤더를 먼저 읽는다. if (currentPosition < PacketHeader.HEADER_SIZE) { // 목표 지점 설정(헤더 위치까지 도달하도록 설정). positionToRead = PacketHeader.HEADER_SIZE; completed = ReadUntil(buffer, ref srcPosition, offset, transffered); if (!completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. header = PacketHeaderConverter.BytesToHeader(messageBuffer); messageSize = header.PacketSize; //MessageSize(); //get_body_size(); // 다음 목표 지점(헤더 + 메시지 사이즈). this.positionToRead = messageSize + PacketHeader.HEADER_SIZE; } // 메시지를 읽는다. completed = ReadUntil(buffer, ref srcPosition, offset, transffered); if (completed) { // 패킷 하나를 완성 했다. byte[] messagePacket = new byte[header.PacketSize]; Buffer.BlockCopy(messageBuffer, header.HeaderSize, messagePacket, 0, header.PacketSize); callback(header, messagePacket); clear_buffer(); } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때 마다 호출된다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="transffered"></param> public void on_receive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { // 이번 receive로 읽어오게 될 바이트 수. this.remain_bytes = transffered; if (message_buffer[0] != 0) { transffered += current_position; } // 원본 버퍼의 포지션값. // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 그 처리를 위한 변수이다. int src_position = offset; // 남은 데이터가 있다면 계속 반복한다. while (this.remain_bytes > 0) { bool completed = false; // 헤더만큼 못읽은 경우 헤더를 먼저 읽는다. if (this.current_position < Defines.HEADERSIZE) { // 목표 지점 설정(헤더 위치까지 도달하도록 설정). this.position_to_read = Defines.HEADERSIZE; completed = read_until(buffer, ref src_position, offset, transffered); if (!completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. this.message_size = get_body_size(); // 다음 목표 지점(헤더 + 메시지 사이즈). this.position_to_read = this.message_size + Defines.HEADERSIZE; } // 메시지를 읽는다. completed = read_until(buffer, ref src_position, offset, transffered); if (completed) { // 패킷 하나를 완성 했다. callback(new Const <byte[]>(this.message_buffer)); clear_buffer(); } } }
// 데이터가 도착했을 때 이를 읽어주는 메소드. internal void OnReceive(byte[] buffer, int offset, int bytesTransferred, CompletedMessageCallback callback) { Console.WriteLine($"{System.Reflection.MethodBase.GetCurrentMethod().Name} Function Entry"); // 이번 receive로 읽어오게 될 바이트 수. remainBytes = bytesTransferred; // 원본 버퍼의 포지션 값. // 패킷의 여러 개 뭉쳐서 올 경우 원본 버퍼의 포지션을 계속 앞으로 밀어주기 위함. int srcPosition = offset; while (remainBytes > 0) { var isCompleted = false; var header = new PacketHeader(); if (currentPosition < Defines.HeaderSize) { positionToRead = Defines.HeaderSize; isCompleted = ReadUntil(buffer, ref srcPosition, offset, bytesTransferred); if (isCompleted == false) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 헤더 하나를 온전히 읽어왔으므로 메시지의 사이를 구한다. header = GetPacketHeader(); messageSize = header.BodySize; // 다음 목표 지점 (헤더 + 메시지 사이즈) positionToRead = messageSize + Defines.HeaderSize; } isCompleted = ReadUntil(buffer, ref srcPosition, offset, bytesTransferred); if (isCompleted) { // 패킷 하나를 완성시킨다. byte[] cloneBuffer = new byte[positionToRead - Defines.HeaderSize]; Array.Copy(messageBuffer, Defines.HeaderSize, cloneBuffer, 0, positionToRead - Defines.HeaderSize); ClearBuffer(); callback(header.PacketId, new ArraySegment <byte>(cloneBuffer, 0, cloneBuffer.Length)); } } }
/// <summary> /// 소켓 버퍼로 부터 데이터를 수신할 때 마다 호출됩니다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="transffered"></param> /// <param name="callback"></param> public void on_receive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { // 이번 receive로 읽어온 바이트 수 this.remain_bytes = transffered; // 원본 버퍼의 포지션 값 // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 // 그 처리를 위한 변수입니다. int src_position = offset; // 남은 데이터가 있다면 계속 반복합니다. while (this.remain_bytes > 0) { bool completed = false; // 헤더 만큼 못읽은 경우 헤더를 먼저 읽는다. if (this.current_position < Defines.HEADERSIZE) { this.position_to_read = Defines.HEADERSIZE; completed = read_until(buffer, ref src_position, offset, transffered); if (!completed) { return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. this.message_size = get_body_size(); // 다음 목표 지점 (헤더 + 메시지 사이즈) this.position_to_read = this.message_size + Defines.HEADERSIZE; } // 메시지를 읽는다. completed = read_until(buffer, ref src_position, offset, transffered); if (completed) { callback(new Const <byte[]>(this.message_buffer)); clear_buffer(); } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때마다 호출된다. /// 데이터가 남아 있을 때까지 계속 패킷을 만들어 callback을 호출해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> public void OnReceive(byte[] buffer, int offset, int transferred, CompletedMessageCallback callback) { // 이번 receive로 읽어오게 될 바이트 수. _remainBytes = transferred; // 원본 버퍼의 포지션값. // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 그 처리를 위한 변수이다. int srcPosition = offset; // 남은 데이터가 있다면 계속 반복한다. while (_remainBytes > 0) { bool completed = false; // 헤더만큼 못읽은 경우 헤더를 먼저 읽는다. if (_currentPosition < Defines.HEADERSIZE) { // 목표 지점 설정(헤더 위치까지 도달하도록 설정). _positionToRead = Defines.HEADERSIZE; completed = ReadUntil(buffer, ref srcPosition); if (!completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. _messageSize = GetTotalMessageSize(); // 메시지 사이즈가 0이하라면 잘못된 패킷으로 처리한다. // It was wrong message if size less than zero. if (_messageSize <= 0) { ClearBuffer(); return; } // 다음 목표 지점. // 패킷 헤더에 기록된 body size = 헤더길이를 제외한 메시지 사이즈. // 따라서 패킷 전체 길이를 알려면 헤더길이를 더해줘야 함. _positionToRead = _messageSize + Defines.HEADERSIZE; // 헤더를 다 읽었는데 더이상 가져올 데이터가 없다면 다음 receive를 기다린다. // (예를들어 데이터가 조각나서 헤더만 오고 메시지는 다음번에 올 경우) if (_remainBytes <= 0) { return; } } // 메시지를 읽는다. completed = ReadUntil(buffer, ref srcPosition); if (completed) { // 패킷 하나를 완성 했다. byte[] clone = new byte[_positionToRead]; Array.Copy(_messageBuffer, clone, _positionToRead); ClearBuffer(); callback(new ArraySegment <byte>(clone, 0, _positionToRead)); } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때 마다 호출된다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="_buffer"></param> /// <param name="_offset"></param> /// <param name="_transferred"></param> public void ReadReceive(byte[] _buffer, int _offset, int _transferred, CompletedMessageCallback _callback) { Console.WriteLine(this + " ReadReceive\r\n _buffer:{0} _offset:{1} _transferred:{2}\r\n _callback(받은메세지를 이콜백으로 처리함)", _buffer, _offset, _transferred); // 이번 receive로 읽어오게 될 바이트 수. this.remainBytes = _transferred; // 원본 버퍼의 포지션값. // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 그 처리를 위한 변수이다. int _offset2 = _offset; // 남은 데이터가 있다면 계속 반복한다. while (this.remainBytes > 0) { Console.WriteLine(" > _offset:{0} _offset2:{1} _transferred:{2}\r\n" + " > messageOffset:{3} remainBytes:{4} messageToRead:{5} messageSize:{6}", _offset, _offset2, _transferred, messageOffset, remainBytes, messageToRead, messageSize); bool _completed = false; // 헤더만큼 못읽은 경우 헤더를 먼저 읽는다. if (this.messageOffset < Defines.HEADERSIZE) { // 목표 지점 설정(헤더 위치까지 도달하도록 설정). this.messageToRead = Defines.HEADERSIZE; Console.WriteLine(" > (헤더읽기) messageOffset:{0} messageToRead:{1}", messageOffset, messageToRead); _completed = ReadUtil(_buffer, ref _offset2, _offset, _transferred); if (!_completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. Console.WriteLine(" > (**** 헤더아직 안들어와서 리턴 *****"); return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. this.messageSize = GetBodySize(); Console.WriteLine(" > 해더만읽고 messageSize:{0}", messageSize); // 다음 목표 지점(헤더 + 메시지 사이즈). this.messageToRead = Defines.HEADERSIZE + this.messageSize; Console.WriteLine(" > messageSize:{0} messageToRead:{1}", messageSize, messageToRead); } // 메시지를 읽는다. _completed = ReadUtil(_buffer, ref _offset2, _offset, _transferred); if (_completed) { Console.WriteLine(" > 메세지를 정상읽음 > 메세지 처리하러가기~~~"); // 패킷 하나를 완성 했다. _callback(new Const <byte[]>(this.messageBuffer)); ClearBuffer(); } else { Console.WriteLine(" > 메세지를 비정상 ****> 자동대기중~~~"); } } }
public void ReadReceive(byte[] _buffer, int _offset, int _transferred, CompletedMessageCallback _onParseCode) { if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(this + " ReadReceive\r\n _buffer:{0} _offset:{1} _transferred:{2}\r\n _callback(받은메세지를 이콜백으로 처리함)", _buffer, _offset, _transferred); } remainBytes = _transferred; int _offset2 = _offset; bool _completed = false; while (remainBytes > 0) { if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > _offset:{0} _offset2:{1} _transferred:{2}\r\n" + " > messageOffset:{3} remainBytes:{4} messageToRead:{5} messageSize:{6}", _offset, _offset2, _transferred, messageOffset, remainBytes, messageToRead, messageSize); } _completed = false; if (messageOffset < Protocol.HEADER_SIZE) { messageToRead = Protocol.HEADER_SIZE; if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > (헤더읽기) messageOffset:{0} messageToRead:{1}", messageOffset, messageToRead); } _completed = ReadUtil(_buffer, ref _offset2, _offset, _transferred); if (!_completed) { if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > (**** 헤더아직 안들어와서 리턴 *****"); } return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. messageSize = Util.GetShort(messageBuffer, 0); if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > 해더만읽고 messageSize:{0}", messageSize); } // 다음 목표 지점(헤더 + 메시지 사이즈). messageToRead = Protocol.HEADER_SIZE + messageSize; if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > messageSize:{0} messageToRead:{1}", messageSize, messageToRead); } } // 메시지를 읽는다. _completed = ReadUtil(_buffer, ref _offset2, _offset, _transferred); if (_completed) { if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > 메세지를 정상읽음 > 메세지 처리하러가기~~~"); } // 패킷 하나를 완성 했다. _onParseCode(messageBuffer, messageSize); ClearBuffer(); } else { if (Protocol.DEBUG_PACKET_RECEIVE) { Console.WriteLine(" > 메세지를 비정상 ****> 자동대기중~~~"); } } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때 마다 호출된다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="transffered"></param> public void on_receive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { // 이번 receive로 읽어오게 될 바이트 수. this.remain_bytes = transffered; // 원본 버퍼의 포지션값. // 패킷이 여러개 뭉쳐 올 경우 원본 버퍼의 포지션은 계속 앞으로 가야 하는데 그 처리를 위한 변수이다. int src_position = offset; // 남은 데이터가 있다면 계속 반복한다. while (this.remain_bytes > 0) { bool completed = false; // 헤더만큼 못읽은 경우 헤더를 먼저 읽는다. if (this.current_position < Defines.HEADERSIZE) { // 목표 지점 설정(헤더 위치까지 도달하도록 설정). this.position_to_read = Defines.HEADERSIZE; completed = read_until(buffer, ref src_position); if (!completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 헤더 하나를 온전히 읽어왔으므로 메시지 사이즈를 구한다. this.message_size = get_total_message_size(); // 메시지 사이즈가 0이하라면 잘못된 패킷으로 처리한다. // It was wrong message if size less than zero. if (this.message_size <= 0) { clear_buffer(); return; } // 다음 목표 지점. this.position_to_read = this.message_size; // 헤더를 다 읽었는데 더이상 가져올 데이터가 없다면 다음 receive를 기다린다. // (예를들어 데이터가 조각나서 헤더만 오고 메시지는 다음번에 올 경우) if (this.remain_bytes <= 0) { return; } } // 메시지를 읽는다. completed = read_until(buffer, ref src_position); if (completed) { // 패킷 하나를 완성 했다. byte[] clone = new byte[this.position_to_read]; Array.Copy(this.message_buffer, clone, this.position_to_read); clear_buffer(); callback(new ArraySegment <byte>(clone, 0, this.position_to_read)); } } }
/// <summary> /// 소켓 버퍼로부터 데이터를 수신할 때 마다 호출된다. /// 데이터가 남아 있을 때 까지 계속 패킷을 만들어 callback을 호출 해 준다. /// 하나의 패킷을 완성하지 못했다면 버퍼에 보관해 놓은 뒤 다음 수신을 기다린다. /// </summary> /// <param name="buffer">받은 버퍼 데이터</param> /// <param name="offset">시작 위치</param> /// <param name="transffered">데이터 길이</param> public void on_receive(byte[] buffer, int offset, int transffered, CompletedMessageCallback callback) { /* 1. transffered는 클라이언트에서 보내는 길이 * 2. Data Length는 중간에 받는 위치 값이 있음 * 3. 조건 * a. 데이터가 정상적으로 잘 넘어 온 경우 * b. 데이터가 비상적으로 넘어 온 경우 * c. 데이터가 짧게 짤려서 넘어오는 경우 */ this.transffered_length = Convert.ToInt32(transffered); // 전달 된 길이 저장 int src_position = offset; // 시작 위치 저장 // 짤려서 올 경우를 대비해서 반복 처리 while (this.transffered_length > 0) { bool completed = false; // 1. 현재 위치가 헤더보다 작을 경우 if (this.current_position < Defines.HEADERSIZE) { this.position_to_read = Defines.HEADERSIZE; completed = read_until(buffer, ref src_position); if (!completed) { // 아직 다 못읽었으므로 다음 receive를 기다린다. return; } // 못해도 헤더는 다 읽음. this.message_size = get_total_message_size(); // 메시지 사이즈가 0이하라면 잘못된 패킷으로 처리한다. // It was wrong message if size less than zero. if (this.message_size <= 0) { clear_buffer(); return; } // 다음 목표 지점은 메시지 사이즈 만큼 this.position_to_read = this.message_size; // 헤더를 다 읽었는데 더이상 가져올 데이터가 없다면 다음 receive를 기다린다. // (예를들어 데이터가 조각나서 헤더만 오고 메시지는 다음번에 올 경우) if (this.transffered_length <= 0) { return; } } // 메시지를 읽는다. completed = read_until(buffer, ref src_position); if (completed) { // 패킷 하나를 완성 했다. byte[] clone = new byte[this.position_to_read]; Array.Copy(this.message_buffer, clone, this.position_to_read); clear_buffer(); callback(new ArraySegment <byte>(clone, 0, this.position_to_read)); } } }