-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkHelper.cs
113 lines (92 loc) · 3.9 KB
/
NetworkHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using SqualiveNetworking.Message.Processor;
using Unity.Collections;
using Unity.Networking.Transport;
using Unity.Networking.Transport.Error;
using UnityEngine;
namespace SqualiveNetworking
{
internal static class NetworkHelper
{
public static bool BeginSend( NetworkDriver driver, NetworkPipeline pipeline, MessageProcessorHandler messageProcessorHandler, MessageProcessor processor, NetworkConnection connection, out DataStreamWriter writer, int requiredPayloadSize = 0 )
{
int status = driver.BeginSend( pipeline, connection, out writer, requiredPayloadSize );
if ( status != 0 )
{
#if ENABLE_SQUALIVE_NET_DEBUG
Debug.Log( $"Failed to begin send message, status code: {status}" );
#endif
return false;
}
if ( !messageProcessorHandler.Equals( MessageProcessorHandler.Null ) && !processor.Equals( MessageProcessor.Null ) )
{
writer.WriteByte( processor.InternalID );
messageProcessorHandler.ProcessWrite( processor, ref writer );
}
else
{
writer.WriteByte( 0 );
}
return true;
}
public static bool BeginSend( NetworkDriver driver, NetworkConnection connection, out DataStreamWriter writer, int requiredPayloadSize = 0 )
{
return BeginSend( driver, NetworkPipeline.Null, MessageProcessorHandler.Null, MessageProcessor.Null, connection, out writer, requiredPayloadSize );
}
public static bool EndSend( NetworkDriver driver, DataStreamWriter writer )
{
var status = driver.EndSend( writer );
if ( status < 0 )
{
#if ENABLE_SQUALIVE_NET_DEBUG
Debug.LogError( $"Failed to send data ( {writer.Length} ), status code: {status}" );
#endif
return false;
}
return true;
}
private static void EndSend<T>( T netMessage, NetworkDriver driver, ref DataStreamWriter writer ) where T : unmanaged, INetMessage
{
writer.WriteUShort( netMessage.MessageID() );
netMessage.Serialize( ref writer );
EndSend( driver, writer );
}
public static void SendCustomMessage<T>( T netMessage, NetworkDriver driver, ref DataStreamWriter writer ) where T : unmanaged, INetMessage
{
writer.SetupCustomMessage();
EndSend( netMessage, driver, ref writer );
}
public static void SendInternalMessage<T>( T netMessage, NetworkDriver driver, ref DataStreamWriter writer ) where T : unmanaged, INetMessage
{
writer.SetupInternalMessage();
EndSend( netMessage, driver, ref writer );
}
private static DataStreamWriter SetupInternalMessage( this ref DataStreamWriter writer )
{
writer.WriteByte( 0 );
return writer;
}
private static DataStreamWriter SetupCustomMessage( this ref DataStreamWriter writer )
{
writer.WriteByte( 1 );
return writer;
}
public static FixedString32Bytes ToFixedString( this DisconnectReason reason )
{
switch ( reason )
{
case DisconnectReason.Timeout:
return "( Timed Out )";
case DisconnectReason.AuthenticationFailure:
return "( Auth Error )";
case DisconnectReason.ClosedByRemote:
return "( Closed By Remote )";
case DisconnectReason.ProtocolError:
return "( Protocol Error )";
case DisconnectReason.MaxConnectionAttempts:
return "( Connection Attempts Failed )";
default:
return "( Default )";
}
}
}
}