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.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
@ -27,6 +26,7 @@ namespace VelNet
YOU_LEFT = 7,
ROOM_DATA = 8
}
public enum MessageSendType
{
MESSAGE_OTHERS_ORDERED = 7,
@ -168,7 +168,7 @@ namespace VelNet
public class RoomDataMessage : Message
{
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
@ -199,6 +199,7 @@ namespace VelNet
public int userId;
public string room;
}
public class YouLeftMessage : Message
{
public string room;
@ -296,8 +297,6 @@ namespace VelNet
}
case RoomsMessage rm:
{
Debug.Log("Got Rooms Message:\n" + rm);
try
{
RoomsReceived?.Invoke(rm);
@ -312,7 +311,6 @@ namespace VelNet
}
case RoomDataMessage rdm:
{
Debug.Log("Got room data message:" + rdm.room);
try
{
RoomDataReceived?.Invoke(rdm);
@ -322,6 +320,7 @@ namespace VelNet
{
Debug.LogError(e);
}
break;
}
case YouJoinedMessage jm:
@ -333,15 +332,13 @@ namespace VelNet
foreach (int playerId in jm.ids)
{
VelNetPlayer player = new VelNetPlayer
{
isLocal = playerId == userid,
room = jm.room,
userid = playerId
userid = playerId,
isLocal = playerId == userid,
};
players.Add(player.userid, player);
}
try
@ -356,6 +353,8 @@ namespace VelNet
}
foreach (KeyValuePair<int, VelNetPlayer> kvp in players)
{
if (kvp.Key != userid)
{
try
{
@ -367,12 +366,12 @@ namespace VelNet
Debug.LogError(e);
}
}
}
break;
}
case YouLeftMessage lm:
case YouLeftMessage msg:
{
string oldRoom = LocalPlayer?.room;
// delete all networkobjects that aren't sceneobjects or are null now
@ -401,6 +400,7 @@ namespace VelNet
{
Debug.LogError(e);
}
break;
}
case PlayerLeftMessage lm:
@ -446,11 +446,11 @@ namespace VelNet
{
Debug.LogError(e);
}
break;
}
case JoinMessage jm:
{
// we got a join message, create it
VelNetPlayer player = new VelNetPlayer
{
@ -470,7 +470,6 @@ namespace VelNet
}
break;
}
case DataMessage dm:
@ -500,6 +499,7 @@ namespace VelNet
{
Debug.LogError("Scene Network ID is 0. Make sure to assign one first.", sceneObjects[i]);
}
sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId;
sceneObjects[i].owner = masterPlayer;
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
@ -599,7 +599,6 @@ namespace VelNet
AddMessage(new ConnectedMessage());
while (true)
{
//read a byte
MessageReceivedType type = (MessageReceivedType)stream.ReadByte();
@ -658,6 +657,7 @@ namespace VelNet
rdm.members.Add(new Tuple<int, string>(client_id, username));
Debug.Log(username);
}
AddMessage(rdm);
break;
}
@ -700,6 +700,7 @@ namespace VelNet
{
m.ids.Add(GetIntFromBytes(ReadExact(stream, 4)));
}
N = stream.ReadByte();
byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8
m.room = Encoding.UTF8.GetString(utf8data);
@ -725,7 +726,6 @@ namespace VelNet
AddMessage(m);
break;
}
}
}
}
@ -845,20 +845,22 @@ namespace VelNet
}
/// <summary>
/// Connects to the server with a username
/// Connects to the server for a particular app
/// </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();
BinaryWriter writer = new BinaryWriter(stream);
byte[] uB = Encoding.UTF8.GetBytes(username);
byte[] pB = Encoding.UTF8.GetBytes(password);
byte[] id = Encoding.UTF8.GetBytes(deviceId);
byte[] app = Encoding.UTF8.GetBytes(appName);
writer.Write((byte)MessageSendType.MESSAGE_LOGIN);
writer.Write((byte)uB.Length);
writer.Write(uB);
writer.Write((byte)pB.Length);
writer.Write(pB);
writer.Write((byte)id.Length);
writer.Write(id);
writer.Write((byte)app.Length);
writer.Write(app);
SendTcpMessage(stream.ToArray());
}