//you gots data private void RxDone(int iNumBytes) { BaseDataPackage data; // save the current read position long lPos = _ms.Position; // seek to the end to append the data _ms.Seek(0, SeekOrigin.End); // append the new data to the end of the stream _ms.Write(_bBuff, 0, iNumBytes); // restore the read position back to where it started _ms.Position = lPos; // show byte count received //Console.WriteLine("RxDone: Added " + iNumBytes.ToString() + //" bytes... for a total of " + (_ms.Length - _ms.Position).ToString()); _iRecieves++; do { // attempt to extract 1 or more complete frames // save the stream position in case the // deserialization fails, and it has to be reset long lStartPos = _ms.Position; try { // attempt to deserialize an object at this position object o = _bf.Deserialize(_ms); // no exception, so process the received frame and move on if (o is BaseDataPackage) { data = (o as BaseDataPackage); _iFrames++; DataHandlers.CallMethod(_context, data); } else { Console.WriteLine("RxDone: unknown frame"); } } catch (System.Runtime.Serialization.SerializationException) { // deserialize failed, so move the read position back // assume more data will show up that this item needs to be deserialized _ms.Position = lStartPos; // show that this time, a full object could not be pulled Console.WriteLine("RxDone: Could not deserialize.... yet..."); _iFragments++; // get out of the destacking loop break; } }while (_ms.Position < _ms.Length); //update stats lables //if (_iBytesTotal < 1024) // lBytes.Text = "Bytes RX'ed: B" + _iBytesTotal.ToString(); //else // if (_iBytesTotal < 1048576) // lBytes.Text = "Bytes RX'ed: kB" + (_iBytesTotal / 1024.0).ToString("f2"); //else // if (m_iBytesTotal < 1073741824) // lBytes.Text = "Bytes RX'ed: MB" + (_iBytesTotal / 1048576.0).ToString("f2"); //else // lBytes.Text = "Bytes RX'ed: MB" + (_iBytesTotal / 1073741824.0).ToString("f2"); //lDestackAvg.Text = "Destack Avg.: " + (_iFrames / (double)_iRecieves).ToString("f2"); //lFragments.Text = "Fragments: " + _iFragments.ToString(); //lFrames.Text = "Frames RX'ed: " + _iFrames.ToString(); // if all data has been read from the rx memorystream, reset it // otherwise it will continue to hold all data EVER received if (_ms.Position == _ms.Length) { _ms.Position = 0; _ms.SetLength(0); Console.WriteLine("RxDone: Resetting RX Memorystream..."); } // do optional trim, if buffer pounding is a concern // start another rx operation try { _socket.BeginReceive(_bBuff, 0, _bBuff.Length, SocketFlags.None, cbRxDone, _socket); } catch (Exception) { Console.WriteLine("RxDone:BeginReceive"); } }
/// <summary> /// Check if data is a complete package /// </summary> private void DataReceived(ClientReference client) { //remove client if it is required if (client.BytesReceived == 0) { try { //dispatch call into the proper thread _context.Post(s => { ClientDisconnected?.Invoke(client.Id); }, null); //ClientDisconnected?.Invoke(client.Id); } catch (Exception ex) { //dont trust user code Console.WriteLine("Calling server ClientDisconnected event handler threw an exception"); } lock (_clients) { _clients.Remove(client); } return; } //LineSegment LS; //Getting ready for some Lines received //List<LineSegment> lsList = new List<LineSegment>(); //Putting the memory stream pointer in the correct place long lPos = client.ms.Position; client.ms.Seek(0, SeekOrigin.End); client.ms.Write(client.Buffer, 0, client.BytesReceived); client.ms.Position = lPos; do //Defragmenting copy pasta { long lStartPos = client.ms.Position; try { object o = _bf.Deserialize(client.ms); //deserialize into object to check for types if (o is BaseDataPackage) { //Data received is of the correct type ClientReference.TotalRecieved++; client.Recieved++; //calls the method registered to handle this package DataHandlers.CallMethod(_context, (o as BaseDataPackage), client.Id); } else { //what the heck was this type? Someone trying to break our server! Console.WriteLine("unknown shiz"); } } catch (SerializationException) { //Get out of loop and wait for more data Console.WriteLine("fragmentation of data"); client.ms.Position = lStartPos; client.Fragments++; //increases the number of fragments that happened per connection break; } }while (client.ms.Position < client.ms.Length); //client.RecieveCalls++; //display traffic information //string s; //if (ClientReference.TotalBytes < 1024) // s = "Bytes RX'ed: B" + ClientReference.TotalBytes.ToString(); //else // if (ClientReference.TotalBytes < 1048576) // s = "Bytes RX'ed: kB" + (ClientReference.TotalBytes / 1024.0).ToString("f2"); //else // if (ClientReference.TotalBytes < 1073741824) // s = "Bytes RX'ed: MB" + (ClientReference.TotalBytes / 1048576.0).ToString("f2"); //else // s = "Bytes RX'ed: MB" + (ClientReference.TotalBytes / 1073741824.0).ToString("f2"); //Text = "TolalBytes: " + s + " TotalFrames: " + CSam.TotalRecieved.ToString(); //all data in stream has been read, reset stream if (client.ms.Position == client.ms.Length) { //reset the memory stream pointer and length client.ms.Position = 0; client.ms.SetLength(0); Console.WriteLine("Resetting RX Memory stream"); } try { //Receive more stuff! client.sock.BeginReceive(client.Buffer, 0, client.Buffer.Length, SocketFlags.None, cbRxDone, client); } catch (Exception ex) { Console.WriteLine("DataReceived:BeginReceive at end " + ex.ToString()); } }