Skip to content

Commit 089fcfd

Browse files
authored
Update Readme.md
1 parent 9807623 commit 089fcfd

File tree

1 file changed

+38
-58
lines changed

1 file changed

+38
-58
lines changed

Readme.md

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,32 @@
1-
# Protobuffers over network for Unity3D
2-
This project lets you quickly and easily send protobuffer objects over the
3-
network between 2 apps. The protobuffers will get automatically compressed if
4-
they are larger than 1mb in order to save space when sending them through the
1+
# Protobuffers for Unity3D
2+
This project lets you quickly and easily send [protobuf](https://github.com/google/protobuf) objects between applications. The protobuffers will get automatically compressed if they are larger than 1MB in order to save space when sending them.
53
network.
64

75
# How it works
8-
The way the project works is with a messaging system. Messages get sent
9-
through sockets. On one side one application has a listening socket and on
10-
the other side the application sends messages to it.
6+
A socket will be opened between the applications to serve as a messaging system for the object transmission. On one side an application has a listening socket, on the other one the application sends messages to it.
117

12-
*Messages* are the basic way how protobuffers are sent, a message always
13-
contains a Protobuffer and a *Type* that identifies the protobuffer object you
8+
**Messages** are the basic way how protobuffers are sent, a message always
9+
contains a Protobuffer and a **Type** that identifies the protobuffer object you
1410
are sending.
1511

1612
# Project Structure
17-
In the unity project, There are 2 scenes. **SimpleClient** and
18-
**SimpleServer**.
19-
20-
All the information you need to run the project is contained in these 2
21-
scenes, specifically in 2 files called *ClientTest.cs* (for sending messages)
22-
and *ServerTest.cs* for receiving messages.
13+
There are two scenes in the Unity project: **SimpleClient** and **SimpleServer**. All the information you need to run the project is contained in these 2 scenes, specifically in 2 files called `ClientTest.cs` (for sending messages)
14+
and `ServerTest.cs` for receiving messages.
2315

2416
## SimpleClient scene.
25-
*SimpleClient* is an empty scene that contains a *ClientTest.cs* Behaviour
26-
added to the main camera.
17+
SimpleClient is an empty scene that contains a `ClientTest.cs` which is added to the main camera.
2718

28-
This scene acts as a client and sends messages to the other application
29-
called *SimpleServer* which acts as the listening socket.
19+
This scene acts as a client and sends messages to the other application (SimpleServer) which acts as the listening socket.
3020

3121
### ClientTest.cs
32-
In order to send a Protobuf object to another application, you need to open a
33-
socket and send a Protobuf object encapsulated in a Message object. convert it
34-
to an array of bytes and send it.
22+
In order to send Protobuf objects you need to encapsulate them inside a **Message** object that will be sent through the communications socket. The messages will be converted into byte arrays that will be send over the network.
3523

36-
The *ClientTest.cs* class contains precissely that function already for you to
37-
send messages and its called:
24+
The `ClientTest.cs` class already contains a function that does this for you:
3825

39-
*void SendMessage(Message m);*
26+
`void SendMessage(Message m);`
4027

41-
In ClientTest.cs you will find the UI for setting up values for the
42-
SampleObject protobuf object and the address and port of the app you want to
43-
send the values to.
28+
In `ClientTest.cs` you will find the UI for setting up values for the
29+
SampleObject protobuf object and the address and port of the application that will be receiving the data.
4430

4531
## SimpleServer Scene.
4632
*SimpleServer* is an empty scene that contains a *ServerTest.cs* Behaviour
@@ -51,30 +37,26 @@ incoming messages from any client. It processes the messages, rebuilds them
5137
and print them.
5238

5339
### ServerTest.cs
54-
In order to receive a protobuf object, a *SocketProtobufListener* object
55-
needs to be created. This object receives an IP, a port where the socket
56-
is going to be listening for incoming messages and a *ConcurrentQueue* for
57-
*ProtobufMessages* that are stored in the Queue as they come.
58-
59-
The *ServerTest.cs* creates a SocketProtobufListener object, binds a socket
60-
automatically to the current IP used and on a separate thread a socket starts
61-
to listen for incoming messages, these messages are stored in the
62-
*messaQueue* declared in the class.
63-
64-
When a message gets added to the queue, in the Update function as soon as a
65-
message arrives, the protobuf message gets read and depending on the type it
40+
In order to receive a protobuf object, a `SocketProtobufListener` object
41+
needs to be created. This object receives a host and a port where the socket
42+
is going to be listening for incoming messages. A *ConcurrentQueue* is used for storing incoming *ProtobufMessages*.
43+
44+
`ServerTest.cs` creates a `SocketProtobufListener` object, binds a socket
45+
and starts listening for incoming messages on a on a separate thread. These messages are stored in the
46+
*messageQueue* declared in the class.
47+
48+
When a message gets added to the queue (in the `Update` function as soon as a
49+
message arrives), the protobuf message gets read and depending on the type it
6650
has, it gets casted and then printed in the UI with the
67-
*PrintSampleObjectFields(SampleObject s)* function.
51+
`PrintSampleObjectFields(SampleObject s)` function.
6852

6953

7054
# How to add a Protobuf Object and transmit it over the network
7155
Here I will walk you through on how to send an object and receive it on the
72-
other end. This app doesnt check for errors in connections nor anything
73-
else. This is left for you, the reader.
56+
other end. This app doesn't check for errors in connections nor anything
57+
else. This is left as an exercise for the reader :)
7458

75-
First things first. We need to create a Protobuf object. I will not cover the
76-
specific syntax on how protobuffer objects work, for more reference just
77-
check it out [here](https://developers.google.com/protocol-buffers/docs/overview).
59+
I won't be convering how protobufs work or their syntax, for more information you can find and overview [here](https://developers.google.com/protocol-buffers/docs/overview).
7860

7961
In order to compile our SampleObject you need to have **protoc** installed
8062
and compile the protobuffer for C#. After that you are pretty much done.
@@ -93,31 +75,29 @@ message SampleObject {
9375
}
9476
```
9577

96-
It contains a *type* which is **necessary** for all your objects you want to
97-
transmit. 2 sample strings, a float and an integer.
78+
It contains a *type* (**required** for all your objects you want to
79+
transmit), two sample strings, a float and an integer.
9880

99-
After compiling the protocol buffer to C# we need to tell the system with a
100-
unique identifier the type of the object. This is added in *ProtobufMessagesTypes.cs*
81+
After compiling the protocol buffer to C# we need to add a unique identifier to the type of the object. This is added in `ProtobufMessagesTypes.cs`
10182

10283
```csharp
10384
//ProtobufMessageTypes.cs
10485
public static class ProtobufMessageTypes {
105-
public const int SAMPLE_OBJECT = 1;//can have any value, just make sure is unique
86+
public const int SAMPLE_OBJECT = 1; // can have any value, just make sure is unique
10687
}
10788
```
10889

10990
Then, we create a function for creating this object message in
110-
*MessageCreator.cs*. Here we define a function that creates an object with
111-
the parameters and returns a message (See MessageCreator.cs for the
91+
`MessageCreator.cs`. Here we define a function that creates an object with
92+
the parameters and returns a message (See `MessageCreator.cs` for the
11293
implementation details).
11394

11495
```csharp
11596
//MessageCreator.cs
11697
public static Message CreateSampleObjectMessage(string objName, string sampleStr, int sampleInt, float sampleFloat)
11798
```
11899

119-
In order to receive and undertand the message, we have to convert the message
120-
to a ProtobufMessage, this is done in *DeserializeByType(int type, MemoryStream memStream)*
100+
In order to receive and understand the message we have to convert it to a `ProtobufMessage`. This is done by `DeserializeByType(int type, MemoryStream memStream)`:
121101

122102
```csharp
123103
//MessageDeserializer.cs
@@ -134,13 +114,13 @@ private static ProtobufMessage DeserializeByType(int type, MemoryStream memStrea
134114
}
135115
```
136116

137-
Finally, in the *Update()* function in *ServerTest.cs* we can cast our
138-
ProtobufMessage.cs with the specific type to our object.
117+
Finally, in the `Update()` function in *ServerTest.cs* we can cast our
118+
ProtobufMessage with the specific type to our object:
139119

140120
```csharp
141121
// ServerTest.cs
142122
void Update() {
143-
if(messageQueue.Count > 0) {//when there's a protobuf message in the queue, we print it
123+
if(messageQueue.Count > 0) { // when there's a protobuf message in the queue, we print it
144124
ProtobufMessage pm;
145125
messageQueue.TryDequeue(out pm);
146126

0 commit comments

Comments
 (0)