/// <summary> /// used by TPeer* /// </summary> public override PhotonSocketError Send(byte[] data, int length) { if (this.State != PhotonSocketState.Connected) { return(PhotonSocketError.Skipped); } try { if (this.ReportDebugOfLevel(DebugLevel.ALL)) { this.Listener.DebugReturn(DebugLevel.ALL, "Sending: " + SupportClass.ByteArrayToString(data)); } this.sock.Send(data); } catch (Exception e) { this.Listener.DebugReturn(DebugLevel.ERROR, "Cannot send to: " + this.ServerAddress + ". " + e.Message); HandleException(StatusCode.Exception); return(PhotonSocketError.Exception); } return(PhotonSocketError.Success); }
public IEnumerator ReceiveLoop() { this.Listener.DebugReturn(DebugLevel.INFO, "ReceiveLoop()"); while (!this.sock.Connected && this.sock.Error == null) { yield return(new WaitForSeconds(0.1f)); } if (this.sock.Error != null) { this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread due to error: " + this.sock.Error + " Server: " + this.ServerAddress); this.HandleException(StatusCode.ExceptionOnConnect); } else { if (this.ReportDebugOfLevel(DebugLevel.ALL)) { this.Listener.DebugReturn(DebugLevel.ALL, "Receiving by websocket. this.State: " + State); } State = PhotonSocketState.Connected; while (State == PhotonSocketState.Connected) { if (this.sock.Error != null) { this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread (inside loop) due to error: " + this.sock.Error + " Server: " + this.ServerAddress); this.HandleException(StatusCode.ExceptionOnReceive); break; } else { byte[] inBuff = this.sock.Recv(); if (inBuff == null || inBuff.Length == 0) { yield return(new WaitForSeconds(0.1f)); continue; } if (this.ReportDebugOfLevel(DebugLevel.ALL)) { this.Listener.DebugReturn(DebugLevel.ALL, "TCP << " + inBuff.Length + " = " + SupportClass.ByteArrayToString(inBuff)); } // check if it's a ping-result (first byte = 0xF0). this is 9 bytes in total. no other headers! // note: its a coincidence that ping-result-size == header-size. if this changes we have to refactor this if (inBuff[0] == 0xF0) { try { HandleReceivedDatagram(inBuff, inBuff.Length, false); } catch (Exception e) { if (this.ReportDebugOfLevel(DebugLevel.ERROR)) { this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + ". Server: '" + this.ServerAddress + "' Exception: " + e); } this.HandleException(StatusCode.ExceptionOnReceive); } continue; } // get data and split the datagram into two buffers: head and body if (inBuff.Length > 0) { try { HandleReceivedDatagram(inBuff, inBuff.Length, false); } catch (Exception e) { if (this.ReportDebugOfLevel(DebugLevel.ERROR)) { this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + ". Server: '" + this.ServerAddress + "' Exception: " + e); } this.HandleException(StatusCode.ExceptionOnReceive); } } } } } Disconnect(); }
private void _parseMessage(byte[] inBuff, BinaryReader br) { int length = inBuff.Length; using (br) { using (Stream baseStream = br.BaseStream) { while (baseStream.Position != baseStream.Length) { switch (br.ReadByte()) { case 251: int count = HttpBase3._readMessageHeader(br) - 7; if (count == -1) { if (this.debugOut < DebugLevel.ERROR) { return; } this.Listener.DebugReturn(DebugLevel.ERROR, string.Format("Invalid message header for pid={0} cid={1} and message {2}", (object)this.HttpPeerID, (object)this._challengId, (object)SupportClass.ByteArrayToString(inBuff))); return; } System.Diagnostics.Debug.Assert(count >= 2); byte[] numArray = br.ReadBytes(count); if (count < 2) { this.Listener.DebugReturn(DebugLevel.WARNING, string.Format("data len is to small. data {0}", (object)SupportClass.ByteArrayToString(inBuff))); } lock (this.incomingList) { this.incomingList.Add(numArray); if (this.incomingList.Count % this.warningSize == 0) { this.EnqueueStatusCallback(StatusCode.QueueIncomingReliableWarning); break; } break; } case 240: this.ReadPingResponse(br); break; default: this.Listener.DebugReturn(DebugLevel.WARNING, "Unknow response from server"); break; } } } } }
private bool DispatchIncommingActions() { lock (this.ActionQueue) { while (this.ActionQueue.Count > 0) { this.ActionQueue.Dequeue()(); } } byte[] incoming; lock (this.incomingList) { if (this.incomingList.Count <= 0) { return(false); } incoming = this.incomingList[0]; this.incomingList.RemoveAt(0); } this.ByteCountCurrentDispatch = incoming.Length + 3; if (incoming.Length < 2) { this.Listener.DebugReturn(DebugLevel.WARNING, string.Format("message has length less then 2. data {0}", (object)SupportClass.ByteArrayToString(incoming))); } return(this.DeserializeMessageAndCallback(incoming)); }
/// <summary>Used by TPeer to receive.</summary> public void ReceiveLoop() { try { while (this.State == PhotonSocketState.Connected) { if (this.sock.Error != null) { this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread (inside loop). Server: " + this.ConnectAddress + " Error: " + this.sock.Error); this.HandleException(StatusCode.ExceptionOnReceive); break; } byte[] inBuff = this.sock.Recv(); if (inBuff == null || inBuff.Length == 0) { Thread.Sleep(0); continue; } if (inBuff.Length < 0) { // got disconnected (from remote or net) if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected) { this.HandleException(StatusCode.DisconnectByServer); } break; } if (this.ReportDebugOfLevel(DebugLevel.ALL)) { this.Listener.DebugReturn(DebugLevel.ALL, "TCP << " + inBuff.Length + " = " + SupportClass.ByteArrayToString(inBuff)); } this.HandleReceivedDatagram(inBuff, inBuff.Length, false); } } catch (Exception e) { if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected) { if (this.ReportDebugOfLevel(DebugLevel.ERROR)) { this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + ". Server: '" + this.ConnectAddress + "' Exception: " + e); } this.HandleException(StatusCode.ExceptionOnReceive); } } this.Disconnect(); }
public override object Deserialize(StreamBuffer din, byte type) { switch (type) { case 105: return(this.DeserializeInteger(din)); case 115: return(this.DeserializeString(din)); case 97: return(this.DeserializeStringArray(din)); case 120: return(this.DeserializeByteArray(din, -1)); case 110: return(this.DeserializeIntArray(din, -1)); case 104: return(this.DeserializeHashTable(din)); case 68: return(this.DeserializeDictionary(din)); case 111: return(this.DeserializeBoolean(din)); case 107: return(this.DeserializeShort(din)); case 108: return(this.DeserializeLong(din)); case 98: return(this.DeserializeByte(din)); case 102: return(this.DeserializeFloat(din)); case 100: return(this.DeserializeDouble(din)); case 121: return(this.DeserializeArray(din)); case 99: { byte customTypeCode = din.ReadByte(); return(this.DeserializeCustom(din, customTypeCode)); } case 122: return(this.DeserializeObjectArray(din)); case 101: return(this.DeserializeEventData(din, null)); case 113: return(this.DeserializeOperationRequest(din)); case 112: return(this.DeserializeOperationResponse(din)); case 0: case 42: return(null); default: throw new Exception("Deserialize(): " + type + " pos: " + din.Position + " bytes: " + din.Length + ". " + SupportClass.ByteArrayToString(din.GetBuffer())); } }