Skip to content

Commit

Permalink
Simplify OutputStream constructors in C# (#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Aug 1, 2024
1 parent 41102d0 commit fe1f0d2
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 274 deletions.
2 changes: 1 addition & 1 deletion cpp/src/slice2cs/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace
{
case DefaultFormat:
{
return "Ice.FormatType.DefaultFormat";
return "null";
}
case CompactFormat:
{
Expand Down
17 changes: 8 additions & 9 deletions csharp/src/Ice/ConnectionI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ internal ConnectionI(
_readStream = new InputStream(instance, Util.currentProtocolEncoding);
_readHeader = false;
_readStreamPos = -1;
_writeStream = new OutputStream(instance, Util.currentProtocolEncoding);
_writeStream = new OutputStream(); // temporary stream
_writeStreamPos = -1;
_upcallCount = 0;
_state = StateNotInitialized;
Expand Down Expand Up @@ -1511,7 +1511,7 @@ _inactivityTimer is null && // timer not already scheduled
// _sendStreams message.
if (_sendStreams.Count == 0)
{
OutputStream os = new OutputStream(_instance, Util.currentProtocolEncoding);
var os = new OutputStream(Util.currentProtocolEncoding);
os.writeBlob(Protocol.magic);
Util.currentProtocol.ice_writeMembers(os);
Util.currentProtocolEncoding.ice_writeMembers(os);
Expand Down Expand Up @@ -1779,7 +1779,7 @@ private void initiateShutdown()
//
// Before we shut down, we send a close connection message.
//
OutputStream os = new OutputStream(_instance, Util.currentProtocolEncoding);
var os = new OutputStream(Util.currentProtocolEncoding);
os.writeBlob(Protocol.magic);
Util.currentProtocol.ice_writeMembers(os);
Util.currentProtocolEncoding.ice_writeMembers(os);
Expand Down Expand Up @@ -1844,7 +1844,7 @@ private bool validate(int operation)
_writeStream.writeByte(Protocol.validateConnectionMsg);
_writeStream.writeByte(0); // Compression status (always zero for validate connection).
_writeStream.writeInt(Protocol.headerSize); // Message size.
TraceUtil.traceSend(_writeStream, _logger, _traceLevels);
TraceUtil.traceSend(_writeStream, _instance, _logger, _traceLevels);
_writeStream.prepareWrite();
}

Expand Down Expand Up @@ -2048,7 +2048,7 @@ private int sendNextMessage(out Queue<OutgoingMessage> callbacks)
message.stream.prepareWrite();
message.prepared = true;

TraceUtil.traceSend(stream, _logger, _traceLevels);
TraceUtil.traceSend(stream, _instance, _logger, _traceLevels);

//
// Send the message.
Expand Down Expand Up @@ -2123,7 +2123,7 @@ private int sendMessage(OutgoingMessage message)
message.stream.prepareWrite();
message.prepared = true;

TraceUtil.traceSend(stream, _logger, _traceLevels);
TraceUtil.traceSend(stream, _instance, _logger, _traceLevels);

// Send the message without blocking.
if (_observer is not null)
Expand Down Expand Up @@ -2176,8 +2176,7 @@ private OutputStream doCompress(OutputStream uncompressed, bool compress)
_compressionLevel);
if (cbuf is not null)
{
OutputStream cstream =
new OutputStream(uncompressed.instance(), uncompressed.getEncoding(), cbuf, true);
OutputStream cstream = new OutputStream(uncompressed.getEncoding(), new Internal.Buffer(cbuf, true));

//
// Set compression status.
Expand Down Expand Up @@ -2794,7 +2793,7 @@ internal void adopt()
{
if (_adopt)
{
OutputStream stream = new OutputStream(this.stream.instance(), Util.currentProtocolEncoding);
var stream = new OutputStream(Util.currentProtocolEncoding);
stream.swap(this.stream);
this.stream = stream;
_adopt = false;
Expand Down
10 changes: 7 additions & 3 deletions csharp/src/Ice/CurrentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static OutgoingResponse createOutgoingResponse<TResult>(
this Current current,
TResult result,
Action<OutputStream, TResult> marshal,
FormatType formatType = FormatType.DefaultFormat)
FormatType? formatType = null)
{
OutputStream ostr = current.startReplyStream();
if (current.requestId != 0)
Expand Down Expand Up @@ -131,7 +131,8 @@ OutgoingResponse createOutgoingResponseCore(System.Exception exc)

if (current.requestId != 0)
{
ostr = new OutputStream(current.adapter.getCommunicator(), Util.currentProtocolEncoding);
// The default class format doesn't matter since we always encode user exceptions in Sliced format.
ostr = new OutputStream(Util.currentProtocolEncoding);
ostr.writeBlob(Protocol.replyHdr);
ostr.writeInt(current.requestId);
}
Expand Down Expand Up @@ -267,7 +268,10 @@ public static OutputStream startReplyStream(this Current current, ReplyStatus re
}
else
{
var ostr = new OutputStream(current.adapter.getCommunicator(), Util.currentProtocolEncoding);
var ostr = new OutputStream(
Util.currentProtocolEncoding,
current.adapter.getCommunicator().instance.defaultsAndOverrides().defaultFormat);

ostr.writeBlob(Protocol.replyHdr);
ostr.writeInt(current.requestId);
ostr.writeByte((byte)replyStatus);
Expand Down
1 change: 0 additions & 1 deletion csharp/src/Ice/FormatType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Ice;
/// </summary>
public enum FormatType
{
DefaultFormat,
CompactFormat,
SlicedFormat
}
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/BatchRequestQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public BatchRequestQueue(Instance instance, bool datagram)
_interceptor = initData.batchRequestInterceptor;
_batchStreamInUse = false;
_batchRequestNum = 0;
_batchStream = new Ice.OutputStream(instance, Ice.Util.currentProtocolEncoding);
_batchStream = new OutputStream(Ice.Util.currentProtocolEncoding, instance.defaultsAndOverrides().defaultFormat);
_batchStream.writeBlob(Protocol.requestBatchHdr);
_batchMarker = _batchStream.size();
_request = new BatchRequestI(this);
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Ice/Internal/CollocatedRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ private void dispatchAll(Ice.OutputStream os, int requestId, int requestCount)
{
fillInValue(os, Protocol.headerSize, requestCount);
}
TraceUtil.traceSend(os, _logger, _traceLevels);
TraceUtil.traceSend(os, _reference.getInstance(), _logger, _traceLevels);
}

Ice.InputStream iss = new Ice.InputStream(os.instance(), os.getEncoding(), os.getBuffer(), false);
Ice.InputStream iss = new Ice.InputStream(_reference.getInstance(), os.getEncoding(), os.getBuffer(), false);

if (requestCount > 0)
{
Expand Down Expand Up @@ -253,7 +253,7 @@ private void sendResponse(OutgoingResponse response, int requestId, bool amd)

// Adopt the OutputStream's buffer.
var inputStream = new InputStream(
outputStream.instance(),
_reference.getInstance(),
outputStream.getEncoding(),
outputStream.getBuffer(),
adopt: true);
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/EndpointFactoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public EndpointI create(string str, bool oaEndpoint)
// and ask the factory to read the endpoint data from that stream to create
// the actual endpoint.
//
Ice.OutputStream os = new Ice.OutputStream(_instance, Ice.Util.currentProtocolEncoding);
var os = new Ice.OutputStream(Ice.Util.currentProtocolEncoding);
os.writeShort(ue.type());
ue.streamWrite(os);
Ice.InputStream iss =
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/OpaqueEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public OpaqueEndpointI(short type, Ice.InputStream s)
//
public override void streamWrite(Ice.OutputStream s)
{
s.startEncapsulation(_rawEncoding, Ice.FormatType.DefaultFormat);
s.startEncapsulation(_rawEncoding);
s.writeBlob(_rawBytes);
s.endEncapsulation();
}
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Ice/Internal/OutgoingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected OutgoingAsyncBase(Instance instance, OutgoingAsyncCompletionCallback c
_doneInSent = false;
_alreadySent = false;
state_ = 0;
os_ = os ?? new Ice.OutputStream(instance, Ice.Util.currentProtocolEncoding);
os_ = os ?? new OutputStream(Ice.Util.currentProtocolEncoding, instance.defaultsAndOverrides().defaultFormat);
is_ = iss ?? new Ice.InputStream(instance, Ice.Util.currentProtocolEncoding);
_completionCallback = completionCallback;
if (_completionCallback != null)
Expand Down Expand Up @@ -1058,7 +1058,7 @@ protected void invoke(string operation, bool synchronous)

public void invoke(string operation,
Ice.OperationMode mode,
Ice.FormatType format,
Ice.FormatType? format,
Dictionary<string, string> context,
bool synchronous,
System.Action<Ice.OutputStream> write)
Expand Down Expand Up @@ -1144,7 +1144,7 @@ public OutgoingAsyncT(Ice.ObjectPrxHelperBase prx,

public void invoke(string operation,
Ice.OperationMode mode,
Ice.FormatType format,
Ice.FormatType? format,
Dictionary<string, string> context,
bool synchronous,
System.Action<Ice.OutputStream> write = null,
Expand Down
108 changes: 2 additions & 106 deletions csharp/src/Ice/Internal/TraceUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
namespace Ice.Internal;
internal sealed class TraceUtil
{
internal static void traceSend(Ice.OutputStream str, Ice.Logger logger, TraceLevels tl)
internal static void traceSend(Ice.OutputStream str, Instance instance, Ice.Logger logger, TraceLevels tl)
{
if (tl.protocol >= 1)
{
int p = str.pos();
Ice.InputStream iss = new Ice.InputStream(str.instance(), str.getEncoding(), str.getBuffer(), false);
Ice.InputStream iss = new Ice.InputStream(instance, str.getEncoding(), str.getBuffer(), false);
iss.pos(0);

using (System.IO.StringWriter s = new System.IO.StringWriter(CultureInfo.CurrentCulture))
Expand Down Expand Up @@ -41,25 +41,6 @@ internal static void traceRecv(Ice.InputStream str, Ice.Logger logger, TraceLeve
}
}

internal static void trace(string heading, Ice.OutputStream str, Ice.Logger logger, TraceLevels tl)
{
if (tl.protocol >= 1)
{
int p = str.pos();
Ice.InputStream iss = new Ice.InputStream(str.instance(), str.getEncoding(), str.getBuffer(), false);
iss.pos(0);

using (System.IO.StringWriter s = new System.IO.StringWriter(CultureInfo.CurrentCulture))
{
s.Write(heading);
printMessage(s, iss);

logger.trace(tl.protocolCat, s.ToString());
}
str.pos(p);
}
}

internal static void trace(string heading, Ice.InputStream str, Ice.Logger logger, TraceLevels tl)
{
if (tl.protocol >= 1)
Expand Down Expand Up @@ -95,73 +76,6 @@ internal static void traceSlicing(string kind, string typeId, string slicingCat,
}
}

public static void dumpStream(Ice.InputStream stream)
{
int pos = stream.pos();
stream.pos(0);

byte[] data = new byte[stream.size()];
stream.readBlob(data);
dumpOctets(data);

stream.pos(pos);
}

public static void dumpOctets(byte[] data)
{
const int inc = 8;

for (int i = 0; i < data.Length; i += inc)
{
for (int j = i; j - i < inc; j++)
{
if (j < data.Length)
{
int n = data[j];
if (n < 0)
{
n += 256;
}
string s;
if (n < 10)
{
s = " " + n;
}
else if (n < 100)
{
s = " " + n;
}
else
{
s = "" + n;
}
Console.Out.Write(s + " ");
}
else
{
Console.Out.Write(" ");
}
}

Console.Out.Write('"');

for (int j = i; j < data.Length && j - i < inc; j++)
{
// TODO: this needs fixing
if (data[j] >= 32 && data[j] < 127)
{
Console.Out.Write((char)data[j]);
}
else
{
Console.Out.Write('.');
}
}

System.Console.Out.WriteLine('"');
}
}

private static void printIdentityFacetOperation(System.IO.StringWriter s, Ice.InputStream str)
{
try
Expand Down Expand Up @@ -492,24 +406,6 @@ private static byte printMessage(System.IO.StringWriter s, Ice.InputStream str)
return type;
}

internal static void traceHeader(string heading, Ice.InputStream str, Ice.Logger logger, TraceLevels tl)
{
if (tl.protocol >= 1)
{
int p = str.pos();
str.pos(0);

using (System.IO.StringWriter s = new System.IO.StringWriter(CultureInfo.CurrentCulture))
{
s.Write(heading);
printHeader(s, str);

logger.trace(tl.protocolCat, s.ToString());
}
str.pos(p);
}
}

private static string getMessageTypeAsString(byte type)
{
switch (type)
Expand Down
Loading

0 comments on commit fe1f0d2

Please sign in to comment.