I want to prepare an order package for the parameters in the 2nd picture according to the data types in the 1st picture, but I could not set the rules correctly, I could not make the correct conversion and arrangement. How do I set up bundle creation correctly?
According to this code, when I transmit the package to the server, the server closes the connection and does not return a response because I set the package incorrectly.
1. Picture
2. Picture
Some fix values:
- Order Token: 101
- Order book ID: 3437892
- Side girin (B/S/T): B
- Quantity girin: 1
- Price girin (Decimal formatında): 2.9
- Client-Account girin: DE-102001
private static void SendOrder()
{
try
{
Console.WriteLine("Order Token girin:");
string orderToken = Console.ReadLine().PadRight(14);
Console.WriteLine("Order book ID girin:");
int orderBookId = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Side girin (B/S/T):");
string side = Console.ReadLine();
Console.WriteLine("Quantity girin:");
int quantity = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Price girin (Decimal formatında):");
decimal price = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Client-Account girin:");
string clientAccount = Console.ReadLine().PadRight(16);
// Diğer sabit alanlar
string customerInfo = "".PadRight(15);
string exchangeInfo = "".PadRight(32);
byte openClose = 0; // 0 = Default for the account
// Veriyi baytlara dönüştürme
byte[] orderTokenBytes = Encoding.ASCII.GetBytes(orderToken);
byte[] orderBookIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(orderBookId));
byte[] quantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(quantity));
byte[] priceBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(price * 10000))); // Fiyatı 4 bayt olarak ayarlama
byte[] displayQuantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(0L)); // 8 baytlı ve 0 olarak ayarlama
// Paket oluşturma
byte[] orderPacketBytes = new byte[114];
orderPacketBytes[0] = (byte)'O';
orderTokenBytes.CopyTo(orderPacketBytes, 1);
Array.Copy(orderBookIdBytes, 0, orderPacketBytes, 15, 4); // 4 baytlık Order book ID
orderPacketBytes[19] = (byte)side[0];
Array.Copy(quantityBytes, 0, orderPacketBytes, 20, 4); // 8 baytlık Quantity
Array.Copy(priceBytes, 0, orderPacketBytes, 28, 4); // 4 baytlık Price
orderPacketBytes[32] = (byte)'0'; // Time In Force (0 = Day)
orderPacketBytes[33] = openClose; // Open Close (0 = Default)
Encoding.ASCII.GetBytes(clientAccount).CopyTo(orderPacketBytes, 34);
Encoding.ASCII.GetBytes(customerInfo).CopyTo(orderPacketBytes, 50);
Encoding.ASCII.GetBytes(exchangeInfo).CopyTo(orderPacketBytes, 65);
displayQuantityBytes.CopyTo(orderPacketBytes, 97); // Display Quantity
orderPacketBytes[105] = (byte)'1'; // Client Category (1 = Client)
orderPacketBytes[106] = (byte)'0'; // OffHours (0 = Normal hours)
Encoding.ASCII.GetBytes("".PadRight(7)).CopyTo(orderPacketBytes, 107); // Reserved
byte[] packet = CreateOrderPacket(orderPacketBytes);
_networkStream.Write(packet, 0, packet.Length);
LogMessage("Sipariş girme isteği gönderildi.");
LogMessage("Giden: " + BitConverter.ToString(packet));
LogMessage("Giden (Metin): " + Encoding.ASCII.GetString(packet));
Task.Run(() => BeginReadingOrderResponse(_cancellationTokenSource.Token));
}
catch (Exception ex)
{
LogMessage("Sipariş girme isteği gönderilirken hata: " + ex.Message);
}
}
private static byte[] CreateOrderPacket(byte[] messageBytes)
{
byte[] lengthBytes = BitConverter.GetBytes((ushort)(messageBytes.Length + 1));
if (BitConverter.IsLittleEndian)
Array.Reverse(lengthBytes);
byte[] packet = new byte[lengthBytes.Length + messageBytes.Length + 1]; // Length + Type + Payload
lengthBytes.CopyTo(packet, 0);
messageBytes.CopyTo(packet, lengthBytes.Length);
packet[packet.Length - 1] = (byte)'n'; // Terminating newline for SoupBinTCP
return packet;
}
private static async Task BeginReadingOrderResponse(CancellationToken token)
{
try
{
byte[] buffer = new byte[4096];
while (_isConnected && !token.IsCancellationRequested)
{
int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length, token);
if (bytesRead == 0)
{
LogMessage("Sunucu bağlantıyı kesti.");
Disconnect();
break;
}
ProcessOrderPacket(buffer, bytesRead);
}
}
catch (OperationCanceledException)
{
LogMessage("Okuma işlemi iptal edildi.");
}
catch (Exception ex)
{
LogMessage("Yanıt okunurken hata: " + ex.Message);
Disconnect();
}
}
seniro dev created the code but it didn’t work
ysrkdl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.