You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+38-58Lines changed: 38 additions & 58 deletions
Original file line number
Diff line number
Diff 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.
5
3
network.
6
4
7
5
# 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.
11
7
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
14
10
are sending.
15
11
16
12
# 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.
23
15
24
16
## 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.
27
18
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.
30
20
31
21
### 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.
35
23
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:
38
25
39
-
*void SendMessage(Message m);*
26
+
`void SendMessage(Message m);`
40
27
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.
44
30
45
31
## SimpleServer Scene.
46
32
*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
51
37
and print them.
52
38
53
39
### 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
66
50
has, it gets casted and then printed in the UI with the
# How to add a Protobuf Object and transmit it over the network
71
55
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 :)
74
58
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).
78
60
79
61
In order to compile our SampleObject you need to have **protoc** installed
80
62
and compile the protobuffer for C#. After that you are pretty much done.
@@ -93,31 +75,29 @@ message SampleObject {
93
75
}
94
76
```
95
77
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.
98
80
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`
101
82
102
83
```csharp
103
84
//ProtobufMessageTypes.cs
104
85
publicstaticclassProtobufMessageTypes {
105
-
publicconstintSAMPLE_OBJECT=1;//can have any value, just make sure is unique
86
+
publicconstintSAMPLE_OBJECT=1;//can have any value, just make sure is unique
106
87
}
107
88
```
108
89
109
90
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
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)`:
0 commit comments