Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TcpSocket.cs
Go to the documentation of this file.
1using System;
3using System.Net;
6using ReLogic.OS;
8
10
11public class TcpSocket : ISocket
12{
13 private byte[] _packetBuffer = new byte[1024];
14
16
17 private int _messagesInQueue;
18
20
22
23 private SocketConnectionAccepted _listenerCallback;
24
26
27 private bool _isListening;
28
30
31 public TcpSocket()
32 {
34 {
35 NoDelay = true
36 };
37 }
38
40 {
42 _connection.NoDelay = true;
43 IPEndPoint iPEndPoint = (IPEndPoint)tcpClient.Client.RemoteEndPoint;
45 }
46
48 {
49 _remoteAddress = null;
51 }
52
54 {
55 if (_connection == null || _connection.Client == null)
56 {
57 return false;
58 }
60 }
61
63 {
66 _remoteAddress = address;
67 }
68
74
75 private void SendCallback(IAsyncResult result)
76 {
79 {
81 }
82 else
83 {
84 object[] obj = (object[])result.AsyncState;
87 }
88 try
89 {
91 tuple.Item1(tuple.Item2);
92 }
93 catch (Exception)
94 {
95 ((ISocket)this).Close();
96 }
97 }
98
100 {
101 }
102
103 void ISocket.AsyncSend(byte[] data, int offset, int size, SocketSendCallback callback, object state)
104 {
105 if (!Platform.IsWindows)
106 {
107 byte[] array = LegacyNetBufferPool.RequestBuffer(data, offset, size);
108 _connection.GetStream().BeginWrite(array, 0, size, SendCallback, new object[2]
109 {
111 array
112 });
113 }
114 else
115 {
117 }
118 }
119
120 void ISocket.AsyncReceive(byte[] data, int offset, int size, SocketReceiveCallback callback, object state)
121 {
123 }
124
126 {
128 {
129 return false;
130 }
132 }
133
138
139 bool ISocket.StartListening(SocketConnectionAccepted callback)
140 {
141 IPAddress address = IPAddress.Any;
142 if (Program.LaunchParameters.TryGetValue("-ip", out var value) && !IPAddress.TryParse(value, out address))
143 {
144 address = IPAddress.Any;
145 }
146 _isListening = true;
147 _listenerCallback = callback;
148 if (_listener == null)
149 {
150 _listener = new TcpListener(address, Netplay.ListenPort);
151 }
152 try
153 {
155 }
156 catch (Exception)
157 {
158 return false;
159 }
161 thread.IsBackground = true;
162 thread.Name = "TCP Listen Thread";
163 thread.Start();
164 return true;
165 }
166
168 {
169 _isListening = false;
170 }
171
172 private void ListenLoop()
173 {
175 {
176 try
177 {
179 Console.WriteLine(Language.GetTextValue("Net.ClientConnecting", socket.GetRemoteAddress()));
181 }
182 catch (Exception)
183 {
184 }
185 }
186 _listener.Stop();
187 }
188}
static bool IsWindows
Definition Platform.cs:19
static void WriteLine()
Definition Console.cs:733
static bool TryParse([NotNullWhen(true)] string? ipString, [NotNullWhen(true)] out IPAddress? address)
Definition IPAddress.cs:303
static readonly IPAddress Any
Definition IPAddress.cs:19
override int EndRead(IAsyncResult asyncResult)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override void EndWrite(IAsyncResult asyncResult)
NetworkStream GetStream()
Definition TcpClient.cs:391
void Connect(string hostname, int port)
Definition TcpClient.cs:209
static string GetTextValue(string key)
Definition Language.cs:15
static byte[] RequestBuffer(int size)
static void ReturnBuffer(byte[] buffer)
TcpSocket(TcpClient tcpClient)
Definition TcpSocket.cs:39
SocketConnectionAccepted _listenerCallback
Definition TcpSocket.cs:23
void ReadCallback(IAsyncResult result)
Definition TcpSocket.cs:69
void SendCallback(IAsyncResult result)
Definition TcpSocket.cs:75
List< object > _callbackBuffer
Definition TcpSocket.cs:15
static bool Disconnect
Definition Netplay.cs:55
static int ListenPort
Definition Netplay.cs:47
static Dictionary< string, string > LaunchParameters
Definition Program.cs:29
void AsyncSend(byte[] data, int offset, int size, SocketSendCallback callback, object state=null)
void Connect(RemoteAddress address)
bool StartListening(SocketConnectionAccepted callback)
void AsyncReceive(byte[] data, int offset, int size, SocketReceiveCallback callback, object state=null)
RemoteAddress GetRemoteAddress()
delegate void SocketReceiveCallback(object state, int size)
delegate void SocketSendCallback(object state)