private void linker_ErrorOccurred(object sender, SocketHelper.Events.ErrorEventArgs e) { RemoteError re = new RemoteError(e.ErrorMessage); if (_adapter.IsConnected) { _adapter.Send(re.Serialize()); } destoryLinker(); }
private void Server_ClientAccepted(object sender, SocketHelper.Events.ClientAcceptedEventArgs args) { string endpoint = args.Adapter.RemoteEndPoint.ToString(); SocketAdapter adapter = args.Adapter; addEvent(adapter); adapter.Send((int)ParamCode.EnsureConnect); }
private void Adapter_ReceiveCompleted(object sender, ReceiveEventArgs args) { if (args.ByteLength > 0) { SocketAdapter adapter = sender as SocketAdapter; using (MemoryStream ms = new MemoryStream(args.ReceivedBytes)) { int code = PacketBase.ReadInt(ms); byte[] buffer = null; switch ((ParamCode)code) { case ParamCode.TimePeriods: buffer = getVideoTimePeriodsPacketBuffer(VideoBaseInfomParam.Decode(ms)); break; case ParamCode.VideoPacket: buffer = getVideoPacketBuffer(VideoDataParam.Decode(ms)); break; case ParamCode.VideoBaseInfo: VideoBaseInfomParam param = VideoBaseInfomParam.Decode(ms); Common.Log.Logger.Default.Trace($"下载视频:{param.BeginTime} - {param.EndTime} - {param.VideoName} - {param.VideoId} - {param.StreamId} -- {adapter.RemoteEndPoint}"); _isDownload = true; buffer = getVideoBasePacketBuffer(param); break; case ParamCode.StorageFlag: onStorageFlag(StorageFlagParam.Decode(ms)); break; case ParamCode.StorageFlagAll: buffer = getStorageFlagAll(); Console.WriteLine("StorageFlagAll: {0}", buffer.Length); break; } if (buffer != null) { adapter.Send(code, buffer); } } } }
internal ObexListenerContext(SocketAdapter s) { buffer = new byte[0x2000]; this.localEndPoint = s.LocalEndPoint; this.remoteEndPoint = s.RemoteEndPoint; bool moretoreceive = true; bool putCompleted = false; while (moretoreceive) { //receive the request and store the data for the request object int received = 0; try { while (received < 3) { int readLen = s.Receive(buffer, received, 3 - received, SocketFlags.None); if (readLen == 0) { moretoreceive = false; if (received == 0) { break; // Waiting for first byte of packet -- OK to close then. } else { throw new EndOfStreamException("Connection lost."); } } received += readLen; } //Debug.WriteLine(s.GetHashCode().ToString("X8") + ": RecvH", "ObexListener"); } catch (SocketException se) { //Console.Write(se.Message); HandleConnectionError(se); } if (received == 3) { ObexMethod method = (ObexMethod)buffer[0]; //get length (excluding the 3 byte header) short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 1)) - 3); if (len > 0) { int iPos = 0; while (iPos < len) { int wanted = len - iPos; Debug.Assert(wanted > 0, "NOT wanted > 0, is: " + wanted); int receivedBytes = s.Receive(this.buffer, iPos + 3, wanted, SocketFlags.None); if (receivedBytes == 0) { moretoreceive = false; throw new EndOfStreamException("Connection lost."); } iPos += receivedBytes; } } byte[] responsePacket; // Don't init, then the compiler will check that it's set below. //Debug.WriteLine(s.GetHashCode().ToString("X8") + ": Method: " + method, "ObexListener"); responsePacket = HandleAndMakeResponse(ref moretoreceive, ref putCompleted, method); try { System.Diagnostics.Debug.Assert(responsePacket != null, "Must always respond to the peer."); if (responsePacket != null) { s.Send(responsePacket); } } catch (Exception se) { //Console.WriteLine(se.Message); HandleConnectionError(se); } } else { moretoreceive = false; } }//while Debug.WriteLine(s.GetHashCode().ToString("X8") + ": Completed", "ObexListener"); s.Close(); s = null; if (!putCompleted) { // Should not return the request. throw new ProtocolViolationException("No PutFinal received."); } request = new ObexListenerRequest(bodyStream.ToArray(), headers, localEndPoint, remoteEndPoint); }
public void EnsureConnect() { _adapter.Send(new CodePacket(MessageCode.EnsureConnect).Serialize()); }