Ejemplo n.º 1
0
 public TcpBase(MessageType messageType)
 {
     MessageType = messageType;
     CommandTimeout = 30 * 1000;
     connection = null;
     packetSize = 8192;
 }
 internal MessageReceivedEventArgs(TcpConnection connection, ulong typeId, ulong uid, byte[] data)
 {
     Connection = connection;
     MessageTypeId = typeId;
     MessageUID = uid;
     MessageData = data;
 }
 internal MessageReceivedEventArgs(TcpConnection connection, Message msg)
 {
     Connection = connection;
     MessageTypeId = msg.MessageTypeId;
     MessageUID = msg.MessageUID;
     MessageData = msg.MessageData;
 }
Ejemplo n.º 4
0
 private void SendDatagram(TcpConnection connection, TcpDatagram datagram)
 {
     if (connection != null)
     {
         lock (connection) //Синхронизируем (если вдруг доступ из многих потоков, одного клиента)
         {
             OnBeginSending(new BeginSendingEventArgs(connection));
             byte[] data = new byte[datagram.Data.Length + 4];
             ConvertHelper.Int32ToBytes(datagram.Size, ref data);
             datagram.Data.Position = 0;
             datagram.Data.Read(data, 4, (int)datagram.Data.Length);
             Send(connection, data);
             OnEndSending(new EndSendingEventArgs(connection));
         }
     }
 }
Ejemplo n.º 5
0
 protected void SendPortion(TcpConnection connection, byte[] data)
 {
     try
     {
         connection.Socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(DataSent), connection);
     }
     catch (Exception e)
     {
         OnErrorOccured(new ErrorOccuredEventArgs(e));
     }
 }
Ejemplo n.º 6
0
 protected void Send(TcpConnection connection, byte[] buffer)
 {
     long num;
     byte[] array;
     for (num = 0L; num != buffer.LongLength / (long)packetSize; num += 1L)
     {
         array = new byte[packetSize];
         Array.Copy(buffer, (long)packetSize * num, array, 0L, (long)packetSize);
         SendPortion(connection, array);
     }
     array = new byte[buffer.LongLength % (long)packetSize];
     Array.Copy(buffer, (long)packetSize * num, array, 0L, buffer.LongLength % (long)packetSize);
     SendPortion(connection, array);
 }
Ejemplo n.º 7
0
 protected void ProcessData(TcpConnection connection)
 {
     if (connection.InBuffer.Length >= 4 || connection.Datagram != null)
     {
         if (connection.Datagram == null)
         {
             int szPakageRead = ConvertHelper.BytesToInt32(connection.InBuffer);
             connection.Datagram = new TcpDatagram(szPakageRead);
             Delete(ref connection, 4);
             if (connection.Datagram.Size > ((long)packetSize - 4))
             {
                 OnBeginReceiving(new BeginReceivingEventArgs(connection));
             }
         }
         if (connection.Length >= connection.Datagram.Size)
         {
             connection.Datagram.GetData(connection.InBuffer);
             Delete(ref connection, connection.Datagram.Size);
             if (connection.Datagram.Size > ((long)packetSize - 4))
             {
                 OnEndReceiving(new EndReceivingEventArgs(connection));
             }
             NewDatagram(connection);
             connection.Datagram = null;
             ProcessData(connection);
             return;
         }
         OnReceivingInProgress(new ReceivingInProgressEventArgs(connection, (int)(connection.Length * 100 / connection.Datagram.Size)));
     }
 }
Ejemplo n.º 8
0
 public EndReceivingEventArgs(TcpConnection connection)
 {
     Connection = connection;
 }
Ejemplo n.º 9
0
 public EndSendingEventArgs(TcpConnection connection)
 {
     Connection = connection;
 }
Ejemplo n.º 10
0
 protected void Append(ref TcpConnection connection, long length)
 {
     if (connection.InBuffer == null)
     {
         connection.InBuffer = new byte[length];
     }
     else
     {
         if (connection.InBuffer.LongLength - connection.Length < length)
         {
             connection.ResizeInputBuffer(connection.Length + length);
         }
     }
     Array.Copy(connection.Buffer, 0, connection.InBuffer, connection.Length, length);
     connection.Length += length;
 }
Ejemplo n.º 11
0
 protected internal void SendMessage(TcpConnection connection, ulong messageType, ulong messageUID, MemoryStream data)
 {
     byte[] buffer = new byte[data.Length];
     data.Position = 0;
     data.Read(buffer, 0, (int)data.Length);
     Message msg = new Message()
     {
         MessageTypeId = messageType,
         MessageUID = messageUID,
         MessageData = buffer
     };
     SendMessage(connection, msg);
 }
Ejemplo n.º 12
0
 protected internal void SendMessage(TcpConnection connection, ulong messageType, ulong messageUID, byte[] data)
 {
     Message msg = new Message()
     {
         MessageTypeId = messageType,
         MessageUID = messageUID,
         MessageData = data
     };
     SendMessage(connection, msg);
 }
Ejemplo n.º 13
0
 public DisconnectedEventArgs(TcpConnection connection)
 {
     Connection = connection;
 }
Ejemplo n.º 14
0
 public BeginSendingEventArgs(TcpConnection connection)
 {
     Connection = connection;
 }
Ejemplo n.º 15
0
 private void SendMessage(TcpConnection connection, Message message)
 {
     MemoryStream stream = new MemoryStream();
     switch(MessageType)
     {
         case MessageType.Simple:
             byte[] data;
             ConvertHelper.Int32ToNewBytes((int)message.MessageTypeId, out data);
             stream.Write(data, 0, 4);
             if (message.MessageData != null)
                 stream.Write(message.MessageData, 0, message.MessageData.Length);
             break;
         case MessageType.Protobuf:
             ProtoBuf.Serializer.Serialize<Message>(stream, message);
             break;
     }
     TcpDatagram datagram = new TcpDatagram((int)stream.Length);
     datagram.Data = stream;
     SendDatagram(connection, datagram);
 }
Ejemplo n.º 16
0
 protected void Delete(ref TcpConnection connection, long length)
 {
     byte[] array = new byte[connection.InBuffer.LongLength - length];
     Array.Copy(connection.InBuffer, length, array, 0, connection.InBuffer.LongLength - length);
     connection.InBuffer = array;
     connection.Length -= length;
 }
Ejemplo n.º 17
0
 protected internal void SendMessage(TcpConnection connection, ulong messageType)
 {
     Message msg = new Message()
     {
         MessageTypeId = messageType,
     };
     SendMessage(connection, msg);
 }
Ejemplo n.º 18
0
 protected void NewDatagram(TcpConnection connection)
 {
     Message msg = null;
     try
     {
         switch (MessageType)
         {
             case MessageType.Simple:
                 msg = new Message();
                 byte[] type = new byte[4];
                 connection.Datagram.Data.Read(type, 0, 4);
                 msg.MessageTypeId = (ulong)ConvertHelper.BytesToInt32(type);
                 msg.MessageData = new byte[connection.Datagram.Data.Length - 4];
                 connection.Datagram.Data.Read(msg.MessageData, 0, msg.MessageData.Length);
                 break;
             case MessageType.Protobuf:
                 msg = ProtoBuf.Serializer.Deserialize<Message>(connection.Datagram.Data);
                 break;
         }
     }
     catch (Exception ex)
     {
         OnErrorOccured(new ErrorOccuredEventArgs(ex));
     }
     if (msg != null)
     {
         OnMessageReceived(new MessageReceivedEventArgs(connection, msg.MessageTypeId, msg.MessageUID, msg.MessageData));
     }
 }
 public ReceivingInProgressEventArgs(TcpConnection connection, int percentage)
 {
     Connection = connection;
     Percentage = percentage;
 }
 public BeginReceivingEventArgs(TcpConnection connection)
 {
     Connection = connection;
 }