Changeset 7142


Ignore:
Timestamp:
2016-04-14 17:41:56 (3 years ago)
Author:
sieerinn
Message:

Serialisoinnin assembly nimiriippuvuus korjattu NetworkConnectionissa?. Bin ja obj kansiot lisätty ignore listalle.

Location:
2015/koodauskerho/sieerinn/TasohyppelyNettipeli
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • 2015/koodauskerho/sieerinn/TasohyppelyNettipeli

    • Property svn:ignore set to
      *.suo
  • 2015/koodauskerho/sieerinn/TasohyppelyNettipeli/GameServer

    • Property svn:ignore set to
      bin
      obj
  • 2015/koodauskerho/sieerinn/TasohyppelyNettipeli/TasohyppelyNettipeli/TasohyppelyNettipeli

    • Property svn:ignore set to
      *.cachefile
      bin
      obj
  • 2015/koodauskerho/sieerinn/TasohyppelyNettipeli/TasohyppelyNettipeli/TasohyppelyNettipeli/NetworkConnection.cs

    r7141 r7142  
    33using System.Linq; 
    44using System.Net.Sockets; 
     5using System.Reflection; 
     6using System.Runtime.Serialization; 
    57using System.Threading; 
    68using System.Runtime.Serialization.Formatters.Binary; 
     
    911public class NetworkConnection 
    1012{ 
     13    sealed class AssemblyBinder : SerializationBinder 
     14    { 
     15        public override Type BindToType(string assemblyName, string typeName) 
     16        { 
     17            return Type.GetType(String.Format("{0}, {1}", typeName, Assembly.GetExecutingAssembly().FullName)); 
     18        } 
     19    } 
     20 
     21    private const int BLOCK_SIZE = 2048; 
     22 
    1123    private readonly byte[] guidBytes; 
    1224    private readonly TcpClient client; 
     
    1426    private NetworkStream stream; 
    1527    private Thread receiveThread; 
    16     private Guid guid; 
    1728    private string gameName; 
    1829 
     
    3546        Connected = false; 
    3647        this.gameName = gameName; 
    37         guid = Guid.NewGuid(); 
     48        var guid = Guid.NewGuid(); 
    3849        guidBytes = guid.ToByteArray(); 
    3950        cancelSource = new CancellationTokenSource(); 
     
    6374    } 
    6475 
    65     private void Send(MessageType type) 
    66     { 
    67         var mstream = new MemoryStream(); 
    68         var writer = new BinaryWriter(mstream); 
    69         writer.Write((byte)type); 
    70         writer.Write(guidBytes, 0, guidBytes.Length); 
    71         writer.Write(gameName); 
    72         var sendBytes = mstream.ToArray(); 
    73         stream.Write(sendBytes, 0, sendBytes.Length); 
    74     } 
    75  
    76     private void SendWithMessage(MessageType type, string message) 
     76    private void Send(MessageType type, string message="") 
    7777    { 
    7878        var mstream = new MemoryStream(); 
     
    8282        writer.Write(guidBytes, 0, guidBytes.Length); 
    8383        writer.Write(gameName); 
    84         writer.Write(message); 
     84        if (message.Length > 0) 
     85            writer.Write(message); 
    8586 
    8687        var sendBytes = mstream.ToArray(); 
     
    9192    public void SendMessage(string message) 
    9293    { 
    93         SendWithMessage(MessageType.Message, message); 
     94        Send(MessageType.Message, message); 
    9495    } 
    9596 
     
    100101        { 
    101102            var formatter = new BinaryFormatter(); 
     103            formatter.Binder = new AssemblyBinder(); 
    102104            mstream.Seek(0, SeekOrigin.Begin); 
    103105            return formatter.Deserialize(mstream) as T; 
     
    121123        while (!cancelToken.IsCancellationRequested) 
    122124        { 
    123             if (client.Available <= 0) continue; 
     125            if (client.Available == 0) continue; 
    124126 
    125             var receivedBytes = new byte[2048]; 
    126             stream.Read(receivedBytes, 0, 2048); 
     127            var receivedBytes = new byte[BLOCK_SIZE]; 
     128            stream.Read(receivedBytes, 0, BLOCK_SIZE); 
    127129            var mstream = new MemoryStream(receivedBytes); 
    128130            var reader = new BinaryReader(mstream); 
     
    137139                OnMessageReceive(senderGuid, message); 
    138140            } 
    139  
    140             if (messageType == MessageType.Connected && OnConnect != null) 
     141            else if (messageType == MessageType.Connected && OnConnect != null) 
    141142            { 
    142143                OnConnect(senderGuid); 
    143144            } 
    144  
    145             if (messageType == MessageType.Disconnected && OnDisconnect != null) 
     145            else if (messageType == MessageType.Disconnected && OnDisconnect != null) 
    146146            { 
    147147                OnDisconnect(senderGuid); 
  • 2015/koodauskerho/sieerinn/TasohyppelyNettipeli/TasohyppelyNettipeli/TasohyppelyNettipeliContent

    • Property svn:ignore set to
      bin
      obj
Note: See TracChangeset for help on using the changeset viewer.