Skip to content

Commit 8d16f8d

Browse files
committed
Reformatted and cleaned up
1 parent 1e524f6 commit 8d16f8d

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

dotnet/RPCServer.cs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
11
using System;
22
using RabbitMQ.Client;
33
using RabbitMQ.Client.Events;
4+
using System.Text;
45

5-
class RPCServer {
6-
public static void Main() {
7-
ConnectionFactory factory = new ConnectionFactory();
8-
factory.HostName = "localhost";
9-
using (IConnection connection = factory.CreateConnection())
10-
using (IModel channel = connection.CreateModel()) {
11-
channel.QueueDeclare("rpc_queue", false, false, false, null);
12-
channel.BasicQos(0, 1, false);
13-
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
14-
channel.BasicConsume("rpc_queue", false, consumer);
15-
Console.WriteLine(" [x] Awaiting RPC requests");
6+
class RPCServer
7+
{
8+
public static void Main()
9+
{
10+
var factory = new ConnectionFactory() { HostName = "localhost" };
11+
using (var connection = factory.CreateConnection())
12+
{
13+
using (var channel = connection.CreateModel())
14+
{
15+
channel.QueueDeclare("rpc_queue", false, false, false, null);
16+
channel.BasicQos(0, 1, false);
17+
var consumer = new QueueingBasicConsumer(channel);
18+
channel.BasicConsume("rpc_queue", false, consumer);
19+
Console.WriteLine(" [x] Awaiting RPC requests");
1620

17-
while(true) {
18-
string response = null;
19-
BasicDeliverEventArgs ea =
20-
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
21+
while (true)
22+
{
23+
string response = null;
24+
var ea =
25+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
2126

22-
byte[] body = ea.Body;
23-
IBasicProperties props = ea.BasicProperties;
24-
IBasicProperties replyProps = channel.CreateBasicProperties();
25-
replyProps.CorrelationId = props.CorrelationId;
27+
var body = ea.Body;
28+
var props = ea.BasicProperties;
29+
var replyProps = channel.CreateBasicProperties();
30+
replyProps.CorrelationId = props.CorrelationId;
2631

27-
try {
28-
string message = System.Text.Encoding.UTF8.GetString(body);
29-
int n = int.Parse(message);
30-
Console.WriteLine(" [.] fib({0})", message);
31-
response = fib(n).ToString();
32-
} catch (Exception e) {
33-
Console.WriteLine(" [.] " + e);
34-
response = "";
35-
} finally {
36-
byte[] responseBytes =
37-
System.Text.Encoding.UTF8.GetBytes(response);
38-
channel.BasicPublish("", props.ReplyTo, replyProps,
39-
responseBytes);
40-
channel.BasicAck(ea.DeliveryTag, false);
32+
try
33+
{
34+
var message = Encoding.UTF8.GetString(body);
35+
int n = int.Parse(message);
36+
Console.WriteLine(" [.] fib({0})", message);
37+
response = fib(n).ToString();
38+
}
39+
catch (Exception e)
40+
{
41+
Console.WriteLine(" [.] " + e.Message);
42+
response = "";
43+
}
44+
finally
45+
{
46+
var responseBytes =
47+
Encoding.UTF8.GetBytes(response);
48+
channel.BasicPublish("", props.ReplyTo, replyProps,
49+
responseBytes);
50+
channel.BasicAck(ea.DeliveryTag, false);
51+
}
4152
}
4253
}
4354
}
4455
}
4556

46-
private static int fib(int n) {
57+
private static int fib(int n)
58+
{
4759
if (n == 0 || n == 1) return n;
4860
return fib(n - 1) + fib(n - 2);
4961
}

0 commit comments

Comments
 (0)