public int Receive(byte[] data, int offset, int count) { try { if (!IsConnected()) { Disconnect(); return(-1); } var received = Sock.Receive(data, offset, count, SocketFlags.None); if (received <= 0) { Disconnect(); return(-1); } var dargs = new DataEventArgs(this, data, received); OnDataReceived(this, dargs); return(received); } catch (Exception ex) { #if DEBUG Console.WriteLine(ex.ToString()); #endif Disconnect(); return(-1); } }
private byte[] Receive() { var buf = new byte[4048]; byte last = 0; while (last != 0x05) { var bytesRead = Sock.Receive(buf, 0, 2048, SocketFlags.None); if (bytesRead > 0) { last = 0; for (var i = buf.Length - 1; i > 0; i--) { var b = buf[i]; if (b != 0x0 && b != 0xA) { last = b; break; } } } } return(buf); }
private void Loop() { while (true) { try { byte[] DataLength = new byte[2]; Sock.Receive(DataLength); short Length = System.BitConverter.ToInt16(DataLength, 0); byte[] PacketIdentifier = new byte[1]; Sock.Receive(PacketIdentifier); byte[] Packet = new byte[Length]; Sock.Receive(Packet); if (PacketIdentifier[0] == 0xFF) { //get some shit switch (Packet[0]) { case 1: //handshake Handshake(); break; default: throw new Exception("Got protocol ID, but is malformed."); } } else { ISComm.EventArgs.OnMessageArgs args = new ISComm.EventArgs.OnMessageArgs(); args.ID = PacketIdentifier[0]; if (args.ID == 0xFF) { args.IsProtocolPacket = true; } else { args.IsProtocolPacket = false; } args.Data = Packet; args.Length = (short)(3 + Packet.Length); //now call event handler OnMessage(this, args); } } catch (ObjectDisposedException) { return; } catch (SocketException) { return; } } }