Skip to content

Commit

Permalink
Updated for latest Mirror (v4.0.6)
Browse files Browse the repository at this point in the history
small changes to ServerSend & ClientReceive.
  • Loading branch information
Raystorms committed Oct 2, 2019
1 parent 9115b01 commit b648c08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
7 changes: 3 additions & 4 deletions Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum ConnectionState : byte {
}

public event Action<Exception> OnReceivedError;
public event Action<byte[]> OnReceivedData;
public event Action<byte[], int> OnReceivedData;
public event Action OnConnected;
public event Action OnDisconnected;

Expand Down Expand Up @@ -135,8 +135,7 @@ public async void Disconnect()
if (!Disconnected)
{
SendInternal(hostSteamID, disconnectMsgBuffer);

state = ConnectionState.DISCONNECTED;
Disconnected = true;
cancelToken.Cancel();

//Wait a short time before calling steams disconnect function so the message has time to go out
Expand Down Expand Up @@ -168,7 +167,7 @@ private async Task ReceiveLoop()
continue;
}
// we received some data, raise event
OnReceivedData?.Invoke(receiveBuffer);
OnReceivedData?.Invoke(receiveBuffer, i);
}
}
//not got a message - wait a bit more
Expand Down
9 changes: 5 additions & 4 deletions FizzySteamyMirror.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using Steamworks;
using System;
using System.Collections.Generic;

namespace Mirror.FizzySteam
{
Expand All @@ -27,13 +28,13 @@ public FizzySteamyMirror()
// dispatch the events from the server
server.OnConnected += (id) => OnServerConnected?.Invoke(id);
server.OnDisconnected += (id) => OnServerDisconnected?.Invoke(id);
server.OnReceivedData += (id, data) => OnServerDataReceived?.Invoke(id, new ArraySegment<byte>(data));
server.OnReceivedData += (id, data, channel) => OnServerDataReceived?.Invoke(id, new ArraySegment<byte>(data), channel);
server.OnReceivedError += (id, exception) => OnServerError?.Invoke(id, exception);

// dispatch events from the client
client.OnConnected += () => OnClientConnected?.Invoke();
client.OnDisconnected += () => OnClientDisconnected?.Invoke();
client.OnReceivedData += (data) => OnClientDataReceived?.Invoke(new ArraySegment<byte>(data));
client.OnReceivedData += (data, channel) => OnClientDataReceived?.Invoke(new ArraySegment<byte>(data), channel);
client.OnReceivedError += (exception) => OnClientError?.Invoke(exception);

Debug.Log("FizzySteamyMirror initialized!");
Expand All @@ -42,7 +43,7 @@ public FizzySteamyMirror()
// client
public override bool ClientConnected() { return client.Connected; }
public override void ClientConnect(string address) { client.Connect(address); }
public override bool ClientSend(int channelId, byte[] data) { return client.Send(data, channelId); }
public override bool ClientSend(int channelId, ArraySegment<byte> segment) { return client.Send(segment.Array, channelId); }
public override void ClientDisconnect() { client.Disconnect(); }

// server
Expand All @@ -57,7 +58,7 @@ public virtual void ServerStartWebsockets(string address, int port, int maxConne
Debug.LogError("FizzySteamyMirror.ServerStartWebsockets not possible!");
}

public override bool ServerSend(int connectionId, int channelId, byte[] data) { return server.Send(connectionId, data, channelId); }
public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> segment) { return server.Send(connectionIds, segment.Array, channelId); }

public override bool ServerDisconnect(int connectionId)
{
Expand Down
30 changes: 13 additions & 17 deletions Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ enum ConnectionState : byte
}

public event Action<int> OnConnected;
public event Action<int, byte[]> OnReceivedData;
public event Action<int, byte[], int> OnReceivedData;
public event Action<int> OnDisconnected;
public event Action<int, Exception> OnReceivedError;

Expand Down Expand Up @@ -215,7 +215,7 @@ private async Task ReceiveLoop()
try {
int connectionId = steamConnectionMap.fromSteamID[clientSteamID].connectionID;
// we received some data, raise event
OnReceivedData?.Invoke(connectionId, receiveBuffer);
OnReceivedData?.Invoke(connectionId, receiveBuffer,i);
} catch (KeyNotFoundException) {
CloseP2PSessionWithUser(clientSteamID);
//we have no idea who this connection is
Expand Down Expand Up @@ -284,22 +284,18 @@ private async void Disconnect(SteamClient steamClient)
CloseP2PSessionWithUser(steamClient.steamID);
}

public bool Send(int connectionId, byte[] data, int channelId = 0)
{
try
{
SteamClient steamClient = steamConnectionMap.fromConnectionID[connectionId];
//will default to reliable at channel 0 if sent on an unknown channel
Send(steamClient.steamID, data, channelToSendType(channelId), channelId >= channels.Length ? 0 : channelId);
return true;
}
catch (KeyNotFoundException)
{
//we have no idea who this connection is
Debug.LogError("Tryign to Send on a connection thats not known " + connectionId);
return false;
public bool Send(List<int> connectionIds, byte[] data, int channelId = 0) {
for (int i = 0; i < connectionIds.Count; i++) {
try {
SteamClient steamClient = steamConnectionMap.fromConnectionID[connectionIds[i]];
//will default to reliable at channel 0 if sent on an unknown channel
Send(steamClient.steamID, data, channelToSendType(channelId), channelId >= channels.Length ? 0 : channelId);
} catch (KeyNotFoundException) {
//we have no idea who this connection is
Debug.LogError("Tryign to Send on a connection thats not known " + connectionIds[i]);
}
}

return true;
}

public string ServerGetClientAddress(int connectionId)
Expand Down

0 comments on commit b648c08

Please sign in to comment.