koala написав:FakiNyan написав:Хаі-хаі! Чому, якщо в статичному класі написати...
http://msdn.microsoft.com/ru-RU/library/79b3xss3.aspx
Статический класс в основном такой же, что и нестатический класс, но имеется одно отличие: нельзя создавать экземпляры статического класса.
Ви вже розберіться, що і як створюєте. А то розгадувати ваші загадки не цікаво.
Які загадки. Є статичний клас, є нестатичний. В статичному оголошується екземпляр нестатичного класу, де тут загадка?
от статичний
using System.Threading;
using UnityEngine;
using System.Collections;
using SCdll.NSClient;
public static class TempStorage
{
private static string errorMessage = "";
public static MyGUIManager gui;
public static string ErrorMessage
{
get { return errorMessage; }
set { errorMessage = value; }
}
public static Communicator communicator; // оцей класс
public static ScreenSize screenSize=new ScreenSize();
public static int userId;
public static string login, password;
}
от нестатичний
using System;
using System.Threading;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using SCdll.NSClient.ClientInterface;
using SCdll.NSServer;
using SCdll.NSServer.ServerInterface;
public class Communicator
{
private Dictionary<string, Delegate> executeResults;
private Client client;
private string host;
private int port;
private byte[] buffer1 = new byte[1024];
public Communicator()
{
executeResults=new Dictionary<string, Delegate>();
host = "127.0.0.1";
port = 9070;
client = new Client();
client.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Start();
Debug.Log("starting connection...");
}
public void Start()
{
TempStorage.ErrorMessage = "Trying to connect...";
client.socket.BeginConnect(host, port, new AsyncCallback(ConnectionCallback), client);
}
private void ConnectionCallback(IAsyncResult result)
{
try
{
Client client = (Client) result.AsyncState;
client.socket.EndConnect(result);
Debug.Log("status = "+client.socket.Connected);
client.socket.BeginReceive(buffer1, 0, buffer1.Length, SocketFlags.None,
new AsyncCallback(ReceiveCallback), client);
TempStorage.ErrorMessage = "";
}
catch (Exception e)
{
TempStorage.ErrorMessage = e.Message;
Debug.Log(e.Message);
}
}
private void SendCallback(IAsyncResult result)
{
try
{
Client client1 = (Client) result.AsyncState;
client1.socket.EndSend(result);
}
catch (Exception e)
{
Debug.Log(e.Message + " | SendCallback");
}
}
public void Send(IClientData data,Delegate del)
{
try
{
if (client.socket.Connected)
{
executeResults.Add(data.GetKeyWord(), del);
Debug.Log(data.GetKeyWord());
Send_1(data);
}
else
{
Debug.Log("starting from Send 0");
Start();
}
}
catch (Exception e)
{
Debug.Log(e.Message + " | Send");
}
}
public void Send(IClientData data)
{
try
{
if (client.socket.Connected)
{
Send_1(data);
}
else
{
Debug.Log("starting from Send 1");
Start();
}
}
catch (Exception e)
{
Debug.Log(e.Message+" | Send");
}
}
private void Send_1(IClientData data)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
formatter.Serialize(memoryStream, data);
client.buffer = memoryStream.GetBuffer();
client.socket.BeginSend(client.buffer, 0, client.buffer.Length, SocketFlags.None,
new AsyncCallback(SendCallback), client);
}
private void ReceiveCallback(IAsyncResult result)
{
try
{
Client client1 = (Client) result.AsyncState;
int numberReceiveBytes = client1.socket.EndReceive(result);
Debug.Log("received data "+numberReceiveBytes+" bytes");
if (numberReceiveBytes > 0)
{
IFormatter formatter = new BinaryFormatter();
IServerData serverResult = (IServerData) formatter.Deserialize(new MemoryStream(buffer1));
serverResult.Execute(executeResults);
client1.socket.BeginReceive(buffer1, 0, buffer1.Length, SocketFlags.None,
new AsyncCallback(ReceiveCallback),
client1);
}
}
catch (Exception e)
{
Debug.Log(e.Message+" | ReceiveCallback");
}
}
}