Menu

[r2]: / Broadcaster.java  Maximize  Restore  History

Download this file

197 lines (153 with data), 4.9 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* <strong>广播消息处理</strong>
* <p>
*
* </p>
*
* @author: 汉娱网络.技术部.yangxf
* @date: 2008-01-04
* @version: 1.1
**/
package com.handjoys;
import java.nio.channels.SocketChannel;
import java.util.*;
import com.handjoys.util.*;
import com.handjoys.conf.ConfigParam;
import com.handjoys.conf.ConfigReader;
import com.handjoys.console.GameState;
import com.handjoys.console.MovableObject;
import com.handjoys.logger.FileLogger;
import com.handjoys.packet.MetaBroadCastMessage;
import com.handjoys.packet.SingleLine;
import com.handjoys.socket.GSession;
import com.handjoys.socket.GBBServer;
import com.handjoys.socket.RServerFactory;
public class Broadcaster {
public static final String BROADSYMBOL="##";
private static GameServer gs;
//用于定时广播信息的timer
private static Timer brocastTimer = new Timer(true);
//一个容器用于广播房间里面的人物状态信息
private static LinkedList<MetaBroadCastMessage> msgContiner = new LinkedList<MetaBroadCastMessage>();
private Broadcaster() {
;
}
/**初始化函数
* @param instance
*/
public static void init(GameServer instance) {
gs=instance;
//开始状态通知timer,一秒一次,1秒后就开始,1Hz
brocastTimer.schedule(new TimerTask() {
public void run() {
broadcastMsg();
}
}, 1000, 1000);
}
/**
* 广播消息,用于定时的广播
*/
private static void broadcastMsg()
{
if(msgContiner.size()>0){
LinkedList<MetaBroadCastMessage> tmp=null;
MetaBroadCastMessage msg=null;
synchronized(msgContiner){
tmp=msgContiner;
msgContiner.clear();
}
try{
if(tmp!=null){
//按顺序输出出去
while((msg=tmp.poll())!=null){
writeToRoom(msg.getRoomId(),msg.toMessagePacket().toString());
}
}
}catch(Exception e){
}
}
}
/**添加要广播的消息
* @param action
* @param roomId
* @param line
*/
public static void addBroadCastMsg(String action, long roomId, SingleLine line){
if(action!=null && line!=null){
MetaBroadCastMessage metaMsg=new MetaBroadCastMessage();
metaMsg.setAction(action);
metaMsg.setRoomId(roomId);
metaMsg.insertLine(line);
synchronized(msgContiner){
msgContiner.addLast(metaMsg);
}
}
}
/**向一个房间写信息
* @param roomId
* @param msg
*/
public static void writeToRoom(long roomId,String msg){
try{
MovableObject mo=new MovableObject(0,new Long(roomId).intValue());
Map userList = GameState.getInteractiveMo(mo);
if(userList!=null){
Iterator it = userList.values().iterator();
while (it.hasNext()) {
MovableObject tmpSprite = (MovableObject) it.next();
write(msg,new GSession(tmpSprite.getSessionID(),tmpSprite.getClient()));
}
}
}catch(Exception e){
}
}
//向外出队列中加入广播消息
public static void write(String msg, GSession session) {
// ##符号表示广播消息
gs.addBroadEvent(BROADSYMBOL + msg, session);
}
//向进入队列中加入广播消息
public static void append4LocalExecute(String msg, GSession session) {
// 用于本地处理
gs.addBroadEvent(msg, session);
}
/**
* 向排名服务器发送消息
* @param msg
*/
public static void write2GBB(String msg) {
System.out.println(GBBServer.getInstance());
// ##符号表示广播消息
gs.addBroadEvent(BROADSYMBOL + msg, GBBServer.getInstance());
}
/**向帐户服务器发送消息
* @param msg
*/
public static void write2ASServer(String msg){
try{
String serverName="";
Map rserverM=(HashMap)ConfigReader.getParam(ConfigParam.RSERVER);
Iterator it=rserverM.keySet().iterator();
String tmpString="";
while(it.hasNext()){
tmpString=(String)it.next();
//找字符串和loginserver相同的
if(tmpString.compareToIgnoreCase("AccountServer")==0){
serverName= tmpString;
break;
}
}
Broadcaster.write(msg,RServerFactory.getRServer(serverName));
}catch(Exception e){
}
}
/**踢掉一个Socket 根据SocketChannel
* @param channel
*/
public static void kickSocket(SocketChannel channel){
try{
gs.lostConnection(channel);
}catch(Exception e){
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.