fixed connected event, added ordered tcp messages (not group), automatically running login/connect

BinaryServer
Kyle Johnsen 2022-01-21 08:59:02 -05:00
parent 9adb458b0f
commit 35f999f77b
2 changed files with 43 additions and 15 deletions

View File

@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Dissonance; using Dissonance;
@ -79,6 +80,17 @@ namespace VelNet
} }
}; };
*/ */
StartCoroutine(testes());
}
IEnumerator testes()
{
yield return new WaitForSeconds(1.0f);
HandleLogin();
yield return new WaitForSeconds(1.0f);
HandleJoin();
yield return null;
} }
public void handleMicrophoneSelection() public void handleMicrophoneSelection()

View File

@ -19,6 +19,8 @@ namespace VelNet
{ {
public enum MessageSendType public enum MessageSendType
{ {
MESSAGE_OTHERS_ORDERED = 7,
MESSAGE_ALL_ORDERED = 8,
MESSAGE_LOGIN = 0, MESSAGE_LOGIN = 0,
MESSAGE_GETROOMS = 1, MESSAGE_GETROOMS = 1,
MESSAGE_JOINROOM = 2, MESSAGE_JOINROOM = 2,
@ -128,6 +130,11 @@ namespace VelNet
public int masterId; public int masterId;
} }
public class ConnectedMessage: Message
{
}
public readonly List<Message> receivedMessages = new List<Message>(); public readonly List<Message> receivedMessages = new List<Message>();
private void Awake() private void Awake()
@ -146,20 +153,9 @@ namespace VelNet
}; };
} }
private IEnumerator Start() private void Start()
{ {
ConnectToTcpServer(); ConnectToTcpServer();
yield return null;
try
{
OnConnectedToServer?.Invoke();
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
} }
@ -181,6 +177,19 @@ namespace VelNet
{ {
switch (m) switch (m)
{ {
case ConnectedMessage connected:
{
try
{
OnConnectedToServer?.Invoke();
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
break;
}
case LoginMessage lm: case LoginMessage lm:
{ {
userid = lm.userId; userid = lm.userId;
@ -445,6 +454,8 @@ namespace VelNet
socketConnection = new TcpClient(host, port); socketConnection = new TcpClient(host, port);
socketConnection.NoDelay = true; socketConnection.NoDelay = true;
NetworkStream stream = socketConnection.GetStream(); NetworkStream stream = socketConnection.GetStream();
//now we are connected, so add a message to the queue
AddMessage(new ConnectedMessage());
//Join("MyRoom"); //Join("MyRoom");
//SendTo(MessageSendType.MESSAGE_OTHERS, Encoding.UTF8.GetBytes("Hello")); //SendTo(MessageSendType.MESSAGE_OTHERS, Encoding.UTF8.GetBytes("Hello"));
//FormGroup("close", new List<uint> { 1 }); //FormGroup("close", new List<uint> { 1 });
@ -632,7 +643,7 @@ namespace VelNet
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[] uB = Encoding.UTF8.GetBytes(username);
byte[] pB = Encoding.UTF8.GetBytes(password); byte[] pB = Encoding.UTF8.GetBytes(password);
writer.Write((byte)0); writer.Write((byte)0);
@ -685,9 +696,14 @@ namespace VelNet
} }
} }
public static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true) public static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false)
{ {
byte sendType = (byte)(include_self ? MessageSendType.MESSAGE_ALL : MessageSendType.MESSAGE_OTHERS); byte sendType = (byte) MessageSendType.MESSAGE_OTHERS;
if (include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_ALL_ORDERED;
if (include_self && !ordered) sendType = (byte)MessageSendType.MESSAGE_ALL;
if (!include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_OTHERS_ORDERED;
if (reliable) if (reliable)
{ {
MemoryStream stream = new MemoryStream(); MemoryStream stream = new MemoryStream();