Skip to content

Commit 433c555

Browse files
Unhackify HackyNetStream with less hacky hacks
1 parent d4967e8 commit 433c555

File tree

1 file changed

+10
-42
lines changed

1 file changed

+10
-42
lines changed

fCraft/Network/Player.Networking.cs

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static Player() {
4949

5050
readonly Thread ioThread;
5151
readonly TcpClient client;
52-
HackyStream stream;
52+
Stream stream;
5353
PacketReader reader;
5454
PacketWriter writer;
5555

@@ -95,8 +95,7 @@ internal static Player StartSession( [NotNull] TcpClient tcpClient ) {
9595
IP = ( (IPEndPoint)( client.Client.RemoteEndPoint ) ).Address;
9696
if( Server.RaiseSessionConnectingEvent( IP ) ) return;
9797

98-
NetworkStream netStream = client.GetStream();
99-
stream = new HackyNetStream(netStream);
98+
stream = client.GetStream();
10099
reader = new PacketReader( stream );
101100
writer = new PacketWriter( stream );
102101

@@ -261,7 +260,7 @@ void IoLoop() {
261260

262261

263262
// get input from player
264-
while( canReceive && stream.HasDataAvailable) {
263+
while( canReceive && client.Available > 0 ) {
265264
byte opcode = reader.ReadByte();
266265
switch( (OpCode)opcode ) {
267266

@@ -574,59 +573,26 @@ static bool IsModernClient( string client ) {
574573
return client.CaselessContains( "ClassiCube" ) || client.CaselessContains( "ClassicalSharp" );
575574
}
576575

577-
// NOTE: Because Stream doesn't have DataAvailable and we need this info
578-
public abstract class HackyStream : Stream {
576+
public sealed class WebSocketStream : Stream {
579577
static NotSupportedException ex = new NotSupportedException("Unsupported I/O operation");
580-
public abstract bool HasDataAvailable { get; }
581578

582579
public override bool CanSeek { get { return false; } }
583580
public override void SetLength(long value) { throw ex; }
584581
public override long Seek(long offset, SeekOrigin origin) { throw ex; }
585582

586583
public override long Length { get { throw ex; } }
587584
public override long Position { get { throw ex; } set { throw ex; } }
588-
}
589-
590-
public sealed class HackyNetStream : HackyStream {
591-
NetworkStream underlying;
592585

593586
public override bool CanRead { get { return underlying.CanRead; } }
594587
public override bool CanWrite { get { return underlying.CanWrite; } }
595588
public override void Close() { underlying.Close(); }
596589
public override void Flush() { underlying.Flush(); }
597-
public override bool HasDataAvailable { get { return underlying.DataAvailable; } }
598-
599-
public HackyNetStream(NetworkStream underlying) {
600-
this.underlying = underlying;
601-
}
602-
603-
public override int Read(byte[] buffer, int offset, int count) {
604-
if (underlying.CanRead) {
605-
return underlying.Read(buffer, offset, count);
606-
} else {
607-
return 0;
608-
}
609-
}
610-
611-
public override void Write(byte[] buffer, int offset, int count) {
612-
underlying.Write(buffer, offset, count);
613-
}
614-
}
615-
616-
public sealed class WebSocketStream : HackyStream {
617-
HackyStream underlying;
618-
619-
public override bool CanRead { get { return true; } }
620-
public override bool CanWrite { get { return true; } }
621-
public override void Close() { underlying.Close(); }
622-
public override void Flush() { underlying.Flush(); }
623-
// TODO: Probably inaccurate
624-
public override bool HasDataAvailable { get { return underlying.HasDataAvailable; } }
625590

626-
public WebSocketStream(HackyStream underlying) {
591+
public WebSocketStream(Stream underlying) {
627592
this.underlying = underlying;
628593
}
629594

595+
Stream underlying;
630596
bool readingHeaders = true;
631597
bool conn, upgrade, version, proto;
632598
string verKey;
@@ -635,9 +601,11 @@ public WebSocketStream(HackyStream underlying) {
635601
// So just allocate one array here instead
636602
byte[] tmp = new byte[1];
637603
byte ReadRawByte() {
604+
// socket was disconnected
605+
if (!underlying.CanRead) throw new EndOfStreamException();
606+
638607
int len = underlying.Read(tmp, 0, 1);
639-
if (len == 0)
640-
throw new EndOfStreamException();
608+
if (len == 0) throw new EndOfStreamException();
641609
return tmp[0];
642610
}
643611

0 commit comments

Comments
 (0)