upm
Anton Franzluebbers 2022-02-04 00:09:46 -05:00
parent a207658b77
commit cb23d41691
1 changed files with 153 additions and 151 deletions

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
@ -27,6 +26,7 @@ namespace VelNet
YOU_LEFT = 7, YOU_LEFT = 7,
ROOM_DATA = 8 ROOM_DATA = 8
} }
public enum MessageSendType public enum MessageSendType
{ {
MESSAGE_OTHERS_ORDERED = 7, MESSAGE_OTHERS_ORDERED = 7,
@ -131,7 +131,7 @@ namespace VelNet
public static bool IsConnected => instance != null && instance.connected && instance.udpConnected; public static bool IsConnected => instance != null && instance.connected && instance.udpConnected;
//this is for sending udp packets // this is for sending udp packets
private static readonly byte[] toSend = new byte[1024]; private static readonly byte[] toSend = new byte[1024];
// Use this for initialization // Use this for initialization
@ -168,7 +168,7 @@ namespace VelNet
public class RoomDataMessage : Message public class RoomDataMessage : Message
{ {
public string room; public string room;
public List<Tuple<int, string>> members = new List<Tuple<int, string>>(); public readonly List<Tuple<int, string>> members = new List<Tuple<int, string>>();
} }
public class JoinMessage : Message public class JoinMessage : Message
@ -188,18 +188,19 @@ namespace VelNet
public int masterId; public int masterId;
} }
public class YouJoinedMessage: Message public class YouJoinedMessage : Message
{ {
public List<int> ids; public List<int> ids;
public string room; public string room;
} }
public class PlayerLeftMessage: Message public class PlayerLeftMessage : Message
{ {
public int userId; public int userId;
public string room; public string room;
} }
public class YouLeftMessage: Message
public class YouLeftMessage : Message
{ {
public string room; public string room;
} }
@ -296,8 +297,6 @@ namespace VelNet
} }
case RoomsMessage rm: case RoomsMessage rm:
{ {
Debug.Log("Got Rooms Message:\n" + rm);
try try
{ {
RoomsReceived?.Invoke(rm); RoomsReceived?.Invoke(rm);
@ -312,7 +311,6 @@ namespace VelNet
} }
case RoomDataMessage rdm: case RoomDataMessage rdm:
{ {
Debug.Log("Got room data message:" + rdm.room);
try try
{ {
RoomDataReceived?.Invoke(rdm); RoomDataReceived?.Invoke(rdm);
@ -322,6 +320,7 @@ namespace VelNet
{ {
Debug.LogError(e); Debug.LogError(e);
} }
break; break;
} }
case YouJoinedMessage jm: case YouJoinedMessage jm:
@ -331,17 +330,15 @@ namespace VelNet
masterPlayer = null; masterPlayer = null;
foreach(int playerId in jm.ids) foreach (int playerId in jm.ids)
{ {
VelNetPlayer player = new VelNetPlayer VelNetPlayer player = new VelNetPlayer
{ {
isLocal = playerId == userid,
room = jm.room, room = jm.room,
userid = playerId userid = playerId,
isLocal = playerId == userid,
}; };
players.Add(player.userid, player); players.Add(player.userid, player);
} }
try try
@ -355,7 +352,9 @@ namespace VelNet
Debug.LogError(e); Debug.LogError(e);
} }
foreach(KeyValuePair<int,VelNetPlayer> kvp in players) foreach (KeyValuePair<int, VelNetPlayer> kvp in players)
{
if (kvp.Key != userid)
{ {
try try
{ {
@ -367,12 +366,12 @@ namespace VelNet
Debug.LogError(e); Debug.LogError(e);
} }
} }
}
break; break;
} }
case YouLeftMessage lm: case YouLeftMessage msg:
{ {
string oldRoom = LocalPlayer?.room; string oldRoom = LocalPlayer?.room;
// delete all networkobjects that aren't sceneobjects or are null now // delete all networkobjects that aren't sceneobjects or are null now
@ -401,6 +400,7 @@ namespace VelNet
{ {
Debug.LogError(e); Debug.LogError(e);
} }
break; break;
} }
case PlayerLeftMessage lm: case PlayerLeftMessage lm:
@ -446,11 +446,11 @@ namespace VelNet
{ {
Debug.LogError(e); Debug.LogError(e);
} }
break; break;
} }
case JoinMessage jm: case JoinMessage jm:
{ {
// we got a join message, create it // we got a join message, create it
VelNetPlayer player = new VelNetPlayer VelNetPlayer player = new VelNetPlayer
{ {
@ -470,7 +470,6 @@ namespace VelNet
} }
break; break;
} }
case DataMessage dm: case DataMessage dm:
@ -500,6 +499,7 @@ namespace VelNet
{ {
Debug.LogError("Scene Network ID is 0. Make sure to assign one first.", sceneObjects[i]); Debug.LogError("Scene Network ID is 0. Make sure to assign one first.", sceneObjects[i]);
} }
sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId; sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId;
sceneObjects[i].owner = masterPlayer; sceneObjects[i].owner = masterPlayer;
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
@ -599,7 +599,6 @@ namespace VelNet
AddMessage(new ConnectedMessage()); AddMessage(new ConnectedMessage());
while (true) while (true)
{ {
//read a byte //read a byte
MessageReceivedType type = (MessageReceivedType)stream.ReadByte(); MessageReceivedType type = (MessageReceivedType)stream.ReadByte();
@ -658,6 +657,7 @@ namespace VelNet
rdm.members.Add(new Tuple<int, string>(client_id, username)); rdm.members.Add(new Tuple<int, string>(client_id, username));
Debug.Log(username); Debug.Log(username);
} }
AddMessage(rdm); AddMessage(rdm);
break; break;
} }
@ -696,10 +696,11 @@ namespace VelNet
YouJoinedMessage m = new YouJoinedMessage(); YouJoinedMessage m = new YouJoinedMessage();
int N = GetIntFromBytes(ReadExact(stream, 4)); int N = GetIntFromBytes(ReadExact(stream, 4));
m.ids = new List<int>(); m.ids = new List<int>();
for(int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{ {
m.ids.Add(GetIntFromBytes(ReadExact(stream, 4))); m.ids.Add(GetIntFromBytes(ReadExact(stream, 4)));
} }
N = stream.ReadByte(); N = stream.ReadByte();
byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8 byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8
m.room = Encoding.UTF8.GetString(utf8data); m.room = Encoding.UTF8.GetString(utf8data);
@ -725,7 +726,6 @@ namespace VelNet
AddMessage(m); AddMessage(m);
break; break;
} }
} }
} }
} }
@ -845,20 +845,22 @@ namespace VelNet
} }
/// <summary> /// <summary>
/// Connects to the server with a username /// Connects to the server for a particular app
/// </summary> /// </summary>
public static void Login(string username, string password) /// <param name="appName">A unique name for your app. Communication can only happen between clients with the same app name.</param>
/// <param name="deviceId">Should be unique per device that connects. e.g. md5(deviceUniqueIdentifier)</param>
public static void Login(string appName, string deviceId)
{ {
MemoryStream stream = new MemoryStream(); MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream); BinaryWriter writer = new BinaryWriter(stream);
byte[] uB = Encoding.UTF8.GetBytes(username); byte[] id = Encoding.UTF8.GetBytes(deviceId);
byte[] pB = Encoding.UTF8.GetBytes(password); byte[] app = Encoding.UTF8.GetBytes(appName);
writer.Write((byte)MessageSendType.MESSAGE_LOGIN); writer.Write((byte)MessageSendType.MESSAGE_LOGIN);
writer.Write((byte)uB.Length); writer.Write((byte)id.Length);
writer.Write(uB); writer.Write(id);
writer.Write((byte)pB.Length); writer.Write((byte)app.Length);
writer.Write(pB); writer.Write(app);
SendTcpMessage(stream.ToArray()); SendTcpMessage(stream.ToArray());
} }
@ -929,7 +931,7 @@ namespace VelNet
SendToRoom(mem.ToArray(), include_self, reliable, ordered); SendToRoom(mem.ToArray(), include_self, reliable, ordered);
} }
public static void SendCustomMessageToGroup(string group, byte[] message,bool reliable = true) public static void SendCustomMessageToGroup(string group, byte[] message, bool reliable = true)
{ {
using MemoryStream mem = new MemoryStream(); using MemoryStream mem = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(mem); using BinaryWriter writer = new BinaryWriter(mem);
@ -1049,7 +1051,7 @@ namespace VelNet
writer.Write((byte)MessageType.Instantiate); writer.Write((byte)MessageType.Instantiate);
writer.Write(newObject.networkId); writer.Write(newObject.networkId);
writer.Write(prefabName); writer.Write(prefabName);
SendToRoom(mem.ToArray(), include_self:false, reliable:true); SendToRoom(mem.ToArray(), include_self: false, reliable: true);
return newObject; return newObject;
} }