-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetServer.cs
More file actions
117 lines (99 loc) · 3.67 KB
/
NetServer.cs
File metadata and controls
117 lines (99 loc) · 3.67 KB
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
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace NetAzy
{
public class NetServer
{
protected List<Connection> connections;
public List<Connection> Connections
{
get { return connections; }
}
protected Socket listener;
public NetServer()
{
this.connections = new List<Connection>();
}
public delegate void OnMessageReceivedDel(IncomingNetMessage msg, Connection con);
public OnMessageReceivedDel OnMessageReceived;
public delegate void OnConnectionLostDel(Connection con);
public OnConnectionLostDel OnConnectionLost;
public void SendMessage(OutgoingNetMessage msg)
{
foreach(Connection con in connections)
con.Socket.BeginSend(msg.Bytes, 0, msg.Size, 0, new AsyncCallback(send), con);
}
public void SendMessage(OutgoingNetMessage msg, Connection con)
{
con.Socket.BeginSend(msg.Bytes, 0, msg.Size, 0, new AsyncCallback(send), con);
}
private void send(IAsyncResult ar)
{
Connection con = (Connection)ar.AsyncState;
int bytesSent = con.Socket.EndSend(ar);
Console.WriteLine(bytesSent);
}
private void readMessage(IAsyncResult ar)
{
try
{
Connection con = (Connection)ar.AsyncState;
Socket handler = con.Socket;
// Read data from the client socket.
int read = handler.EndReceive(ar);
// Data was read from the client socket.
if (read > 0)
{
byte[] bytes = new byte[read];
for (int i = 0; i < read; i++)
{
bytes[i] = con.ReceiveBuffer[i];
}
IncomingNetMessage msg = new IncomingNetMessage(bytes);
if (OnMessageReceived != null)
OnMessageReceived(msg, con);
handler.BeginReceive(con.ReceiveBuffer, 0, con.ReceiveBufferSize, 0,
new AsyncCallback(readMessage), con);
}
}
catch (SocketException e)
{
if (e.ErrorCode == 10054)
{
Connection con = (Connection)ar.AsyncState;
Console.WriteLine("Client disconnected: " + con.IP);
if (OnConnectionLost != null)
OnConnectionLost(con);
connections.Remove(con);
con.Dispose();
}
else
{
throw new Exception("Unkown Error: " + e);
}
}
}
private void acceptConnection(IAsyncResult result)
{
Socket s = listener.EndAccept(result);
Connection con = new Connection(s);
this.connections.Add(con);
s.BeginReceive(con.ReceiveBuffer, 0, con.ReceiveBufferSize, 0, new AsyncCallback(readMessage), con);
listener.BeginAccept(acceptConnection, null);
}
public void Start(string str_ip, int port)
{
IPAddress ip = Helper.ResolveAdress(str_ip);
this.listener = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(ip, port);
listener.Bind(localEP);
listener.Listen(10);
this.listener.BeginAccept(acceptConnection, null);
}
}
}