Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions lab 08 - LLAPI/Assets/ClientConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.Networking;

/// <summary>
/// Author: Matt Gipson
/// Contact: Deadwynn@gmail.com
/// Domain: www.livingvalkyrie.com
///
/// Description: ClientConnection
/// </summary>
public class ClientConnection : MonoBehaviour {
int clientSocketID = -1;

//Will store the unique identifier of the session that keeps the connection between the client
//and the server. You use this ID as the 'target' when sending messages to the server.
int clientServerConnectionID = -1;
int maxConnections = 10;
byte unreliableChannelID;
byte reliableChannelID;
bool isClientConnected = false;

void Start() {
DontDestroyOnLoad(this);

//Build the global config
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.ReactorModel = ReactorModel.FixRateReactor;
globalConfig.ThreadAwakeTimeout = 10;

//Build the channel config
ConnectionConfig connectionConfig = new ConnectionConfig();
reliableChannelID = connectionConfig.AddChannel(QosType.ReliableSequenced);
unreliableChannelID = connectionConfig.AddChannel(QosType.UnreliableSequenced);

//Create the host topology
HostTopology hostTopology = new HostTopology(connectionConfig, maxConnections);

//Initialize the network transport
NetworkTransport.Init(globalConfig);

//Open a socket for the client
clientSocketID = NetworkTransport.AddHost(hostTopology, 7790);

//Make sure the client created the socket successfully
if (clientSocketID < 0) {
print("Server connection failed");
} else {
print("server started");
}


//Create a byte to store a possible error
byte error;

//Connect to the server using
//int NetworkTransport.Connect(int socketConnectingFrom, string ipAddress, int port, 0, out byte possibleError)
//Store the ID of the connection in clientServerConnectionID

clientServerConnectionID = NetworkTransport.Connect(clientSocketID, "127.0.0.1", 7777, 0, out error);

//Display the error (if it did error out)
if (error != (byte) NetworkError.Ok) {
NetworkError networkError = (NetworkError) error;
print("Error: " + networkError.ToString());
} else {
isClientConnected = true;
}
}

void Update() {
//If the client failed to create the socket, leave this function
if (!isClientConnected) {
return;
}

PollBasics();

//If the user pressed the Space key
//Send a message to the server "FirstConnect"
if (Input.GetKeyDown(KeyCode.Space)) {
SendMessage("FirstConnect");
}

//If the user pressed the R key
//Send a message to the server "Random message!"
if (Input.GetKeyDown(KeyCode.R)) {
SendMessage("Random message!");
}
}

void SendMessage(string message) {
//create a byte to store a possible error
byte error;

//Create a buffer to store the message
byte[] buffer = new byte[1024];

//Create a memory stream to send the information through
Stream memoryStream = new MemoryStream(buffer);

//Create a binary formatter to serialize and translate the message into binary
BinaryFormatter binaryFormatter = new BinaryFormatter();

//Serialize the message
binaryFormatter.Serialize(memoryStream, message);

//Send the message from this client, over the client server connection, using the reliable channel
NetworkTransport.Send(clientSocketID,
clientServerConnectionID,
reliableChannelID,
buffer,
(int) memoryStream.Position,
out error);

//Display the error (if it did error out)
if (error != (byte) NetworkError.Ok) {
NetworkError networkError = (NetworkError) error;
print("Error: " + networkError.ToString());
}
}

void InterperateMessage(string message) {
//if the message is "goto_NewScene"
//load the level named "Scene2"
if (message == "goto_NewScene") {
Application.LoadLevel("Scene2");
}
}

void PollBasics() {
//prepare to receive messages by practicing good bookkeeping

int recClientId; //who recieved message
int connectionId; //who sent message
int channelId; //what channel message sent from
int dataSize; //how large message can be
byte[] buffer = new byte[1024]; //actual message
byte error; //if there is an error

NetworkEventType networkEvent = NetworkEventType.DataEvent;

//do
do {
//Receive network events
networkEvent = NetworkTransport.Receive(out recClientId,
out connectionId,
out channelId,
buffer,
1024,
out dataSize,
out error);

//switch on the network event types
switch (networkEvent) {
//if nothing, do nothing
//if connection
case NetworkEventType.ConnectEvent:

//verify that the message was meant for me
if (recClientId == clientSocketID) {
//debug out that i connected to the server, and display the ID of what I connected to
print("Client: connected to: " + connectionId);

//set my bool that is keeping track if I am connected to a server to true
isClientConnected = true;
}
break;

//if data event
case NetworkEventType.DataEvent:

//verify that the message was meant for me and if I am connected to a server
if (recClientId == clientSocketID) {
//decode the message (bring it through the memory stream, deseralize it, translate the binary)
Stream memoryStream = new MemoryStream(buffer);
BinaryFormatter binaryFormatter = new BinaryFormatter();
string message = binaryFormatter.Deserialize(memoryStream).ToString();

//Debug the message and the connection that the message was sent from
print("Client: recieved message " + message + " from " + connectionId);

//the message to interperate
InterperateMessage(message);
}
break;

//if disconnection
case NetworkEventType.DisconnectEvent:

//verify that the message was meant for me, and that I am disconnecting from the current connection I have with the server
if (recClientId == clientSocketID) {
//debug that I disconnected
print("Client: disconnected from server");

//set my bool that is keeping track if I am connected to a server to false
isClientConnected = false;
}
break;
}

//while (the network event I am receiving is not Nothing)
} while (networkEvent != NetworkEventType.Nothing);
}
}
12 changes: 12 additions & 0 deletions lab 08 - LLAPI/Assets/ClientConnection.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lab 08 - LLAPI/Assets/Scene0.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions lab 08 - LLAPI/Assets/Scene0.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lab 08 - LLAPI/Assets/Scene2.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions lab 08 - LLAPI/Assets/Scene2.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions lab 08 - LLAPI/Assets/ServerConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using UnityEngine;
using UnityEngine.Networking;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;

/// <summary>
/// Author: Matt Gipson
/// Contact: Deadwynn@gmail.com
/// Domain: www.livingvalkyrie.com
///
/// Description: ServerConnection
/// </summary>
public class ServerConnection : MonoBehaviour {
#region Fields

int serverSocketID = -1;
int maxConnections = 10;
byte unreliableChannelID;
byte reliableChannelID;
bool serverInitialized = false;

#endregion

void Start() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.ReactorModel = ReactorModel.FixRateReactor;
globalConfig.ThreadAwakeTimeout = 10;

ConnectionConfig connectionConfig = new ConnectionConfig();
reliableChannelID = connectionConfig.AddChannel(QosType.ReliableSequenced);
unreliableChannelID = connectionConfig.AddChannel(QosType.UnreliableSequenced);

HostTopology hostTopology = new HostTopology(connectionConfig, maxConnections);

NetworkTransport.Init(globalConfig);

serverSocketID = NetworkTransport.AddHost(hostTopology, 7777);

if (serverSocketID < 0) {
print("Server connection failed");
} else {
print("server started");
}

serverInitialized = true;
DontDestroyOnLoad(this);
}

void Update() {
if (!serverInitialized) {
return;
}

int recHostId; //who recieved message
int connectionId; //who sent message
int channelId; //what channel message sent from
int dataSize; //how large message can be
byte[] buffer = new byte[1024]; //actual message
byte error; //if there is an error

NetworkEventType networkEvent = NetworkEventType.DataEvent;

do {
networkEvent = NetworkTransport.Receive(out recHostId,
out connectionId,
out channelId,
buffer,
1024,
out dataSize,
out error);

switch (networkEvent) {
case NetworkEventType.ConnectEvent:
if (recHostId == serverSocketID) {
print("Server: Player " + connectionId.ToString() + " connected!");
}
break;
case NetworkEventType.DataEvent:
if (recHostId == serverSocketID) {
//open memory stream to the size of the buffer
Stream memoryStream = new MemoryStream(buffer);

//create binary formatter to begin reading info from stream
BinaryFormatter binaryFormatter = new BinaryFormatter();

//use formatter to deserialize binary info stored in memory string and convert to string
string message = binaryFormatter.Deserialize(memoryStream).ToString();

//debug message
print("Server: received data from " + connectionId + "! Message: " + message);

//respond
RespondMessage(message, connectionId);
}
break;
case NetworkEventType.DisconnectEvent:
if (recHostId == serverSocketID) {
print("Server: Recieved disconnect from " + connectionId.ToString());
}
break;
}
} while (networkEvent != NetworkEventType.Nothing);
}

void RespondMessage(string message, int playerId) {
if (message == "FirstConnect") {
print("Server: recieved first contact from: " + playerId);
SendMessage("goto_NewScene", playerId);
}

//check if scene2 loadeed
if (Application.loadedLevel != 1) {
Application.LoadLevel(1);
}
}

void SendMessage(string message, int target) {
byte error;
byte[] buffer = new byte[1024];
Stream memoryStream = new MemoryStream(buffer);
BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(memoryStream, message);

NetworkTransport.Send(serverSocketID, target, reliableChannelID, buffer, (int) memoryStream.Position, out error);

if (error != (byte)NetworkError.Ok) {
NetworkError networkError = (NetworkError) error;
print("Error: " + networkError.ToString());
}
}

}
12 changes: 12 additions & 0 deletions lab 08 - LLAPI/Assets/ServerConnection.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lab 08 - LLAPI/Assets/UnityVS.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading