-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSSessionManager.java
More file actions
68 lines (57 loc) · 1.36 KB
/
Copy pathNSSessionManager.java
File metadata and controls
68 lines (57 loc) · 1.36 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
package com.nullspace.logic;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.nullspace.packet.NSMessage;
import com.nullspace.socket.NSSocketSession;
public class NSSessionManager
{
public static NSSessionManager manager = new NSSessionManager();
private AtomicInteger mIdGenerator = new AtomicInteger(1);
private ConcurrentHashMap<Integer, NSSocketSession> mSessions = null;
private NSSessionManager()
{
mSessions = new ConcurrentHashMap<Integer, NSSocketSession>();
}
public ConcurrentHashMap<Integer, NSSocketSession> Sessions()
{
return mSessions;
}
public void AddSession(NSSocketSession session)
{
int id = mIdGenerator.getAndIncrement();
session.id(id);
mSessions.put(id, session);
}
public void Remove(int id)
{
NSSocketSession session = mSessions.get(id);
if (session != null)
{
session.Close();
mSessions.remove(id);
}
}
public NSSocketSession GetSession(int id)
{
if (mSessions.containsKey(id))
{
return mSessions.get(id);
}
return null;
}
public void BroadAll(NSMessage msg)
{
BroadAllWithout(msg, -1);
}
public void BroadAllWithout(NSMessage msg, int id)
{
for (NSSocketSession sessionid : mSessions.values())
{
if (sessionid.id() != id)
{
msg.mHead.mSession = sessionid.id();
sessionid.WriteMessage(msg, null);
}
}
}
}