private MessageBase ReadMessaging(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartObject) throw new JsonReaderException("Messaging Message must be a JSON Object"); MessageBase msg = new MessageBase(); for (reader.Read(); reader.TokenType != JsonToken.EndObject; reader.Read()) { if (reader.TokenType != JsonToken.PropertyName) throw new JsonReaderException("JSON format error, JSON Object must have property"); string propertyName = (string)reader.Value; switch (propertyName) { case "destination": reader.Read(); if (reader.TokenType != JsonToken.String) throw new JsonReaderException("Messaging Message 'destination' must be JSON String"); msg.destination = (string)reader.Value; break; case "messageId": reader.Read(); if (reader.TokenType != JsonToken.String) throw new JsonReaderException("Messaging Message 'messageId' must be JSON String"); msg.messageId = (string)reader.Value; break; case "timestamp": reader.Read(); if (reader.TokenType != JsonToken.Integer) throw new JsonReaderException("Messaging Message 'timestamp' must be JSON Number"); msg.timestamp = (long)reader.Value; break; case "timeToLive": reader.Read(); if (reader.TokenType != JsonToken.Integer) throw new JsonReaderException("Messaging Message 'timeToLive' must be JSON Number"); msg.timeToLive = (long)reader.Value; break; case "headers": reader.Read(); if (reader.TokenType != JsonToken.StartObject) throw new JsonReaderException("Messaging Message headers must be object"); Dictionary<string, object> h = serializer.Deserialize<Dictionary<string, object>>(reader); msg.headers = h; break; case "clientId": reader.Read(); //cant definite clientId just a GUID string switch (reader.TokenType) { case JsonToken.String: Guid guid = new Guid(); if (Guid.TryParse((string)reader.Value, out guid)) { msg.clientId = reader.Value; } else { //maybe not appear ByteArray array = new ByteArray(); byte[] bytes = Encoding.Default.GetBytes((string)reader.Value); array.WriteBytes(bytes, 0, bytes.Length); msg.clientId = array; } break; case JsonToken.Null: msg.clientId = reader.Value; break; default: throw new NotSupportedException("Messaging Message clientId only support GUID JSON String, please report this"); break; } break; case "body": reader.Read(); switch (reader.TokenType) { case JsonToken.StartArray: ArrayList list = new ArrayList(); for (reader.Read(); reader.TokenType != JsonToken.EndArray; reader.Read()) { switch (reader.TokenType) { case JsonToken.Null: case JsonToken.Boolean: case JsonToken.Integer: case JsonToken.Float: case JsonToken.String: list.Add(reader.Value); break; default: throw new NotSupportedException("Messaging Message body[] only support JSON Boolean/Number/String, please report this"); } } msg.body = list.ToArray(); break; case JsonToken.StartObject: reader.Read(); if (reader.TokenType != JsonToken.EndObject) throw new NotSupportedException("Messaging Message body{} only support Empty JSON Object, please report this"); msg.body = new ASObject(); break; default: throw new NotSupportedException("Messaging Message body only support JSON Array/Object, please report this"); break; } break; } } return msg; }
public void Send(string act, string cmd, Object data) { if (!_tcpClient.Connected) throw new Exception("Closed, cannot send data."); var reqResVO = new ReqResVO(); reqResVO.accountId = SessionInfo._currentSysUserVO == null ? 0 : SessionInfo._currentSysUserVO.suAccountVO.saId; reqResVO.userId = SessionInfo._currentSysUserVO == null ? 0 : SessionInfo._currentSysUserVO.suId; reqResVO.sessionId = 0; reqResVO.action = act; reqResVO.command = cmd; reqResVO.data = data; //var id: String = getIdByVO(reqResVO); //if(callback != null) _callbackMap[id] = callback; //Logger.debug(SocketService,reqResVO.toString()); //var protocol: IProtocol = ProtocolBuilder.newProtocol(ProtocolType.AMF3_PROTOCOL); var protocol = new AMF3Protocol(); protocol.SetData(reqResVO); var b1 = protocol.GetBytes(); var b2 = FluorineFx.Util.Convert.ToByteArray(b1); var msg = new ByteArray(); msg.WriteInt(1); msg.WriteInt((int)b1.Length); var b3 = b2.Take((int)b1.Length).ToArray(); msg.WriteBytes(b3, 0, b3.Length); b3 = FluorineFx.Util.Convert.ToByteArray(msg).Take((int)msg.Length).ToArray(); Sysinfo("Sending " + act + "#" + cmd); _tcpClient.Client.Send(b3); }
private void ReadFrom(byte[] src) { var len = 0; if (_pos < PROTOCOL_TYPE_SIZE) { len = Math.Min(PROTOCOL_TYPE_SIZE - _pos, src.Length); if (len > 0) { _pTypeBuffer = _pTypeBuffer.Concat(src.Take(len)).ToArray(); src = src.Skip(len).ToArray(); if (_pTypeBuffer.Length == PROTOCOL_TYPE_SIZE) { _pTypeBuffer = _pTypeBuffer.Reverse().ToArray(); var c = BitConverter.ToInt32(_pTypeBuffer, 0); } _pos += len; } } if (_pos >= PROTOCOL_TYPE_SIZE && _pos < PROTOCOL_TYPE_SIZE + HEADER_SIZE) { len = Math.Min(PROTOCOL_TYPE_SIZE + HEADER_SIZE - _pos, src.Length); if (len > 0) { _pLenBuffer = _pLenBuffer.Concat(src.Take(len)).ToArray(); src = src.Skip(len).ToArray(); if (_pLenBuffer.Length == HEADER_SIZE) { _pLenBuffer = _pLenBuffer.Reverse().ToArray(); _dataLength = BitConverter.ToInt32(_pLenBuffer, 0); } _pos += len; } } if (_pos >= PROTOCOL_TYPE_SIZE + HEADER_SIZE) { len = Math.Min(_dataLength + PROTOCOL_TYPE_SIZE + HEADER_SIZE - _pos, src.Length); if (len > 0) { _pDataBuffer = _pDataBuffer.Concat(src.Take(len)).ToArray(); src = src.Skip(len).ToArray(); _pos += len; } } if (Remain == 0) { //var rawStr = Encoding.UTF8.GetString(_pDataBuffer, 0, _pDataBuffer.Length); //_testMsg = rawStr; //if (OnDataReceived != null) //{ // OnDataReceived(rawStr); //} //Sysinfo("Message Received."); var a3p = new AMF3Protocol(); //a3p.SetBytes var da = new ByteArray(); da.WriteBytes(_pDataBuffer, 0, _pDataBuffer.Length); a3p.SetBytes(da); if (OnAMFMsgReceived != null) { OnAMFMsgReceived(a3p); } Reset(); } if (src.Length > 0) { ReadFrom(src); } }
/// <summary> /// 刷花 /// </summary> public void Flower() { if ((!isLogined))//未登录 return; if(Common.micQueue.Count < 1)//无麦序 return; if (!ready)//未就绪 return; if (!changeChannelOk)//未加入频道 return; //uint my = 795759530; //uint mic = 113581884; uint m = 1; uint maxtype = 10; uint mintype = 3; //pack ByteArray pack = new ByteArray(); pack.WriteUnsignedInt(Common.endianLittleBig(uid)); pack.WriteUnsignedInt(Common.endianLittleBig(Common.micQueue[0])); pack.WriteUnsignedInt(Common.endianLittleBig(m));//刷的数量,且为1 //data ByteArray data = new ByteArray(); data.WriteUnsignedInt(Common.endianLittleBig(maxtype)); data.WriteUnsignedInt(Common.endianLittleBig(mintype)); UInt16 pl = (UInt16)pack.Length; short plength = (short)Common.endianLittleBig(pl); data.WriteShort(plength); byte[] buf = new byte[pack.Length]; pack.Position = 0; pack.ReadBytes(buf, (uint)0, (uint)buf.Length); data.WriteBytes(buf, 0, buf.Length); byte[] databuf = new byte[data.Length]; data.Position = 0; data.ReadBytes(databuf, (uint)0, (uint)databuf.Length); //senddata //00:00:00: 42 :0a:0b:01:07:75:72:69:04:82:46:19:61:70:70:5f:64:61:74:61:5f:72:65:71:0a:01:05:69:64:04:f5:1b:09:64:61:74:61:0c:2d: 0a:00:00:00:03:00:00:00:0c:00:aa:53:6e:2f:3c:1f:c5:06:01:00:00:00: 01:01 //00:00:00: 42 :0a:0b:01:07:75:72:69:04:82:46:19:61:70:70:5f:64:61:74:61:5f:72:65:71:0a:01:05:69:64:04:f5:1b:09:64:61:74:61:0c:2d: 0a:00:00:00:03:00:00:00:0c:00:aa:53:6e:2f:3c:1f:c5:06:01:00:00:00: 01:01 //00:00:00: 42: 0a:0b:01:07:75:72:69:04:82:46:19:61:70:70:5f:64:61:74:61:5f:72:65:71:0a:01:05:69:64:04:f5:1b:09:64:61:74:61:0c:2d: 0a:00:00:00:03:00:00:00:0c:00:aa:53:6e:2f:39:73:65:0b:01:00:00:00: 01:01 // byte[] front = { 0, 0, 0, 66, 10, 11, 1, 7, 117, 114, 105, 4, 130, 70, 25, 97, 112, 112, 95, 100, 97, 116, 97, 95, 114, 101, 113, 10, 1, 5, 105, 100, 4, 245, 27, 9, 100, 97, 116, 97, 12, 45 }; byte[] back = { 1, 1 }; //长度 int length = front.Length + databuf.Length + back.Length; front[3] = (byte)length; //组 int sendBuffIndex = 0; byte[] sendBuff = new byte[length]; front.CopyTo(sendBuff, sendBuffIndex); sendBuffIndex += front.Length; databuf.CopyTo(sendBuff, sendBuffIndex); sendBuffIndex += databuf.Length; back.CopyTo(sendBuff, sendBuffIndex); socket.Send(sendBuff); lastMsg = "完成刷花:"+DateTime.Now; }