public object transform(object input) { NadEvent evt = (NadEvent)input; List <byte> byteslist = new List <byte>(); int opCode = evt.getType(); byte[] opbytes = System.BitConverter.GetBytes(opCode); byteslist.Add(opbytes[0]); //byteslist.AddRange(opbytes); if (opCode == Events.LOG_IN) { byte[] prbytes = System.BitConverter.GetBytes(Events.JET_PROTOCOL); byteslist.Add(prbytes[0]); // byteslist.AddRange(prbytes); } MessageBuffer messageBuffer = evt.getSource() as MessageBuffer; byteslist.AddRange(messageBuffer.getBuffer()); return(byteslist.ToArray()); }
public void SendMsg(IExtensible msg) { byte[] data = Serialize(msg); NadEvent evt = Events.convertEvent(Events.SESSION_MESSAGE, data); if (clientSocket.Connected == true) { LoginInOutCodecs loginCodecs = new LoginInOutCodecs(); byte[] dataBytes = loginCodecs.getOutCodecs().transform(evt) as byte[]; clientSocket.Send(dataBytes); } }
private void dispatchEvent(NadEvent events) { string cmd = Events.convertEventTypeToString(events.getType()); foreach (KeyValuePair <string, Action <NadEvent> > item in netEvents) { if (item.Key.Equals(cmd)) { Action <NadEvent> action = item.Value; action.Invoke(events); break; } } }
/* INTERFACE io.nadron.codecs.Transform */ public object transform(object input) { byte[] message = input as byte[]; //List<byte> headbytes = message.GetRange(0,1); int eventType = message[0]; //System.BitConverter.ToInt32(headbytes.ToArray(), 0); ; if (eventType == Events.NETWORK_MESSAGE) { eventType = Events.SESSION_MESSAGE; } MessageBuffer buffer = new MessageBuffer(message.Skip(1).ToList <byte>()); NadEvent events = Events.convertEvent(eventType, buffer); return(events); }
public void TestPrint(NadEvent evt) { Debug.Log("Test Events Send....,event string :" + evt.getType()); }
public void onConnect(NadEvent evt) { tipsstr = "已经连接到游戏服务器"; Debug.Log("Connect game server,event string :"+evt.getType()); }
private void initEvents(NadEvent evt) { Debug.Log("init event handler"); }
private void dispatchEvent(NadEvent events) { string cmd = Events.convertEventTypeToString(events.getType()); foreach (KeyValuePair<string, Action<NadEvent>> item in netEvents) { if (item.Key.Equals(cmd)) { Action<NadEvent> action=item.Value; action.Invoke(events); break; } } }
public void TestPrint(NadEvent evt) { Debug.Log("Test Events Send....,event string :"+evt.getType()); }
private void initEvents(NadEvent evt) { Debug.Log("init event handler"); }
void RecevieMessage() { while (true) { //在接收数据之前,判断socket连接是否断开 if (clientSocket.Poll(10, SelectMode.SelectRead)) { break; } //int length = clientSocket.Receive(data); //data 缓冲区默认为1024,一次性发送数据不应超过1024 int length = clientSocket.Receive(data, data.Length, SocketFlags.None); //length 返回从系统缓冲实际读取的字节 int readCount = 0; bool readEnd = false; if (length > 0) { do { //本次实际读取字(可能包含多条命令) byte[] msgbytes = data.Skip(readCount).Take(length).ToArray(); string msg = Encoding.UTF8.GetString(data, 0, length); Debug.Log("Recevied Data length:" + length); LoginInOutCodecs loginCodecs = new LoginInOutCodecs(); NadEvent nEvent = loginCodecs.getInCodecs().transform(msgbytes) as NadEvent; if (null != nEvent) { /* * 本次消息包含多条命令行 * 每条命令行包头为3个字节, * 如果本次读取的(包头+包体),不大于等于socket消息长度, * 说明还有消息存在,继续读取, */ readCount += (3 + (nEvent.getSource() as MessageBuffer).getBuffer().Length); if (length <= readCount) { readEnd = true; } else { //下条未读取消息的长度 length = length - readCount; } //本次消息派发(线程池启动派发) ThreadPool.QueueUserWorkItem(dispatchEvent, nEvent); } else { break; } } while (!readEnd); } Debug.Log("Read network data..."); // Decoding is not over, mostly because the whole frame // was not received by the LengthFieldBasedFrameDecoder. } }
public void onConnect(NadEvent evt) { tipsstr = "已经连接到游戏服务器"; Debug.Log("Connect game server,event string :" + evt.getType()); }